I have developed a Spring application and I implement Spring security integration to Login and Logout function in it. I used Spring security with xml configuration. But when I login to system it shows 404 for me. Console telling me, No mapping found for HTTP request with URI [/pms/j_spring_security_check] in DispatcherServlet with name 'appServlet'
But I can not understand the error. What I have missed?
My web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<!-- The definition of the Root Spring Container shared by all Servlets
and Filters -->
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- Processes application requests -->
<servlet>
<servlet-name>appServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>appServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring/appServlet/spring-security.xml</param-value>
</context-param>
<!-- Spring Security -->
<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>
</web-app>
My Controller method :
@RequestMapping(value="/login", method = RequestMethod.GET)
public ModelAndView printWelcome() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.addObject("message", "Spring security allows you");
modelAndView.setViewName("loginForm");
return modelAndView;
}
My spring-security.xml :
<beans:beans xmlns="http://www.springframework.org/schema/security"
xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.1.xsd">
<http auto-config="true">
<intercept-url pattern="/loginForm"/>
<intercept-url pattern="/" access="ROLE_USER" />
<form-login login-page="/loginForm"
default-target-url="/login" always-use-default-target="true"
authentication-failure-url="/loginForm?login_error=1" />
<logout logout-success-url="/loginForm" />
</http>
<authentication-manager>
<authentication-provider>
<user-service>
<user name="a2ztechguide" password="123456" authorities="ROLE_USER" />
</user-service>
</authentication-provider>
</authentication-manager>
</beans:beans>
And of my Login page :
<body>
<table>
<tr>
<td valign="top"><c:if test="${not empty param.login_error}">
<font color="red"> Invalid user name or password, try again.
<br /> <br />
</font>
</c:if>
<form name="login_form"
action="<c:url value='j_spring_security_check'/>" method="POST">
<div>
<table width="40%" border="0" cellpadding="0" cellspacing="0">
<tr>
<td valign="top">
<table border="0" cellspacing="0" cellpadding="4" width="40%">
<tr>
<td colspan="2">Custom Login Form
<hr width="100%" size="1" noshade align="left">
</td>
<td></td>
</tr>
<tr>
<td width="80">Username</td>
<td valign="top" align="left"><input type='text'
id='username' size="30" maxlength="40" name='j_username'
value='<c:if test="${not empty param.login_error}">
<c:out value="${SPRING_SECURITY_LAST_USERNAME}"/>
</c:if>' />
</td>
</tr>
<tr>
<td width="80">Password</td>
<td valign="top" align="left"><input type='password'
name='j_password' size="30" maxlength="30"></td>
</tr>
<tr>
<td></td>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</td>
</tr>
</table>
</div>
</form></td>
</tr>
</table>
</body>
Please help me.
I finally solved this by modifying the line where I was declaring my custom login form page.
Added processing-url="/j_spring_security_check":
<form-login login-page="/login" default-target-url="/admin" login-processing-url="/j_spring_security_check" authentication-failure-url="/login?error" username-parameter="username" password-parameter="password"/>
to my security-context.xml page
Below code solved my problem for annotation based security configuration. http.formLogin().loginProcessingUrl("/j_spring_security_check")...other settings
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With