【发布时间】:2019-11-25 03:56:45
【问题描述】:
使用了 CSRF 保护,因此来自其他网站的任何请求都不会影响我的网站造成伤害。 spring security csrf文档中说csrf是用于put post patch delete请求的。
但据我了解,登录/注册表单不需要 csrf 保护,因为它们已经需要用户名和密码形式的凭据,即使这样的请求是从另一个网站发出的,也不会有任何危害,因为用户将刚刚登录。
但是由于登录通常是一个post请求,所以spring默认会在这里自动应用csrf。这意味着我需要将 csrf 令牌生成参数作为隐藏输入字段添加到我的表单中,如下所示:
<form th:action="@{/login}" method="post">
<fieldset>
<input type="hidden"
th:name="${_csrf.parameterName}"
th:value="${_csrf.token}" />
</fieldset>
...
</form>
如果我不添加这个,就会出现 403 Forbidden 错误。但是如果我像这样禁用这个 csrf ..:
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
}
然后网站中的所有页面都失去了csrf保护。如何将 csrf 应用于某些页面而不是其他页面,即使他们正在发出发布请求?
我正在使用 Spring Boot + Spring Security + thymeleaf
【问题讨论】:
标签: spring spring-boot spring-security thymeleaf csrf