I have integrated Spring Security in my application , and would like to display an error message to the user in case of Bad credentials.
I have wired everything in to display the message , but it does not work. Please find the code snippets below
spring-security.xml
<form-login
login-page="/spring/login"
authentication-failure-url="/spring/login?error=true"
default-target-url="/spring/index"/>
<authentication-manager>
<authentication-provider user-service-ref="customService">
</authentication-provider>
</authentication-manager>
CustomService implements UserDetailsService , and in the loadUserByUsername method , i am returning the Spring UserDetails object which contains the details of the user.
My Controller looks as follows
@RequestMapping(value="/login", method = RequestMethod.POST)
public String getLogin(@RequestParam(value="error", required=false) boolean error,
ModelMap model){
if (error == true) {
// Assign an error message
model.put("error", "You have entered an invalid username or password!");
} else {
model.put("error", "");
}
return "loginpage";
}
My loginpage contains the following div to paint the error
<div id="login-error">
<c:if test="${not empty param.error}">
Your login attempt was not successful, try again.<br />
</c:if>
</div>
When I enter wrong credentials , it is redirected to the login page, but the message is not being displayed.
Please help and correct me if I am doing anything wrong.
Issue Resolved.
The real reason for the error message not being painted was that the
${not empty param.error}was not being evaluated.To fix this issue , modified my web.xml to conform to the Servlet 2.5 Spec. and it worked like a charm. 🙂
Please refer my post on changing the Servlet Spec to v2.5
Issue with evaluating ${error} in Spring
Hope this helps all those who might face this issue.