springboot cookie增加SameSite=None;Secure属性
版本
2.x
依赖
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-core</artifactId>
</dependency>
配置
import org.springframework.boot.autoconfigure.session.DefaultCookieSerializerCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.session.MapSessionRepository;
import org.springframework.session.SessionRepository;
import org.springframework.session.config.annotation.web.http.EnableSpringHttpSession;
import org.springframework.session.web.http.DefaultCookieSerializer;
import java.util.concurrent.ConcurrentHashMap;
@Configuration
@EnableSpringHttpSession
public class SessionConfig {
@Bean
public SessionRepository sessionRepository() {
return new MapSessionRepository(new ConcurrentHashMap<>());
}
@Bean
DefaultCookieSerializerCustomizer cookieSerializerCustomizer() {
return new DefaultCookieSerializerCustomizer() {
@Override
public void customize(DefaultCookieSerializer cookieSerializer) {
cookieSerializer.setSameSite("None");
cookieSerializer.setUseSecureCookie(true); // 此项必须,否则set-cookie会被chrome浏览器阻拦
}
};
}
}