18.5.3 Logging Out

Adding CSRF will update the LogoutFilter to only use HTTP POST. This ensures that log out requires a CSRF token and that a malicious user cannot forcibly log out your users.

One approach is to use a form for log out. If you really want a link, you can use JavaScript to have the link perform a POST (i.e. maybe on a hidden form). For browsers with JavaScript that is disabled, you can optionally have the link take the user to a log out confirmation page that will perform the POST.

If you really want to use HTTP GET with logout you can do so, but remember this is generally not recommended. For example, the following Java Configuration will perform logout with the URL /logout is requested with any HTTP method:

@EnableWebSecurity
public class WebSecurityConfig extends
WebSecurityConfigurerAdapter { @Override
protected void configure(HttpSecurity http) throws Exception {
http
.logout()
.logoutRequestMatcher(new AntPathRequestMatcher("/logout"));
}
}

http://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#csrf-logout

34down voteaccepted

From the Spring Security documentation

CSRF protection is enabled by default with Java configuration. If you would like to disable CSRF, the corresponding Java configuration can be seen below. Refer to the Javadoc of csrf() for additional customizations in how CSRF protection is configured.

And, when CSRF protection is enabled

The last step is to ensure that you include the CSRF token in all PATCH, POST, PUT, and DELETE methods.

In your case:

  • you have CSRF protection enabled by default (because you are using Java configuration),
  • you are submitting the login form using an HTTP POST and
  • are not including the CSRF token in the login form. For this reason, your login request is denied upon submission because the CSRF protection filter cannot find the CSRF token in the incoming request.

You have already determined the possible solutions:

  1. Disable CSRF protection as http.csrf().disable(); or
  2. Include the CSRF token in the login form as a hidden parameter.

Since you are using Thymeleaf, you will have to do something like the following in your HTML template for the login page:

<form name="f" th:action="@{/login}" method="post">
<fieldset> <input type="hidden"
th:name="${_csrf.parameterName}"
th:value="${_csrf.token}" /> ...
</fieldset>
</form>

Note that you must use th:action and not HTML action as the Thymeleaf CSRF processor will kick-in only with the former.

You could change the form submission method to GET just to get over the problem but that isn't recommended since the users are going to submit sensitive information in the form.

I typically create a Thymeleaf fragment that is then used in all pages with forms to generate the markup for the forms with the CSRF token included. This reduces boilerplate code across the app.

https://stackoverflow.com/questions/25692735/simple-example-of-spring-security-with-thymeleaf

最新文章

  1. 结构体内嵌函数指针实现C语言面向对象
  2. 在Oracle中恢复被DROP掉的表
  3. Python开发【前端】:CSS
  4. ASP.NET控件&lt;ASP:Button /&gt; html控件&lt;input type=&quot;button&quot;&gt;区别联系
  5. Auty自动化测试框架第六篇——垃圾代码回收、添加suite支持
  6. gcc/g++动态链接库和静态库的链接顺序
  7. linq 日常关键字使用
  8. chrome浏览器无法设置打开特定网页
  9. Android 自定义组件随着手指自动画圆
  10. 解决Hibernate中不同包内有形同实体导致映射失败的问题
  11. openwrt看IP流量
  12. C# mongodb 1
  13. Spring总结 0.概述
  14. Linux下自动化监控内存、存储空间!
  15. C++标准模板库(STL)之Queue
  16. redis sentinel 读写分离
  17. mybatis检测mysql表是否存在
  18. centos7安装单机rocketmq,图文教程
  19. BOM简单总结
  20. CSS| 實例---寬度自由調節button,圖片切換

热门文章

  1. SpriteBuilder中的CCB Node尺寸
  2. Android开发技巧——使用Dialog实现仿QQ的ActionSheet菜单
  3. Erlang cowboy websocket 服务器
  4. How tomcat works 读书笔记十七 启动tomcat 下
  5. HTML中的javascript交互
  6. Python 3.7 将引入 dataclass 装饰器
  7. kubernetes-dashboard(1.8.3)部署与踩坑
  8. 听《津津乐道》ThinkPad专题节目有感
  9. Validate Binary Search Tree(一定掌握的方法)
  10. Jquery的过滤选择器分为哪几种?