Skip to content

Conversation

vpavic
Copy link
Contributor

@vpavic vpavic commented Nov 13, 2018

This PR enhances the existing Spring Session auto-configuration with DefaultCookieSerializer configuration. This DefaultCookieSerializer is configurable using ServerProperties i.e. server.servlet.session.cookie.* configuration properties.

By doing this, users are able to fully configure DefaultCookieSerializer off server.servlet.session.cookie.* which removes the limitation of Spring Session's native configuration that is unable to reliably set httpOnly and secure attributes.

This resolves #15139 - note that even though that issue is assigned to 2.0.x I've targeted this at master branch as it feels like a big change to have so late in the 2.0 lifecycle. The changes here take basically the same approach as in #10440.

@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label Nov 13, 2018
public DefaultCookieSerializer cookieSerializer() {
Cookie cookie = this.serverProperties.getServlet().getSession().getCookie();
DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
if (cookie.getName() != null) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could probably use the PropertyMapper here:

Something like:

PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
map.from(cookie::getName).to(cookie::setName);
...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's very nice - will get to that shortly.

@philwebb philwebb added type: bug A general bug and removed status: waiting-for-triage An issue we've not yet triaged labels Nov 13, 2018
@philwebb philwebb added this to the 2.0.x milestone Nov 13, 2018

@Bean
@ConditionalOnMissingBean({ CookieSerializer.class,
HeaderHttpSessionIdResolver.class })
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After taking a second look I think this would be better off being conditional on missing CookieSerializer and conditional on CookieHttpSessionIdResolver, because that's the component using the cookie serializer we're configuring here. Otherwise this would switch off when HeaderHttpSessionIdResolver bean is present, but not when users provide their custom HttpSessionIdResolver implementation.

Thoughts?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are the gotchas that I was referring to in #15139 and that we started to discuss in #10440 before we went with the SessionCookieConfig-based approach. I'll need to spend some time refreshing my memory.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After giving this some more thought, I've added a commit with custom condition that kicks in if either of the following is true:

  • no HttpSessionIdResolver and CookieSerializer beans are registered - this is the default state with Spring Session
  • CookieHttpSessionIdResolver bean is registered but CookieSerializer is not - this is when users configure a custom CookieHttpSessionIdResolver but they don't register the CookieSerializer

This covers the case (a quite valid one) when users provide their custom HttpSessionIdResolver implementation. That shouldn't result in CookieSerializer being configured. There's a test in second commit that covers this scenario.

IMO the options are either to have this composite condition approach, or abandon the case when users provide their own CookieHttpSessionIdResolver but not CookieSerializer - one might argue that since CookieHttpSessionIdResolver itself is final and CookieSerializer is the only thing can be configure on it that it's an edge case.

Do you have any thoughts on this @wilkinsona?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @vpavic. The scenario that you've described is the one that I needed to refresh my memory about. I like the custom condition approach that you've implemented. This looks ready to merge to me. Thanks for the PR.

@wilkinsona wilkinsona changed the title Auto-configure Spring Session's cookie serializer Cookie http-only setting has no effect when using Spring Session Nov 23, 2018
@wilkinsona wilkinsona self-assigned this Nov 27, 2018
wilkinsona pushed a commit that referenced this pull request Nov 27, 2018
wilkinsona added a commit that referenced this pull request Nov 27, 2018
* gh-15163:
  Polish "Auto-configure Spring Session's cookie serializer"
  Auto-configure Spring Session's cookie serializer
@wilkinsona wilkinsona modified the milestones: 2.0.x, 2.0.7 Nov 27, 2018
@wilkinsona
Copy link
Member

Thanks again, @vpavic. I've merged the changes in 2.0.x and forwards into master.

@vpavic vpavic deleted the gh-15139 branch November 27, 2018 11:28
@vpavic vpavic restored the gh-15139 branch December 5, 2018 17:33
@vpavic vpavic deleted the gh-15139 branch December 5, 2018 17:35
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type: bug A general bug
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Cookie http-only setting has no effect when using Spring Session
4 participants