我有一个使用Spring Security进行身份验证的应用程序。之前我们使用的是Spring Security 3.2.10,现在尝试升级到4.2.6。
从春季4开始,CSRF保护默认为启用。

我正在遵循迁移指南-https://docs.spring.io/spring-security/site/migrate/current/3-to-4/html5/migrate-3-to-4-xml.html

当我只是更改名称空间并替换了安全罐并尝试从登录页面登录时,它给“ / j_spring_security_check”提供了404错误。

根据《指南》,我将表单登录操作更改为“ / login”而不是“ / j_spring_security_check”。还将用户名和密码参数更改为“用户名”和“密码”,而不是登录页面中定义的“ j_username”和“ j_password”。

 <form action="/login" method="POST">


经过这些更改后,我得到了CSRF令牌相关错误以及403状态代码-

 Type Status Report
 Message Could not verify the provided CSRF token because your session was
 not found.
 Description The server understood the request but refuses to authorize it.


我通过在spring安全配置文件中添加以下内容来禁用CSRF保护-

  <security:http disable-url-rewriting="true" use-expressions="true">
    <security:csrf disabled="true"/>
    ..
    <security:intercept-url pattern="/login.jsp"
    access="hasRole('ROLE_ANONYMOUS')"/>
    <security:form-login login-page="/login.jsp"
                         authentication-failure-url="/login.jsp?
                         login_error=1"
                         default-target-url="/index.html" authentication-
             success-handler-ref="successHandler" always-use-default-
    target="true"/>
    <security:logout logout-success-url="/login.jsp"/>


但这仍然无法正常工作,并给我CSRF相关的错误(状态为403)-

 Type Status Report
 Message Could not verify the provided CSRF token because your session was
 not found.
 Description The server understood the request but refuses to authorize it.


我有什么想念的吗?

最佳答案

我终于找到了问题。实际上,在我们的应用程序A中,我们有一个可以由该应用程序B处理的URI列表,否则它将请求转发给另一个应用程序。

因此,先前的“ j_spring_security_check”位于该列表中。但是我错过了在该列表中添加“ / login”的信息。

在该应用程序B上,未禁用CSRF,因此导致了该问题。

07-26 09:30
查看更多