XSRF-TOKEN仅在AngularJS+中由Http保护;Spring引导,但不包括在请求返回中
使用http设置XSRF-TOKEN仅在AngularJS+中由Http保护;Spring引导,但不包括在请求返回中,angularjs,security,spring-boot,Angularjs,Security,Spring Boot,使用http设置XSRF-TOKENcookie只会给Angular Framework从document.cookie函数中获取它带来问题 这有什么办法吗?我无法将XSRF-TOKEN设置为非HttpOnly 我想也许我可以拦截中间层(Spring Boot)中的每个传入请求,检查cookies,如果它是POST/PUT/DELETE请求,并且它有XSRF-TOKEN,我将添加X-XSRF-TOKEN头?AngularJS以“document.cookies…”的方式访问cookies,即通过
XSRF-TOKEN
cookie只会给Angular Framework从document.cookie
函数中获取它带来问题
这有什么办法吗?我无法将XSRF-TOKEN
设置为非HttpOnly
我想也许我可以拦截中间层(Spring Boot)中的每个传入请求,检查cookies,如果它是POST/PUT/DELETE请求,并且它有
XSRF-TOKEN
,我将添加X-XSRF-TOKEN
头?AngularJS以“document.cookies…”的方式访问cookies,即通过javascript本身,在这种情况下,HttpOnly无法工作。您应该将SpringCookie翻译成angular期望和理解的东西(“XSRF-TOKEN”)。这可以在如下过滤器中完成:
public class CsrfHeaderFilter extends OncePerRequestFilter {
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {
CsrfToken csrf = (CsrfToken) request.getAttribute(CsrfToken.class.getName());
if (csrf != null) {
Cookie cookie = WebUtils.getCookie(request, "XSRF-TOKEN");
String token = csrf.getToken();
if (cookie == null || token != null && !token.equals(cookie.getValue())) {
cookie = new Cookie("XSRF-TOKEN", token);
cookie.setSecure(true);
cookie.setPath(request.getContextPath() + "/");
response.addCookie(cookie);
}
}
filterChain.doFilter(request, response);
}
}