Spring Security中的Same-Site cookie

是否可以在
Spring Security中设置Same-site Cookie标志?

见:
https://tools.ietf.org/html/draft-west-first-party-cookies-07

如果没有,请在路线图上添加支持吗?某些浏览器(即Chrome)已经支持.

T.H. 最佳答案 如果可以获得HttpServletResponse的实例,则可以始终在Java世界中自己设置cookie值.

然后你可以这样做:

response.setHeader("Set-Cookie", "key=value; HttpOnly; SameSite=strict")

在spring-security中,您可以使用过滤器轻松完成此操作,这是一个示例:

public class CustomFilter extends GenericFilterBean {

    @Override
    public void doFilter(ServletRequest request,  ServletResponse response, FilterChain chain) throws IOException, ServletException {

        HttpServletResponse resp = (HttpServletResponse)response;

        resp.setHeader("Set-Cookie", "locale=de; HttpOnly; SameSite=strict");

        chain.doFilter(request, response);
    }
}

将此过滤器添加到您的SecurityConfig,如下所示:

http.addFilterAfter(new CustomFilter(), BasicAuthenticationFilter.class)

或者通过XML:

    <http>
        <custom-filter after="BASIC_AUTH_FILTER" ref="myFilter" />
    </http>

<beans:bean id="myFilter" class="org.bla.CustomFilter"/>
点赞