最近一个项目使用springboot 第一次接触,遇到了很多问题,今天说说其中一个 支付宝支付接口 异步通知 返回 403
原因是 scurity 默认开启了csrf ,csrf post 方法需要 _csrf ,支付宝的通知 显然是没有 _scrf的 结果导致访问403.
解决办法 :把支付宝通知方法排除在csrf 之外:
http.csrf().requireCsrfProtectionMatcher( new RequestMatcher() {
private Pattern allowedMethods = Pattern.compile("^(GET|HEAD|TRACE|OPTIONS)$");
@Override
public boolean matches(HttpServletRequest request) {
String servletPath = request.getServletPath();
if (servletPath.contains("/xxxx/xxxx_notify")) {
return false;
}
return !allowedMethods.matcher(request.getMethod()).matches();
}
});