Java教程

Java Cookie添加SameSite属性

本文主要是介绍Java Cookie添加SameSite属性,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.iwhalecloud.dbepecsp.sigma.gateway.config;

import org.apache.tomcat.util.http.Rfc6265CookieProcessor;
import org.apache.tomcat.util.http.SameSiteCookies;
import org.springframework.boot.web.embedded.tomcat.TomcatContextCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 设置cookie的SameSite属性,防CSRF
 * @version 1.0
 * @date 2021/11/19
 */
@Configuration
public class TomcatConfiguration {
    @Bean
    public TomcatContextCustomizer sameSiteCookiesConfig() {
        return context -> {
            final Rfc6265CookieProcessor cookieProcessor = new Rfc6265CookieProcessor();
            // 设置Cookie的SameSite
            cookieProcessor.setSameSiteCookies(SameSiteCookies.LAX.getValue());
            context.setCookieProcessor(cookieProcessor);
        };
    }
}


无需其他操作,此类加载后可覆写Tomcat配置
这篇关于Java Cookie添加SameSite属性的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!