@RequestMapping("/getSession")
public ResultModel getSession(HttpServletRequest request,LoginParam loginParam) {
HttpSession session = request.getSession();
String sessionId = session.getId();
session.setAttribute("sessionId","sessionId");
Map<String,String> obj = new HashMap<String,String>();
obj.put("sessionId",sessionId);
return protocolBuilder.build(obj, "登录接口调用成功");
}
let sessonId = data.data.sessionId;
let days = 1;
let nowTime = new Date();
nowTime.setTime(nowTime.getTime() + days * 24 * 60 * 60 * 1000)
document.cookie = 'JSESSIONID=' + sessonId + ';expires=' + nowTime + ';path=/';
Request URL: http://127.0.0.1:8080/getSession
Request Method: POST
Status Code: 200 OK
Remote Address: 127.0.0.1:8080
Referrer Policy: strict-origin-when-cross-origin
Access-Control-Allow-Credentials: true
Access-Control-Allow-Origin: http://127.0.0.1:8080
Access-Control-Expose-Headers: Set-Cookie
Content-Language: zh-CN
Content-Length: 161
Content-Type: text/json;charset=UTF-8
Date: Wed, 30 Nov 2022 10:13:51 GMT
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=C8F8DA6AF9B7BCECDCED6680049ECA61; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,zh;q=0.9
Connection: keep-alive
Content-Length: 91
Content-Type: application/x-www-form-urlencoded;
charset=UTF-8
Host: 192.100.2.74:8888
Origin: http://192.100.2.226:32699
Referer: http://192.100.2.226:32699/
User-Agent: Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/107.0.0.0 Mobile Safari/537.36
Set-Cookie: JSESSIONID=C8F8DA6AF9B7BCECDCED6680049ECA61; Expires=Thu, 01-Jan-1970 00:00:10 GMT; Path=/; HttpOnly
简单的来说就是出现了跨域请求,但浏览器的HttpOnly是不支持跨域下cookie操作的。因此设置cookie失败。
解决方法有很多,有通过代码解决的,可以通过设置浏览器来解决,但是不能要求所有用户都改浏览器设置,我由于是自己的项目,就直接设置浏览器了。
谷歌版本如下图所示:
所以就是下载的插件解决的
链接:https://pan.baidu.com/s/1U_U8a9ZARVJRdh-UeHP7ig
提取码:kbml
打开谷歌浏览器,地址栏输入:chrome://extensions/然后打开开发者模式,把下载的拖进去即可!