Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XSRF-TOKEN仅在AngularJS+中由Http保护;Spring引导,但不包括在请求返回中_Angularjs_Security_Spring Boot - Fatal编程技术网

XSRF-TOKEN仅在AngularJS+中由Http保护;Spring引导,但不包括在请求返回中

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,即通过

使用http设置
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);
}
}