reactjs - 为什么没有添加 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
解决方案
推荐阅读
- java - 使用继承适当的设计没有属性吗?导致序列化问题。爪哇
- java - 如何检查和显示其他两个特定单词之间的单词?
- bash - 是否有一个函数可以让我在文本文件中的一组行的开头添加一个字符串?
- ruby - 使用 ruby byebug 进行 Firendly 回溯(where 命令)
- vue.js - 在存储数据更改时自动更新 v-data-table
- javascript - ag-grid 服务器端无限滚动访问道具
- python - 如何使用 pytest 在装饰器中使用命令行参数?
- macos - mac mojave 中的“rm:“.”和“..”可能无法删除”消息
- excel - 用马尔可夫链记分的石头剪刀布模拟
- c# - 使用 json.net 在 Unity 中保存集合