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

    Customizing your login!

    Leon Suarez Newbie

      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;
      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;
       * 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;
         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"
      <ui:composition xmlns="http://www.w3.org/1999/xhtml"
           xmlns:rich="http://richfaces.org/rich" template="layout/template.xhtml">
           <ui:define name="body">
                <h:form id="loginForm">
                          <f:facet name="header">Login</f:facet>
                          <p>Please login here</p>
                          <div class="dialog"><h:panelGrid columns="2" rowClasses="prop"
                               <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}" />
                          <p><i>Note - </i> You may login with your username, your id and your
                     <div class="actionButtons"><h:commandButton id="submit"
                          value="Login" action="#{identity.login}" /></div>

      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;
      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
              if ("admin".equals(myCredentials.getUsername()) && "1".equals(myCredentials.getUserID()) && "123456".equals(myCredentials.getPassword()))
                  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!