Spring Security笔记:解决CsrfFilter与Rest服务Post方式的矛盾
基于Spring Security+Spring MVC的web应用,为了防止跨站提交攻击,通常会配置csrf,即:
1 <http ...> 2 ... 3 <csrf /> 4 </http>
如果应用中有Post方式访问的Rest服务(参考下面的代码),会很不幸的发现,所有POST方式请求的服务会调用失败。
![](/assets/blank.gif)
1 @RequestMapping(value = "/user/create", method = RequestMethod.POST) 2 @ResponseBody 3 public UserInfo createUser(@RequestBody(required = true) UserInfo user, 4 HttpServletRequest request, HttpServletResponse response) 5 throws Exception { 6 ... 7 }
![](/assets/blank.gif)
原因在于:启用csrf后,所有http请求都被会CsrfFilter拦截,而CsrfFilter中有一个私有类DefaultRequiresCsrfMatcher
![](/assets/blank.gif)
1 private static final class DefaultRequiresCsrfMatcher implements RequestMatcher {2 private Pattern allowedMethods = Pattern.compile("^(GET|HEAD|TRACE|OPTIONS)$");3 4 /* (non-Javadoc)5 * @see org.springframework.security.web.util.matcher.RequestMatcher#matches(javax.servlet.http.HttpServletRequest)6 */7 public boolean matches(HttpServletRequest request) {8 return !allowedMethods.matcher(request.getMethod()).matches();9 } 10 }
![](/assets/blank.gif)
从这段源码可以发现,POST方法被排除在外了,也就是说只有GET|HEAD|TRACE|OPTIONS这4类方法会被放行,其它Method的http请求,都要验证_csrf的token是否正确,而通常post方式调用rest服务时,又没有_csrf的token,所以校验失败。
解决方法:自己弄一个Matcher
![](/assets/blank.gif)
1 package com.cnblogs.yjmyzz.utils;2 3 import java.util.List;4 import java.util.regex.Pattern;5 6 import javax.servlet.http.HttpServletRequest;7 8 import org.springframework.security.web.util.matcher.RequestMatcher;9 10 public class CsrfSecurityRequestMatcher implements RequestMatcher { 11 private Pattern allowedMethods = Pattern 12 .compile("^(GET|HEAD|TRACE|OPTIONS)$"); 13 14 public boolean matches(HttpServletRequest request) { 15 16 if (execludeUrls != null && execludeUrls.size() > 0) { 17 String servletPath = request.getServletPath(); 18 for (String url : execludeUrls) { 19 if (servletPath.contains(url)) { 20 return false; 21 } 22 } 23 } 24 return !allowedMethods.matcher(request.getMethod()).matches(); 25 } 26 27 /** 28 * 需要排除的url列表 29 */ 30 private List<String> execludeUrls; 31 32 public List<String> getExecludeUrls() { 33 return execludeUrls; 34 } 35 36 public void setExecludeUrls(List<String> execludeUrls) { 37 this.execludeUrls = execludeUrls; 38 } 39 }
![](/assets/blank.gif)
这里添加了一个属性execludeUrls,允许人为排除哪些url。
然后在配置文件里,这样修改:
![](/assets/blank.gif)
1 <http entry-point-ref="loginEntryPoint" use-expressions="true">2 ...3 <intercept-url pattern="/rest/**" access="permitAll" />4 ...5 <csrf request-matcher-ref="csrfSecurityRequestMatcher"/> 6 </http>7 8 <beans:bean id="csrfSecurityRequestMatcher" class="com.cnblogs.yjmyzz.utils.CsrfSecurityRequestMatcher">9 <beans:property name="execludeUrls"> 10 <beans:list> 11 <beans:value>/rest/</beans:value> 12 </beans:list> 13 </beans:property> 14 </beans:bean>
![](/assets/blank.gif)
这里约定所有/rest/开头的都是Rest服务地址,上面的配置就把/rest/排除在csrf验证的范围之外了.
https://blog.csdn.net/cwfreebird/article/details/50433188
转载于:https://www.cnblogs.com/softidea/p/10316841.html
Spring Security笔记:解决CsrfFilter与Rest服务Post方式的矛盾相关推荐
- 解决CsrfFilter与Rest服务Post方式的矛盾
基于Spring Security+Spring MVC的web应用,为了防止跨站提交攻击,通常会配置csrf,即: 1 <http ...> 2 ... 3 <csrf /> ...
- 使用Spring Security 3.1保护RESTful Web服务,第3部分
1.概述 本教程显示了如何使用Spring和基于Java的Spring Security 3.1来保护REST服务 . 本文将重点介绍如何使用"登录和Cookie"方法专门针对RE ...
- Spring Security笔记:使用数据库进行用户认证(form login using database) - 菩提树下的杨过 - 博客园...
Spring Security笔记:使用数据库进行用户认证(form login using database) - 菩提树下的杨过 - 博客园 在前一节,学习了如何自定义登录页,但是用户名.密码仍然 ...
- 《深入理解 Spring Cloud 与微服务构建》第十八章 使用 Spring Security OAuth2 和 JWT 保护微服务系统
<深入理解 Spring Cloud 与微服务构建>第十八章 使用 Spring Security OAuth2 和 JWT 保护微服务系统 文章目录 <深入理解 Spring Cl ...
- Spring Security笔记:HTTP Basic 认证
在第一节 Spring Security笔记:Hello World 的基础上,只要把Spring-Security.xml里改一个位置 1 <http auto-config="tr ...
- 使用Spring Security Oauth2 和 JWT保护微服务--Uaa授权服务器的编写
学习自深入理解微服务 采用Spring Security OAuth2 和 JWT的方式,Uaa服务只需要验证一次,返回JWT.返回的JWT包含了用户的所有信息,包括权限信息 从三个方面讲解: JWT ...
- 使用Spring Security Oauth2 和 JWT保护微服务--资源服务器的编写
编写hcnet-website的资源服务 依赖管理pom文件 hcnet-website工程的pom文件继承主maven的pom文件.在hcnet-website工程的pom文件中添加web功能的起步 ...
- 深入OAuth2核心源码,阿里大佬的Spring Security笔记惊呆我了
包含Spring Security Java配置.Spring Security安全防护和源码导读.详细讲解OAuth2实战,并剖析OAuth2核心源码 目录展示 由于手册内容太多,在此只截取部分内容 ...
- spring boot整合spring security笔记
最近自己做了一个小项目,正在进行springboot和spring Security的整合,有一丢丢的感悟,在这里分享一下: 首先,spring boot整合spring security最好是使用T ...
最新文章
- c语言折半查找法_C语言学习|选择法排序及折半查找法查找
- 2014-06-25nbsp;12:55
- 手把手教你部署一个最小化的 Kubernetes 集群
- HDU - 5381 The sum of gcd(莫队/线段树区间合并)
- JVM性能提升50%,聊一聊背后的秘密武器Alibaba Dragonwell
- mysql 查询此时日期_mysql 查询日期
- mysql5.6 排序失效_mysql 使用union(all) + order by 导致排序失效
- python正确方法_下列定义函数的方法,在Python中正确的是()。
- 大学生买macbookpro合适吗?
- html5中的web storage的用法
- ht1621b和单片机电平匹配_干货 | 143条 超详细整理STM32单片机学习笔记
- VMWareStation10 密钥
- mac安装linux时触控板不能用,Linux 下 MacBook 触摸板设置
- 《洞见》罗伯特·赖特
- web简单静态页面设计【HTML+CSS】
- 基尼系数,excel计算方法
- PostgreSQL中with和without time zone两者有什么区别
- 通俗易懂的讲解 网关是什么
- 12306抢票工具震撼来袭
- PHPstudy实战安装帝国CMS