HelloWorld HelloWorld - 2 months ago 15
Java Question

unable to display same page with error message in spring mvc

the idea is simple If my login fails user will be redirected to

/loginerror
but is not working as excepted

LoginController

@RequestMapping(value = "/login", method = RequestMethod.GET)
public String login(Model model) {
return "login";
}

@RequestMapping(value = "/loginerror", method = RequestMethod.GET)
public String loginFailed(Model model) {
model.addAttribute("error", "true");
return "login";
}

@RequestMapping(value = "/logout", method = RequestMethod.GET)
public String logout(Model model) {
return "login";
}


login.jsp

<c:if test="${not empty error}">
<div class="alert alert-danger">
<spring:message
code="AbstractUserDetailsAuthenticationProvider.badCredentials" />
<br />
</div>
</c:if>
<form action="<c:url value= "/j_spring_security_check"></c:url>"
method="post">
<fieldset>
<div class="form-group">
<input class="form-control" placeholder="User Name"
name='j_username' type="text">
</div>
<div class="form-group">
<input class="form-control" placeholder="Password"
name='j_password' type="password" value="">
</div>
<input class="btn btn-lg btn-success btn-block" type="submit"
value="Login">
</fieldset>
</form>
</div>


security-context.xml

<security:http auto-config="true">
<security:intercept-url pattern="/all/add"
access="hasRole('ROLE_ADMIN')" />
<security:form-login login-page="/login"
default-target-url="/all/add" authentication-failure-url="/loginerror" />
<security:logout logout-success-url="/logout" />
<security:csrf disabled="true" />
</security:http>

<security:authentication-manager>
<security:authentication-provider>
<security:user-service>
<security:user name="admin" authorities="ROLE_ADMIN"
password="admin" />
</security:user-service>
</security:authentication-provider>

</security:authentication-manager>


what I am trying to do is to redirect the user to login page when its login fails but everytime I do I am getting error 404 page not found on url
localhost......../j_spring_security_check


Edit

spring error message is
AbstractUserDetailsAuthenticationProvider.badCredentials=The user-name or password you entered is incorrect.


after putting debug statement inside
/loginerror
method I came to know that this method is not even being invoked


how to intercept this if url is wrong?

web.xml

<servlet>
<servlet-name>dispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcherServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/security-context.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

Answer

the version of spring you are using does not support <form action="<c:url value= "/j_spring_security_check"></c:url>" method="post"> use /login as url instead <form action="<c:url value= "/login"></c:url>" method="post"> since you asked this link clearly explains the differences Why doesn't my custom login page show with Spring Security 4?

Comments