Is it possible to set Same-site Cookie flag in Spring Security?
And if not, is it on a roadmap to add support, please? There is already support in some browsers (i.e. Chrome).
Is it possible to set Same-site Cookie flag in Spring Security?
And if not, is it on a roadmap to add support, please? There is already support in some browsers (i.e. Chrome).
You can always set cookie values by yourself in the Java world if you can get an instance of the
HttpServletResponse
.Then you can do:
In spring-security you can easily do this with a filter, here is an example:
Add this filter to your SecurityConfig like this:
Or via XML:
Instead of a Filter, In your Authentication Success Handler, you can mention in this way.
It was mentioned in one of the answers. Couldn't find the link after I've implemented it.
Using the interceptor in SpringBoot.
I'm looking for a resolution for adding SameSite as you, and I only want to add the attribute to the existing "Set-Cookie" instead of creating a new "Set-Cookie". I have tried several ways to meet this requirement, including:
1.adding a custom filter as @unwichtich said,
2.and more I overrode basicAuthenticationFilter. It does add the SameSite attribute. While the timing when Spring will add the "Set-Cookie" is hard to catch. I thought in onAuthenticationSuccess() method, the response must have this header, but it doesn't. I'm not sure whether it's the fault of my custom basicAuthenticationFilter's order.
3.using cookieSerializer, but the spring-session version comes up to a problem. Seems only the latest version support it, but I still can't figure out the version number should be added into the dependency list.
Unfortunately, none of them above can add the samesite well as expected.
Finally, I found the interceptor in spring can help me to make it. It took me a week to get it. Hope this can help you if anyone has the same problem.
and you also need to make this interceptor work in your application, which means you should add a bean as below: