大数据(BD社区)-专业IT技术社区

?找回密码
?365bet开户娱乐

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 408|回复: 1
打印 上一主题 下一主题

An invalid domain [.test.com] was specified for this cookie

[复制链接]

70

主题

73

帖子

389

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
389
跳转到指定楼层
楼主
发表于 2018-7-9 19:28:24 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
当项目中使用单点登录功能时,通常会使用cookie进行信息的保存,这样就可以在多个子域名上存取用户信息。 比如有三个domain分别为test.com,cml.test.com,b.test.com这三个域名下的cookie是需要互相访问的。这时会在response上写入cookie信息
  1. Cookie cookie = new Cookie("testCookie", "test");
  2. ? ?? ???cookie.setDomain(".test.com");
  3. ? ?? ???cookie.setPath("/");
  4. ? ?? ???cookie.setMaxAge(36000);
  5. ? ?? ???resp.addCookie(cookie);
复制代码
这样写在tomcat8.0上是没问题的,三个域名可以共享cookie信息。但是把它放到tomcat8.5上就报错了
  1. java.lang.IllegalArgumentException: An invalid domain [.test.com] was specified for this cookie
  2. ? ?? ???at org.apache.tomcat.util.http.Rfc6265CookieProcessor.validateDomain(Rfc6265CookieProcessor.java:181)
  3. ? ?? ???at org.apache.tomcat.util.http.Rfc6265CookieProcessor.generateHeader(Rfc6265CookieProcessor.java:123)
  4. ? ?? ???at org.apache.catalina.connector.Response.generateCookieString(Response.java:989)
  5. ? ?? ???at org.apache.catalina.connector.Response.addCookie(Response.java:937)
  6. ? ?? ???at org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:386)
  7. ? ?? ???at com.cml.mvc.controller.HelloWorld.str(HelloWorld.java:98)
  8. ? ?? ???at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
  9. ? ?? ???at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
  10. ? ?? ???at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  11. ? ?? ???at java.lang.reflect.Method.invoke(Method.java:497)
  12. ? ?? ???at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
  13. ? ?? ???at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
  14. ? ?? ???at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
  15. ? ?? ???at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
  16. ? ?? ???at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
  17. ? ?? ???at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
复制代码
在tomcat8.5上是使用org.apache.tomcat.util.http.Rfc6265CookieProcessor
  1. The standard implementation of CookieProcessor is org.apache.tomcat.util.http.Rfc6265CookieProcessor.

  2. This cookie processor is based on RFC6265 with the following changes to support better interoperability:

  3. Values 0x80 to 0xFF are permitted in cookie-octet to support the use of UTF-8 in cookie values as used by HTML 5.
  4. For cookies without a value, the '=' is not required after the name as some browsers do not sent it.
  5. The RFC 6265 cookie processor is generally more lenient than the legacy cookie parser. In particular:

  6. The '=' and '/' characters are always permitted in a cookie value.
  7. Name only cookies are always permitted.
  8. The cookie header is always preserved.
  9. No additional attributes are supported by the RFC 6265 Cookie Processor.
复制代码
在tomcat8.0上使用的是org.apache.tomcat.util.http.LegacyCookieProcessor
  1. The standard implementation of CookieProcessor is org.apache.tomcat.util.http.LegacyCookieProcessor. Note that it is anticipated that this will change to org.apache.tomcat.util.http.Rfc6265CookieProcessor in a future Tomcat 8 release.

  2. This is the legacy cookie parser based on RFC6265, RFC2109 and RFC2616. It implements a strict interpretation of the cookie specifications. Due to various interoperability issues with browsers not all strict behaviours are enabled by default and additional options are available to further relax the behaviour of this cookie processor if required.
复制代码
问题就可以定位在CookieProcessor不同实现引起的。
解决方法:
  • 指定完整的domain信息,但是这样单点登录就会有问题了
    1. Cookie cookie = new Cookie("testCookie", "test");
    2. ? ?? ???cookie.setDomain("cml.test.com");
    3. ? ?? ???cookie.setPath("/");
    4. ? ?? ???cookie.setMaxAge(36000);
    5. ? ?? ???resp.addCookie(cookie);
    复制代码

  • 设置为一级域名(推荐)
    1. Cookie cookie = new Cookie("testCookie", "test");
    2. ? ?? ???cookie.setDomain("test.com");
    3. ? ?? ???cookie.setPath("/");
    4. ? ?? ???cookie.setMaxAge(36000);
    5. ? ?? ???resp.addCookie(cookie);
    复制代码



0

主题

3

帖子

18

积分

禁止发言

积分
18
沙发
发表于 2018-7-11 14:04:39 | 只看该作者
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 365bet开户娱乐

本版积分规则

Archiver|手机版|小黑屋|BD Inc. ( 京ICP备16038882号-2 )

GMT+8, 2019-9-23 10:21 , Processed in 0.025710 second(s), 16 queries .

Powered by Discuz! X3.4

? 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表