gpt4 book ai didi

Java HttpServletResponse addCookie 在 chrome 和 ie 上没有用

转载 作者:行者123 更新时间:2023-11-29 05:04:50 26 4
gpt4 key购买 nike

我使用addCookie 方法来设置scdid 就像一个jsessionid。但不幸的是,它在 Chrome 和 IE 上没有用,而在 Firefox 上没问题。

那么,有人可以帮助我吗?

Cookie cookie = new Cookie("xx", "xxx");
cookie.setMaxAge(3600);
cookie.setDomain("xxxx");
cookie.setPath("/");
response.addCookie(cookie);

这是请求/响应正文:

Headers
Remote Address:127.0.0.1:80
Request URL:http(can't give it to a link)://localhost/login
Request Method:POST
Status Code:302 Found

Request body
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:28
Content-Type:application/x-www-form-urlencoded
Cookie:JSESSIONID=3a210ab5-2e48-4a0b-b669-f9b5e82b9988
Host:localhost
Origin:http://localhost
Referer:http://localhost/login
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36

Response body
Content-Length:0
Date:Tue, 09 Jun 2015 01:06:52 GMT
Location:http://localhost/
Server:Apache-Coyote/1.1
Set-Cookie:examid=366d69ae-5249-4e68-b779-c03056188249; Domain=localhost; Expires=Tue, 09-Jun-2015 02:06:51 GMT; Path=/

上述回复于 2015 年 6 月 9 日星期二 08:06:51 收到

2015-06-10 编辑:我已经通过以下方式解决了这个问题,但我仍然很困惑。

Cookie cookie = new Cookie("examid", UUID.randomUUID().toString());
response.addCookie(cookie);

在这个方案中,我没有设置expires、domain和path,它们的灵感来自于Chrome中的jsessionid。

Remote Address:127.0.0.1:80
Request URL:http://localhost/login
Request Method:POST
Status Code:302 Found

Request Headers
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Accept-Encoding:gzip, deflate
Accept-Language:zh-CN,zh;q=0.8,en-US;q=0.6,en;q=0.4
Cache-Control:max-age=0
Connection:keep-alive
Content-Length:28
Content-Type:application/x-www-form-urlencoded
Cookie:JSESSIONID=f63df7a3-f381-4914-92c1-a349bf73316b; examid=
Host:localhost
Origin:http://localhost
Referer:http://localhost/login
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.111 Safari/537.36

Form Dataview source
username:admin
password:xxxx

Response Headers
Content-Length:0
Date:Wed, 10 Jun 2015 01:35:53 GMT
Location:http://localhost/
Server:Apache-Coyote/1.1
Set-Cookie:examid=d65f7974-17f1-4338-9284-48f00670a012

2015-06-12 编辑:MaxAge、路径、域我都一一设置。我发现它在设置域(来自请求参数)时出错。我的代码是如何获取域:

private static final String getDomainName(HttpServletRequest request) {
String domainName = null;

String serverName = request.getRequestURL().toString();
if (serverName == null || serverName.equals("")) {
domainName = "";
} else {
serverName = serverName.toLowerCase();
serverName = serverName.substring(7);
final int end = serverName.indexOf("/");
serverName = serverName.substring(0, end);
final String[] domains = serverName.split("\\.");
int len = domains.length;
if (len > 3) {
// www.xxx.com.cn
domainName = "." + domains[len - 3] + "." + domains[len - 2] + "." + domains[len - 1];
} else if (len <= 3 && len > 1) {
// xxx.com or xxx.cn
domainName = "." + domains[len - 2] + "." + domains[len - 1];
} else {
domainName = serverName;
}
}

if (domainName != null && domainName.indexOf(":") > 0) {
String[] ary = domainName.split("\\:");
domainName = ary[0];
}
return domainName;
}

最佳答案

更新问题的答案:

Actually, I get domain "localhost" by my code

所以这就是问题所在:如果您的域名 = localhost,则不会设置 cookie。通过 spec , cookie 域名必须至少有 2 或 3 个点 (.)。 localhost是顶级域名,这样不行。不遵守此规则可能会导致某些浏览器出现问题:

Only hosts within the specified domain can set a cookie for a domain and domains must have at least two (2) or three (3) periods in them to prevent domains of the form: ".com", ".edu", and "va.us".

作为解决方案,在本地主机上工作时,我建议您不要设置域名,或者只使用 127.0.0.1

可以引用this answerthis answer获取更多信息。

关于Java HttpServletResponse addCookie 在 chrome 和 ie 上没有用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30710917/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com