繁体   English   中英

Spring 安全性:激活 csrf 保护会破坏其他功能

[英]Spring security: activating csrf protection breaks other functionality

我正在使用 Spring Security 5.0.13,我想为登录页面激活 csrf 保护。 我正在使用 xml 配置,我从

<http>
    ...
    <csrf disabled="true"/>
</http>

<bean id="csrfMatcher" class="org.springframework.security.web.util.matcher.AntPathRequestMatcher">
    <constructor-arg name="pattern" value="/j_spring_security_check"/>
    <constructor-arg name="httpMethod" value="POST"/>
</bean>
<csrf request-matcher-ref="csrfMatcher" />

然而, j_spring_security_logout端点现在需要一个POST请求,而它曾经接受一个GET请求。 我知道对注销按钮有一个POST请求会更好,但我不能破坏这个功能,因为它在我控制之外的其他地方使用。

如何在不影响注销 url 动词的情况下为登录页面激活 csrf 保护?

CSRF 保护要求您为所有导致更改的请求发送包含 CRSF 属性值的隐藏输入。 这就是保护你的东西——这个 CSRF 属性是由你的服务器生成的,因此不能通过从你网站之外的其他地方发送请求来伪造。

您只能通过post请求在 forms 内部发送隐藏的输入,因此如果您想要 csrf 您将需要使用post 好消息是 - 这很容易,大多数模板引擎会自动为您设置一切。

使用 Thymeleaf 您甚至不需要更改任何内容,它会在您的发布请求中自动生成此属性。

使用 Mustache,您需要添加以下属性:

spring.mustache.expose-request-attributes=true

然后使用以下形式:

<form id="logoutForm" method="POST" action="/logout">
    <input type="hidden" name="_csrf" value="{{_csrf.token}}"/>
    <button type=submit>Logout</button>
</form>

如您所见,这确实相对容易,但是您必须将隐藏的 crsf.token 值添加到每个发布请求中,具体取决于您的模板引擎,就像我对 thymeleaf 所说的那样,您不必担心它。

根据Spring 安全参考文档,您可以修改 Spring 安全如何匹配/logout端点。 默认情况下,它会查找POST /logout ,但您可以将其配置为查找GET /logout

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) {
        http
            // ... other configs
            .logout(logout -> logout
                .logoutRequestMatcher(new AntPathRequestMatcher("/logout", "GET"))
            );
    }
}

通过<logout>元素没有直接的 XML 等效项,尽管您可以声明自己的LogoutFilter类型的<bean>注册它 或者记录一张票可能是一种将其添加到<logout>的选项。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM