首页 > 解决方案 > 为什么没有添加 Set-Cookie 标头?

问题描述

我有一个问题,我不明白为什么不添加cookie。我在不同的服务器和不同的端口上有一个前端和后端,我点击后端获取令牌。作为回应,他得到了它,但是,它没有被设置。有人可以看看吗?安全且 httpOnly 已停用。在前端,我设置了 withCredentials: true。

在前端:

axios.defaults.withCredentials = true;
axios.post('http://backendhost:8584/login', user).then(response => {
            if(response){
                   this.props.history.push('/home');
            }
});

Cors 在 api 方面:

   @Bean
    CorsConfigurationSource corsConfiguration() {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.setAllowedOrigins(Collections.singletonList("*"));
        config.setAllowedMethods(Collections.singletonList("*"));
        config.setAllowedHeaders(Collections.singletonList("*"));

        UrlBasedCorsConfigurationSource source =
                new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        return source;
    }

在认证服务器端设置cookie:

    response.addCookie(generateCookie("session-token", customer.getToken()));

    public Cookie generateCookie(String key, String value) {
        Cookie cookie = new Cookie(key,value);
        cookie.setMaxAge(60 * 60 * 24 * 365);
        cookie.setSecure(false);
        cookie.setHttpOnly(false);
        cookie.setPath("/");
        return cookie;
    }

我在 chrome 中得到它:

Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://frontendhost:8080
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
Content-Type: application/json
Date: Tue, 16 Jun 2020 16:30:11 GMT
Expires: 0
Pragma: no-cache
Referrer-Policy: no-referrer
Set-Cookie: session-token=eyJhbGciOiJSUz; Max-Age=31536000; Expires=Wed, 16-Jun-2021 16:30:11 GMT; Path=/
transfer-encoding: chunked
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
X-Content-Type-Options: nosniff
X-Frame-Options: SAMEORIGIN
X-XSS-Protection: 1; mode=block

标签: reactjscookiesaxioscross-domainresponse

解决方案


推荐阅读