java cookie.setDomain(".test.com"); 错误 There was an unexpected error (type=Internal Server Error, status=500). An invalid domain [.test.com] was specified for this cookie
public void setCookie(HttpServletResponse response, String token) {
// 创建一个新的Cookie对象
Cookie cookie = new Cookie(cookieName, cookieValue);
cookie.setDomain(".test.com"); // domain
cookie.setMaxAge(3600); // 有效期 秒
cookie.setPath("/"); // 设置Cookie的作用路径
response.addCookie(cookie);
}
添以下配置可解决
package com.sdcuike.spring.log.config;
import org.apache.tomcat.util.http.LegacyCookieProcessor;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@Configuration
public class CookieConfig {
/**
* 解决问题:
* There was an unexpected error (type=Internal Server Error, status=500).
* An invalid domain [.localhost.com] was specified for this cookie
*
* @return
*/
@Bean
public WebServerFactoryCustomizer<TomcatServletWebServerFactory> cookieProcessorCustomizer() {
return (factory) -> factory.addContextCustomizers(
(context) -> context.setCookieProcessor(new LegacyCookieProcessor()));
}
}
标签:set,Java,setDomain,cookie,context,import,org,com,Cookie
From: https://blog.51cto.com/jinliang/9214531