OsYYYY
OsYYYY

Reputation: 253

Always return 404 for Spring j_spring_security_check

I try to make a web application using spring and there is a page to authenticate user and identify user's role. But after I log in, I always get 404 and I look back my log, the AuthenticationFilter could not even identify the user role. Please help me.. I spent several days on it but still no expected result.Thanks.

Here is my configuration and code.

web.xml

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


<servlet>
    <servlet-name>user-dispatcher</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>user-dispatcher</servlet-name>
    <url-pattern>/user/*</url-pattern>
</servlet-mapping>

security-context.xml

<http auto-config='true'>
    <intercept-url pattern="/user/operation/Healthcheck"
        access="ROLE_USER" />
    <form-login login-page="/" default-target-url="/"
        authentication-failure-url="/?login=error" />
    <logout logout-success-url="/" />
</http>

<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="tester" password="test" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

My controller class

@Controller
@RequestMapping("/operation")
public class UserOperationController {

        @RequestMapping("")
        public ModelAndView home() {
            return new ModelAndView("index");
        }


        @RequestMapping("/Healthcheck")
        public ModelAndView healthCheck() {

            ....Some Operation....

            return new ModelAndView("healthcheck", "result", "positive");
        }
}

index.jsp

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>

<c:set var="base" value="${pageContext.request.contextPath }/user/operation/" scope="session"/>
<sec:authentication property="principal" var="auth" scope="session" />

<html>
<body>
<h2>Hello World!</h2>
<h2>${auth }</h2>
<form action="${base }j_spring_security_check" method="post">
    Username:<input type="text" name="j_username" /><br/>
    Password:<input type="password" name="j_password" /><br/>
    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> <br/>
    <input type="submit" value="Login" />
</form>
<a href="${base }j_spring_security_logout">Logout</a>
</body>
</html>

The message I got in Tomcat log

2015-09-28 01:15:57 DEBUG AntPathRequestMatcher:151 - Checking match of request
: '/user/operation/j_spring_security_check'; against '/login'
2015-09-28 01:15:57 DEBUG FilterChainProxy:324 - /user/operation/j_spring_securi
ty_check at position 7 of 13 in additional filter chain; firing Filter: 'BasicAu
thenticationFilter'
2015-09-28 01:15:57 DEBUG FilterChainProxy:324 - /user/operation/j_spring_securi
ty_check at position 8 of 13 in additional filter chain; firing Filter: 'Request
CacheAwareFilter'
2015-09-28 01:15:57 DEBUG FilterChainProxy:324 - /user/operation/j_spring_securi
ty_check at position 9 of 13 in additional filter chain; firing Filter: 'Securit
yContextHolderAwareRequestFilter'
2015-09-28 01:15:57 DEBUG FilterChainProxy:324 - /user/operation/j_spring_securi
ty_check at position 10 of 13 in additional filter chain; firing Filter: 'Anonym
ousAuthenticationFilter'
2015-09-28 01:15:57 DEBUG AnonymousAuthenticationFilter:100 - Populated Security
ContextHolder with anonymous token: 'org.springframework.security.authentication
.AnonymousAuthenticationToken@6faa1b5a: Principal: anonymousUser; Credentials: [
PROTECTED]; Authenticated: true; Details: org.springframework.security.web.authe
ntication.WebAuthenticationDetails@ffff6a82: RemoteIpAddress: 127.0.0.1; Session
Id: 8DBBBE56C5021B1DC6DC04236AFD7569; Granted Authorities: ROLE_ANONYMOUS'
2015-09-28 01:15:57 DEBUG FilterChainProxy:324 - /user/operation/j_spring_securi
ty_check at position 11 of 13 in additional filter chain; firing Filter: 'Sessio
nManagementFilter'
2015-09-28 01:15:57 DEBUG FilterChainProxy:324 - /user/operation/j_spring_securi
ty_check at position 12 of 13 in additional filter chain; firing Filter: 'Except
ionTranslationFilter'
2015-09-28 01:15:57 DEBUG FilterChainProxy:324 - /user/operation/j_spring_securi
ty_check at position 13 of 13 in additional filter chain; firing Filter: 'Filter
SecurityInterceptor'
2015-09-28 01:15:57 DEBUG AntPathRequestMatcher:151 - Checking match of request
: '/user/operation/j_spring_security_check'; against '/user/operation/healthchec
k'
2015-09-28 01:15:57 DEBUG FilterSecurityInterceptor:209 - Public object - authen
tication not attempted
2015-09-28 01:15:57 DEBUG FilterChainProxy:309 - /user/operation/j_spring_securi
ty_check reached end of additional filter chain; proceeding with original chain
2015-09-28 01:15:57 DEBUG DispatcherServlet:861 - DispatcherServlet with name 'u
ser-dispatcher' processing POST request for [/UsquareAppSource/user/operation/j_
spring_security_check]
2015-09-28 01:15:57 DEBUG RequestMappingHandlerMapping:319 - Looking up handler
method for path /operation/j_spring_security_check
2015-09-28 01:15:57 DEBUG RequestMappingHandlerMapping:329 - Did not find handle
r method for [/operation/j_spring_security_check]
2015-09-28 01:15:57 WARN  PageNotFound:1136 - No mapping found for HTTP request
with URI [/UsquareAppSource/user/operation/j_spring_security_check] in Dispatche
rServlet with name 'user-dispatcher'
2015-09-28 01:15:57 DEBUG HttpSessionSecurityContextRepository:337 - SecurityCon
text is empty or contents are anonymous - context will not be stored in HttpSess
ion.

I doubt that it may be authentication-manager problem because it even cannot identify user role after clicking the login button.... Or the login page path should not be mixed with dispatcher-servlet url pattern? Thanks a lot

Upvotes: 0

Views: 1793

Answers (1)

OsYYYY
OsYYYY

Reputation: 253

Thanks M. Deinum

I find that /j_spring_security_check , j_username and j_password have been deprecated in 4.0.2.RELEASE version.

Now I change my jsp to the following and it works now.

<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="sec" uri="http://www.springframework.org/security/tags"%>

<sec:authentication property="principal" var="auth" scope="session" />

<html>
<body>
<h2>Hello World!</h2>
<h2>${auth }</h2>
<form action="<c:url value='/login' />" method="POST">
    Username:<input type="text" name="username" /><br/>
    Password:<input type="password" name="password" /><br/>
    <input type="hidden" name="${_csrf.parameterName}" value="${_csrf.token}"/> <br/>
    <input type="submit" value="Login" />
</form>
</body>
</html>

Upvotes: 1

Related Questions