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配置
标签:cookieProcessor,Java,SameSite,Rfc6265CookieProcessor,Cookie,context,org,import 来源: https://www.cnblogs.com/sulishihupan/p/15582648.html