4 Replies Latest reply on Jan 5, 2009 1:32 PM by Susanne Jarl

    seam user session authentication

    safa said Newbie

      Hi,
      I'm naw in seam and i want to manage the user session authentication.
      I'm wondring if there is some plugin for seam to do this.
      Or should implement this by my self and create the table in the database????
      I'm using seam 2.0.3 GA, postgrsql database and eclipse europa
      I'm really frustrated and I don't know how to do this!!!
      thx

        • 1. Re: seam user session authentication
          Dylan Janeke Newbie

          Hi, this is what I did :


          Created the user entity :


          @Entity
          @Name("user")
          @Table(name = "usr")
          @Scope(ScopeType.EVENT)
          public class User_E implements Serializable{
              @Id
              @GeneratedValue
              @Column(name = "uid")
              private int uid;
          
              @NotNull
              @Column(name = "usrname")
              @Length(min = 5, max = 15)
              private String username;
          
              @NotNull
              @Length(min = 5, max = 15)
              @Column(name = "passwd")
              private String password;
          
              @Temporal(TemporalType.DATE)
              @Column(name = "last_login")
              private Date lastLogin;
          
              @NotNull
              @Length(min = 3, max = 15)
              @Column(name = "full_name")
              private String fullname;
          
              @NotNull
              @Column(name = "email_address")
              @Email
              private String email;
          
              @NotNull
              @Column(name = "notify_stats")
              private Boolean notifyStats;
          
          



          That will create the table for you.
          and use this session bean



          @Stateful
          @Scope(ScopeType.EVENT)
          @Name("authenticator")
          public class Authenticator implements AuthenticatorLocal
          {
              @Logger Log log;
              @In Credentials credentials;
          
              @PersistenceContext
              EntityManager em;
          
              public boolean authenticate()
              {
                  log.info("authenticating {0}", credentials.getUsername());
                  User_E user = fetchUser();
                  if (user == null){
                      log.info("No user like {0}", credentials.getUsername());
                      return false;
                  }
          
                  if (user.getPassword().equals(credentials.getPassword())){
                      log.info("successfully authenticated {0}", credentials.getUsername());
                      log.info("setting last login date...");
                      user.setLastLogin(new Date());
                      em.persist(user);
                      return true;
                  }
                  return false;
              }
              
              public User_E fetchUser() {
                  Query query = em.createQuery("from User_E ue where ue.username = :username");
                  query.setParameter("username", credentials.getUsername());
          
                  List<User_E> lst = query.getResultList();
          
                  if(lst != null ||
                          lst.size() >= 1){
                      return lst.get(0);
                  }
                  return null;
              }
          }
          


          Then in your view use this for login screen :


              <h:form id="login">
          
                  <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="#{credentials.username}"/>
                              <h:outputLabel for="password">Password</h:outputLabel>
                              <h:inputSecret id="password"
                                          value="#{credentials.password}"/>
                              <h:outputLabel for="rememberMe">Remember me</h:outputLabel>
                              <h:selectBooleanCheckbox id="rememberMe"
                                                    value="#{identity.rememberMe}"/>
                          </h:panelGrid>
                      </div>
                  </rich:panel>
          
                  <div class="actionButtons">
                      <h:commandButton value="Login" action="#{identity.login}"/>
                  </div>
          
              </h:form>
          



          Hope that helps...

          • 2. Re: seam user session authentication
            safa said Newbie

            Thx Dylan for your help,
            So there is no plugin to help me to implementthe session authenticator,!!!
            I tried your suggession but it didn't work.
            I'll try some others things,
            Thx for your help

            • 3. Re: seam user session authentication
              Dylan Janeke Newbie

              No problems, by u saying it doens't work what errors are u getting etc. Please give more info and I will try to assist, I am not sure of any plugins though..

              • 4. Re: seam user session authentication
                Susanne Jarl Newbie

                If you install the JBoss Tools plugin to eclipse the Seam Gen will create the authenticator for you.


                Also look at the examples that comes with the seam download, seamspace for example.