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;
}