Wicket authentication

Create wicket project using quick start 
Add wicket authentication dependency to pom.xml
 
   <dependency>
   <groupId>org.apache.wicket</groupId>
   <artifactId>wicket-auth-roles</artifactId>
   <version>1.5.5</version>
  </dependency>
Project Structure

SignIn.html
 
<html>
<head>
    <title>SignIn</title>
</head>
<body>
    <span wicket:id="signInPanel"/>
</body>
</html>  

SignIn.java
 
package com.javaassist;

import org.apache.wicket.authroles.authentication.panel.SignInPanel;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.request.mapper.parameter.PageParameters;

public final class SignIn extends WebPage
{
  
    public SignIn(final PageParameters parameters)
    {
        add(new SignInPanel("signInPanel"));
    }
}

SignOut.html
 
<html>
<head>
    <title>SignOut</title>
</head>
<body>
    <wicket:link><a href="HomePage.html">Home</a></wicket:link>
</body>
</html>

SignOut.java
 
package com.javaassist;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.request.mapper.parameter.PageParameters;


public class SignOut extends WebPage
{

    public SignOut(final PageParameters parameters)
    {
        getSession().invalidate();
    }
}

HomePage.html
 
<html>
    <head>
        <title>Hello World</title>
    </head>
    <body>
   <wicket:link><a href="SignOut.html">Sign Out</a></wicket:link>
   <h1>
        <span wicket:id="message">message will be replace here</span>
   </h1>
    </body>
</html>
HomePage.java
 
package com.javaassist;

import org.apache.wicket.authroles.authorization.strategies.role.annotations.AuthorizeInstantiation;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;

@AuthorizeInstantiation("USER")
public class HomePage extends WebPage {
 
 private static final long serialVersionUID = 1L;
 
    public HomePage(final PageParameters parameters) {
 
        add(new Label("message", new Model<String>("Welcome to java-assist")));
 
    }
}

Create WebSession Class to validate User, to get the User Roles

AppWebSession.java
 
package com.javaassist;


import org.apache.wicket.authroles.authentication.AuthenticatedWebSession;
import org.apache.wicket.authroles.authorization.strategies.role.Roles;
import org.apache.wicket.request.Request;



public class AppWebSession extends AuthenticatedWebSession
{

    public AppWebSession(Request request)
    {
        super(request);
    }


    @Override
    public boolean authenticate(final String username, final String password)
    {
        final String WICKET = "java-assist";

        // Check username and password (modify the code to validate the user from database)
        return WICKET.equals(username) && WICKET.equals(password);
    }

   
    @Override
    public Roles getRoles()
    {
        if (isSignedIn())
        {
            
         // assing the role ( modify the code to add dynamic roles from database)
            return new Roles(Roles.USER);
        }
        return null;
    }
}

Create AuthenticatedWebApplication class to get websession class and SignInPage class

WicketApplication.java 
package com.javaassist;

import org.apache.wicket.authroles.authentication.AbstractAuthenticatedWebSession;
import org.apache.wicket.authroles.authentication.AuthenticatedWebApplication;
import org.apache.wicket.markup.html.WebPage;

public class WicketApplication extends AuthenticatedWebApplication
{     

 @Override
 public Class<HomePage> getHomePage()
 {
  return HomePage.class;
 }

 @Override
 protected Class<? extends AbstractAuthenticatedWebSession> getWebSessionClass() {
  return AppWebSession.class;
 }

 @Override
 protected Class<? extends WebPage> getSignInPageClass() {
  return SignIn.class;
 }
 
}

web.xml (add wicket filter)
 
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
 version="2.5">

 <display-name>wicket application</display-name>

 <filter>
  <filter-name>wicket.application</filter-name>
  <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
  <init-param>
   <param-name>applicationClassName</param-name>
   <param-value>com.javaassist.WicketApplication</param-value>
  </init-param>
 </filter>

 <filter-mapping>
  <filter-name>wicket.application</filter-name>
  <url-pattern>/*</url-pattern>
 </filter-mapping>
</web-app>

Run the Jetty server 
Test the app "http://localhost:8080/" Use username and password as "java-assist"




No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...