【发布时间】:2020-07-06 23:46:58
【问题描述】:
Google chrome 引入了需要设置 Same-Site 标头的更改。为了实现这一点,我添加了一个自定义过滤器,如下所示,
public class SameSiteFilter extends GenericFilterBean {
private Logger LOG = LoggerFactory.getLogger(SameSiteFilter.class);
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
HttpServletResponse resp = (HttpServletResponse)response;
response = addSameSiteCookieAttribute((HttpServletResponse) response);
chain.doFilter(request, response);
}
private HttpServletResponse addSameSiteCookieAttribute(HttpServletResponse response) {
Collection<String> header = response.getHeaders(HttpHeaders.SET_COOKIE);
LOG.info(String.format("%s; %s", header, "SameSite=None; Secure"));
response.setHeader(HttpHeaders.SET_COOKIE, String.format("%s; %s", header, "SameSite=None; Secure"));
return response;
}
}
以下是安全配置的代码
@Configuration
@EnableWebMvcSecurity
public class CustomSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
private OnyxUserDetailsService onyxUserDetailsService;
@Autowired
private CustomAuthenticationProvider customAuthenticationProvider;
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests().antMatchers("/rest/user", "/info/**/*","/rest/version/check")
.permitAll().antMatchers("/data/**/*")
.access("hasRole('ROLE_ADMIN')").anyRequest()
.fullyAuthenticated().and().httpBasic().realmName("ADOBENET")
.and().logout().
logoutSuccessHandler((new LogoutSuccessHandler() {
@Override
public void onLogoutSuccess(HttpServletRequest request,
HttpServletResponse response, Authentication authentication)
throws IOException, ServletException {
response.setStatus(HttpStatus.OK.value());
response.getWriter().flush();
}
})).deleteCookies("JSESSIONID", "XSRF-TOKEN")
.invalidateHttpSession(true).logoutUrl("/rest/logout")
.logoutSuccessUrl("/rest/user").and()
.addFilterAfter(new CsrfHeaderFilter(), CsrfFilter.class)
.addFilterAfter(new SameSiteFilter(), BasicAuthenticationFilter.class)
.csrf().disable();
}
@Override
@Order(Ordered.HIGHEST_PRECEDENCE)
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.authenticationProvider(customAuthenticationProvider);
}
}
但是,当我查看收到的标头时,我明白了
过滤器在包含 JSESSIONID cookie 的所有响应异常中添加必填字段。如何将标头添加到此 cookie。我尝试配置 tomcat 设置,但我们将代码部署为 WAR 文件,所以这也不起作用。
【问题讨论】:
-
我遇到了和你一样的问题,请看stackoverflow.com/questions/67320068/…。有什么提示可以解决这个问题吗?
标签: java spring spring-boot spring-security