【问题标题】:Spring Security CSRF Disable Not workingSpring Security CSRF禁用不起作用
【发布时间】:2018-12-12 11:57:12
【问题描述】:

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

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

当我刚刚更改了命名空间并替换了安全 jar 并尝试从登录页面登录时,它为“/j_spring_security_check”给出了 404 错误。

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

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

在这些更改之后,我收到带有 403 状态代码的 CSRF 令牌相关错误-

 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.

我有什么遗漏吗?

【问题讨论】:

标签: java spring spring-security csrf


【解决方案1】:

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

所以早些时候“j_spring_security_check”在那个列表中。但我错过了在该列表中添加“/login”。

在那个应用程序 B 上,CSRF 没有被禁用,因此它导致了问题。

【讨论】:

    猜你喜欢
    • 2020-11-21
    • 2022-12-15
    • 2020-05-09
    • 2016-06-30
    • 2014-04-26
    • 2015-09-27
    • 2021-05-06
    • 2016-07-21
    相关资源
    最近更新 更多