gpt4 book ai didi

java - Spring j_spring_security_check 始终返回 404

转载 作者:行者123 更新时间:2023-12-01 11:07:17 25 4
gpt4 key购买 nike

我尝试使用 Spring 制作一个 Web 应用程序,并且有一个页面可以验证用户身份并识别用户的角色。但是登录后,我总是收到 404,然后我查看日志,AuthenticationFilter 甚至无法识别用户角色。请帮助我..我花了几天时间但仍然没有预期的结果。谢谢。

这是我的配置和代码。

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>

安全上下文.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>

我的 Controller 类

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

我在 Tomcat 日志中收到的消息

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.

我怀疑这可能是身份验证管理器的问题,因为它甚至在单击登录按钮后无法识别用户角色......或者登录页面路径不应与调度程序 servlet url 模式混合?非常感谢

最佳答案

谢谢 M. Deinum

我发现/j_spring_security_check 、 j_username 和 j_password 在 4.0.2.RELEASE 版本中已被弃用。

现在我将 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"%>

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

关于java - Spring j_spring_security_check 始终返回 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32810828/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com