我有:
我需要什么:
我尝试了什么
server.servlet.session.cookie.same-site= none
属性,但是 它只影响 JSESSIONID cookie。它不适用于 csrf cookie。Set-cookie
,但是随后,我丢失了 JSESSIONID cookie 的 set-cookie 标头。¿我可以做什么来获取 csrf cookie Samesite=none? ¿还有其他解决方案可以在不同域的 chrome 中运行 csrf 身份验证吗?
提前致谢
我找到了解决方案
CookieCsrfTokenRepository
方法和 setCookieCustomizer
创建自定义
Consumer<ResponseCookie.ResponseCookieBuilder>
CookieCsrfTokenRepository tokenRepository = new CookieCsrfTokenRepository();
tokenRepository.setCookieCustomizer(new Consumer<ResponseCookie.ResponseCookieBuilder>() {
@Override
public void accept(ResponseCookieBuilder t) {
t.sameSite("none");
t.secure(true);
t.httpOnly(false); //Js can read
}
});
filterChain(HttpSecurity http)
申请方法:
@Configuration
@EnableWebSecurity
public class SecurityJavaConfig {
@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
//Configuramos el Csrf Token repository para tener una cookie cross domnain
CookieCsrfTokenRepository tokenRepository = new CookieCsrfTokenRepository();
tokenRepository.setCookieCustomizer(new Consumer<ResponseCookie.ResponseCookieBuilder>() {
@Override
public void accept(ResponseCookieBuilder t) {
t.sameSite(csrfSameSiteCookie);
t.secure(csrfSecureCookie);
t.httpOnly(false); //Para que js pueda leer la cookie
}
});
http
.cors((cors) -> cors.configurationSource(corsConfigurationSource()))
.csrf((csrf) -> csrf
.csrfTokenRepository(tokenRepository)
)
.sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.ALWAYS))
//Autorizaciones end-points
.authorizeHttpRequests((authorize) -> authorize
.requestMatchers(HttpMethod.OPTIONS,"/**").permitAll()
.requestMatchers(HttpMethod.GET,"/csrf").permitAll()
.anyRequest().authenticated()
)
.httpBasic(withDefaults());;
return http.build();
}
}