gpt4 book ai didi

在没有尾部斜杠的情况下访问 Tomcat 应用程序时,Spring Security Context 为空

转载 作者:行者123 更新时间:2023-11-28 21:59:42 24 4
gpt4 key购买 nike

我在 Tomcat 7 上部署了一个基于 Spring 的 Web 应用程序。要运行它,我要做的是将 application.war 存档复制到 webapps 目录并启动服务器,这样我的应用程序根目录就可用了在 https://localhost:8443/application/ 下。

然后我想给它添加 Spring security (3.2.4)。我的意图是保护整个应用程序,而不仅仅是它的一部分。我拥有的 Spring 安全配置:

<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.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.2.xsd">

<http auto-config="true">
<intercept-url pattern="/**" access="ROLE_USER" />
</http>

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

</beans:beans>

web.xml 中:

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

这是一个基本配置,可以在官方文档和许多教程中找到。一切正常,当我未通过身份验证时,Spring 会将我重定向到它的默认登录页面。然后我可以登录并访问应用程序,直到 session 过期或我导航到注销 url。

当我访问没有结尾斜杠的应用程序根目录时,问题开始出现:https://localhost:8443/application

Spring 找不到安全上下文:

2016-07-19 17:48:36,650 DEBUG: security.web.FilterChainProxy - / at position 1 of 12 in additional filter chain; firing Filter: 'SecurityContextPersistenceFilter'
2016-07-19 17:48:36,650 DEBUG: web.context.HttpSessionSecurityContextRepository - HttpSession returned null object for SPRING_SECURITY_CONTEXT
2016-07-19 17:48:36,650 DEBUG: web.context.HttpSessionSecurityContextRepository - No SecurityContext was available from the HttpSession: org.apache.catalina.session.StandardSessionFacade@53b1e801. A new one will be created.

然后我被重定向到登录页面,但在我登录后我被重定向到没有斜杠的原始 URL,Spring 再次失败,我再次看到登录表单。

有人知道为什么会这样吗?我的配置是最小的,使用开箱即用的默认值。用例也很简单,感觉很奇怪,这没有按预期工作。我觉得我缺少一些基本的东西,无论是 Spring 还是 Tomcat。

任何帮助将不胜感激,谢谢。

最佳答案

问题出在 JSESSIONID cookie 路径上——Tomcat 自动将其设置为 /application/,因此任何访问应用程序根目录且没有尾部斜杠的请求都被视为需要身份验证的新用户。将 cookie 路径设置为 /application 解决了我的问题。

关于在没有尾部斜杠的情况下访问 Tomcat 应用程序时,Spring Security Context 为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38463528/

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