-1

I am using jsecurity check for my login and I want to save the user that logged in and when they did.

How do I get these from j_security_check?

My web xml:

    <login-config>
    <auth-method>FORM</auth-method>
    <realm-name>user-realm</realm-name>
    <form-login-config>
        <form-login-page>/Pages/Login.xhtml</form-login-page>
        <form-error-page>/Pages/LoginError.xhtml</form-error-page>
    </form-login-config>
</login-config>
<security-role>
    <description/>
    <role-name>admin</role-name>
</security-role>
<security-role>
    <description/>
    <role-name>users</role-name>
</security-role>

My login.xhtml page:

        <form role="form" action="j_security_check" method="POST">
    <h2>Please sign in</h2>
    <input name="j_username" type="text" placeholder="Username" required="true"> </input>
    <input name="j_password" type="password" placeholder="Password" required="true"> </input>
    <button type="submit" value="Login">Sign in</button>
  </form>

Just want to know if I can catch the user who logs into my jsf application and save that user and the time they logged in into the database

ollaollu
  • 473
  • 7
  • 19
  • Where is your code? Post your code in the question, see [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – DavidPostill Oct 14 '14 at 11:03

1 Answers1

0

Found an answer on this blog: http://jugojava.blogspot.com/2011/07/jsf-form-authentication-on-servlet-3.html

Replaced j_security_check login page with a login page with a backing bean

Login.xhtml:

        <h:form>  
       <h:panelGrid columns="2">  
            <h:outputLabel for="username" value="Username:" />  
            <h:inputText id="username" value="#{authBean.username}" />  

            <h:outputLabel for="password" value="Password:" />  
            <h:inputSecret id="password" value="#{authBean.password}" />  

            <h:commandButton id="loginButton" value="Login" action="#{authBean.login()}" />  
       </h:panelGrid>
    </h:form>

and my backing bean:

    private String username;
    private String password;

public String login(){
       FacesContext context = FacesContext.getCurrentInstance();  
       HttpServletRequest request = (HttpServletRequest) context  
                                          .getExternalContext().getRequest();  

       try {  
            request.login(username, password);  
       } catch (ServletException e) {  
            context.addMessage(null, new FacesMessage(FacesMessage.SEVERITY_WARN, "Login failed!", null));  
            return "login";  
       }  

       //database action to be done here  
       Principal principal = request.getUserPrincipal();    


       if(request.isUserInRole("admin")) {  
            return "/Pages/AdminLanding.xhtml";  
       } else {  
            return "/Pages/UserLanding.xhtml";  
       }  
  } //getter and setter for username and password
ollaollu
  • 473
  • 7
  • 19