0 Replies Latest reply on Jun 23, 2010 7:56 PM by lcsuarezl

    Customizing your login!

    lcsuarezl

      Hello, initially I suppose to post to ask for help, but before I ask I try again and I found the answer by myself! and it is my first time with seam!


      So here is it:


      For customizing your login you need to do 4 things
      1- Create your custom Credential class extending the default:


      package org.jboss.seam.security.credentials;
      
      import org.jboss.seam.annotations.intercept.BypassInterceptors;
      import org.jboss.seam.security.Credentials;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Startup;
      import org.jboss.seam.core.Events;
      
      @Name("myCredentials")
      @BypassInterceptors
      @Startup
      public class MyCredentials extends Credentials {
      
           private String userID;
      
           public String getUserID() {
                return userID;
           }
      
           public void setUserID(String userID) {
                this.userID = userID;
                if (Events.exists()) Events.instance().raiseEvent(EVENT_CREDENTIALS_UPDATED);
           }
      }



      In this case I add a simple String userID field which I need in my Login form


      2- Create your custom Identity class extending the default:


      package org.jboss.seam.security.identity;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.annotations.Startup;
      import org.jboss.seam.annotations.intercept.BypassInterceptors;
      import org.jboss.seam.log.LogProvider;
      import org.jboss.seam.log.Logging;
      import org.jboss.seam.security.Identity;
      
      @Name("myIdentity")
      
      @BypassInterceptors
      
      @Startup
      /**
       * Generamos una clase propia para Identity que permita contar con el atributo extra 
       * para el inicio de sesion 
       * 
       */
      public class MyIdentity extends Identity
      
      {
      
         private static final LogProvider log = Logging.getLogProvider(MyIdentity.class);
      
      
         private String userID;
      
      
         public String getUserID() {
              return userID;
         }
      
      
      
         public void setUserID(String userID) {
              this.userID = userID;
         }
         
      
         @Override
      
         public String login()
      
         {
      
            log.info("###### CUSTOM LOGIN CALLED ######");
      
            return super.login();
      
         }
      
      }



      3- Customizing your Login Form:



      <!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
          "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
           xmlns:s="http://jboss.com/products/seam/taglib"
           xmlns:ui="http://java.sun.com/jsf/facelets"
           xmlns:f="http://java.sun.com/jsf/core"
           xmlns:h="http://java.sun.com/jsf/html"
           xmlns:rich="http://richfaces.org/rich" template="layout/template.xhtml">
      
           <ui:define name="body">
      
                <h:form id="loginForm">
      
                     <rich:panel>
                          <f:facet name="header">Login</f:facet>
      
                          <p>Please login here</p>
      
                          <div class="dialog"><h:panelGrid columns="2" rowClasses="prop"
                               columnClasses="name,value">
                               <h:outputLabel for="username">Username</h:outputLabel>
                               <h:inputText id="username" value="#{myCredentials.username}" />
                               <h:outputLabel for="userID">User ID</h:outputLabel>
                               <h:inputText id="userID" value="#{myCredentials.userID}" />     
                               <h:outputLabel for="password">Password</h:outputLabel>
                               <h:inputSecret id="password" value="#{myCredentials.password}" />
                               <h:outputLabel for="rememberMe">Remember me</h:outputLabel>
                               <h:selectBooleanCheckbox id="rememberMe"
                                    value="#{rememberMe.enabled}" />
                          </h:panelGrid></div>
      
                          <p><i>Note - </i> You may login with your username, your id and your
                          password.</p>
      
                     </rich:panel>
      
                     <div class="actionButtons"><h:commandButton id="submit"
                          value="Login" action="#{identity.login}" /></div>
      
                </h:form>
      
           </ui:define>
      </ui:composition>



      I just add my userID field in the form


      4 Finally modify your authentication method like this:



      package org.domain.authentication.session;
      
      import org.jboss.seam.annotations.In;
      import org.jboss.seam.annotations.Logger;
      import org.jboss.seam.annotations.Name;
      import org.jboss.seam.log.Log;
      import org.jboss.seam.security.credentials.MyCredentials;
      import org.jboss.seam.security.identity.MyIdentity;
      
      @Name("authenticator")
      public class Authenticator
      {
          @Logger private Log log;
      
          @In MyIdentity myIdentity;
          @In MyCredentials myCredentials;
          
          public boolean authenticate()
          {
              log.info("authenticating {0}", myCredentials.getUsername());
              //write your authentication logic here,
              //return true if the authentication was
              //successful, false otherwise
              System.out.println("user:"+myCredentials.getUsername());
              System.out.println("password:"+myCredentials.getPassword());
              System.out.println("ID:"+myCredentials.getUserID());
              if ("admin".equals(myCredentials.getUsername()) && "1".equals(myCredentials.getUserID()) && "123456".equals(myCredentials.getPassword()))
              {
                  myIdentity.addRole("admin");
                  return true;
              }
              return false;
          }
      
      }



      And that's all!


      But there is Still something more to do, in the welcome page I got a message:


      Welcome Null!


      That will be a further exercise :-)


      P.D excuse my English!