0
点赞
收藏
分享

微信扫一扫

Java set-cooike cookie.setDomain错误

微笑沉默 2024-01-12 阅读 8

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()));
    }

}
举报

相关推荐

0 条评论