0
votes

Im trying to create a login page using Spring Security, but the username and password are not updating properly after validation. If I set the property in the Bean it appears when page loads, but if I set the fields in the xhtml and then I submit it, I cannot get the updated values.

Here is the code:

login.xhtml:

<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="./WEB-INF/templates/template.xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui"
                xmlns:c="http://java.sun.com/jsp/jstl/core"
                xmlns:ezcomp="http://java.sun.com/jsf/composite/ezcomp">

<ui:define name="contenido">

<div class="mainContent">    

<h:form id="formLogin" prependId="false">
       <div class="form">
       <p><p:message for="formLogin" /></p>
       <p>
          <label>User Name <span>(Required Field)</span></label>
          <h:inputText id="j_username" label="User Name" value="#{loginMB.userName}"  required="true" />
          <label><p:message  for="j_username" /></label>
       </p>
       <p>
          <label>Password <span>(Required Field)</span></label>
          <h:inputSecret id="j_password" label="Password" value="#{loginMB.password}" required="true" />
          <label><p:message  for="j_password" /></label>
       </p>

  </div>
    <div class="buttons">
    <h:commandButton id="login" actionListener="#{loginMB.login}" value="Login" icon="ui-icon-person" />
  </div>
  <h:inputHidden value="#{loginMB.logoutHidden}" />
</h:form>
</ui:define>
</ui:composition>

LoginMB.java

@ManagedBean(name="loginMB")
@SessionScoped
@Component
public class LoginMB  implements Serializable {

    private static final long serialVersionUID = 1L;

    @NotEmpty
    @Size(min = 1, max = 25)
    private String userName;

    @NotEmpty
    @Size(min = 1, max = 25)
    private String password;


    @ManagedProperty(value="#{authenticationManager}")
    private AuthenticationManager authenticationManager;

    public AuthenticationManager getAuthenticationManager() {
        return authenticationManager;
    }

    public void setAuthenticationManager(AuthenticationManager authenticationManager) {
        this.authenticationManager = authenticationManager;
    }

    public LoginMB() {}

    public String login() throws java.io.IOException {
            try {
                Authentication request = new UsernamePasswordAuthenticationToken(userName, password);
                Authentication result = authenticationManager.authenticate(request);
                SecurityContextHolder.getContext().setAuthentication(result);
                System.out.println("Login Success! ..");
                return "/admin/index.html";
        } catch (AuthenticationException ex) {
                System.out.println("Login Failed");
                FacesContext.getCurrentInstance().addMessage("formLogin", new FacesMessage(FacesMessage.SEVERITY_WARN,"Login Failed", "User Name and Password Not Match!"));           
            return "/login";
        }
    }

    public String logout() {
            SecurityContextHolder.getContext().setAuthentication(null);
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
                            .clear();
            return "/login";
    }

    public String getLogoutHidden() {
            SecurityContextHolder.getContext().setAuthentication(null);
            FacesContext.getCurrentInstance().getExternalContext().getSessionMap()
                            .clear();
            return "logout";
    }

    public void setLogoutHidden(String logoutHidden) {
    }


    public String getUserName() {
            return userName;
    }

    public void setUserName(String userName) {
            this.userName = userName;
    }

    public String getPassword() {
            return password;
    }

    public void setPassword(String password) {
            this.password = password;
    }
3
What do you mean by "updating after validation"? The inputs are cleared after failing validation?kolossus
Yes, I text something in the imputText, and when I validate the field is empty or with the default value. I cannot retrieve what I texted into the field.andoni90

3 Answers

1
votes

Try to use action instead of actionListener attribute. I expect your method is not triggered from the view, when you are clicking on the button. This is because of:

An action listener method must be a public method with an ActionEvent parameter and a void return type.

See here h:commandButton reference

0
votes

it seems like the process of rerendering the matching form fails :

have you tried to add update attribute to commandButton, and the value should match the form id.

<h:commandButton id="login" update ="formLogin" actionListener="#{loginMB.login}" value="Login" icon="ui-icon-person" />

hope it solves the problem.

0
votes

Finally I found the mistake in spring configuration file, I put scope twice:

<bean id="LoginMB" name="LoginMB" class="com....LoginMB" scope="prototype">
  <property name="authenticationManager" ref="authenticationManager"></property>
</bean>

And corrected it:

<bean id="LoginMB" name="LoginMB" class="com.....LoginMB">
  <property name="authenticationManager" ref="authenticationManager"></property>
</bean>