Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 538|回复: 0

java.lang.IllegalArgumentException: An invalid domain [.test.com] was specified for this cookie解决方法

[复制链接]
  • TA的每日心情
    奋斗
    3 天前
  • 签到天数: 798 天

    [LV.10]以坛为家III

    2050

    主题

    2108

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    724790
    发表于 2021-6-24 05:54:38 | 显示全部楼层 |阅读模式

    当项目中使用单点登录功能时,通常会使用cookie进行信息的保存,这样就可以在多个子域名上存取用户信息。
    比如有三个domain分别为test.com,cml.test.com,b.test.com这三个域名下的cookie是需要互相访问的。这时会在response上写入cookie信息

    Cookie cookie = new Cookie("testCookie", "test");
    cookie.setDomain(".test.com");
    cookie.setPath("/");
    cookie.setMaxAge(36000);
    resp.addCookie(cookie);
    这样写在tomcat8.0上是没问题的,三个域名可以共享cookie信息。但是把它放到tomcat8.5上就报错了

    java.lang.IllegalArgumentException: An invalid domain [.test.com] was specified for this cookie
    at org.apache.tomcat.util.http.Rfc6265CookieProcessor.validateDomain(Rfc6265CookieProcessor.java:181)
    at org.apache.tomcat.util.http.Rfc6265CookieProcessor.generateHeader(Rfc6265CookieProcessor.java:123)
    at org.apache.catalina.connector.Response.generateCookieString(Response.java:989)
    at org.apache.catalina.connector.Response.addCookie(Response.java:937)
    at org.apache.catalina.connector.ResponseFacade.addCookie(ResponseFacade.java:386)
    at com.cml.mvc.controller.HelloWorld.str(HelloWorld.java:98)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)
    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:137)
    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandleMethod(RequestMappingHandlerAdapter.java:777)
    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:706)
    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)

    在tomcat8.5上是使用org.apache.tomcat.util.http.Rfc6265CookieProcessor

    The standard implementation of CookieProcessor is org.apache.tomcat.util.http.Rfc6265CookieProcessor.

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

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

    The '=' and '/' characters are always permitted in a cookie value.
    Name only cookies are always permitted.
    The cookie header is always preserved.
    No additional attributes are supported by the RFC 6265 Cookie Processor.

    在tomcat8.0上使用的是org.apache.tomcat.util.http.LegacyCookieProcessor

    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.

    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不同实现引起的。 

    原因分析见下半篇博客:An invalid domain [.test.com] was specified for this cookie 原因分析

    解决方法:

    指定完整的domain信息,但是这样单点登录就会有问题了
    Cookie cookie = new Cookie("testCookie", "test");
    cookie.setDomain("cml.test.com");
    cookie.setPath("/");
    cookie.setMaxAge(36000);
    resp.addCookie(cookie);

    2.设置为一级域名(推荐)

    Cookie cookie = new Cookie("testCookie", "test");
    cookie.setDomain("test.com");
    cookie.setPath("/");
    cookie.setMaxAge(36000);
    resp.addCookie(cookie);

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-10-15 22:28 , Processed in 0.069267 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

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