Skip to content

Commit

Permalink
Polish "Add support for customizing Spring Session's cookie serializer"
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Apr 29, 2020
1 parent 32454b8 commit d65d951
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,9 @@ DefaultCookieSerializer cookieSerializer(ServerProperties serverProperties,
static class RememberMeServicesConfiguration {

@Bean
RememberMeServicesCookieSerializerCustomizer rememberMeServicesCookieSerializerCustomizer() {
return new RememberMeServicesCookieSerializerCustomizer();
CookieSerializerCustomizer rememberMeServicesCookieSerializerCustomizer() {
return (cookieSerializer) -> cookieSerializer
.setRememberMeRequestAttribute(SpringSessionRememberMeServices.REMEMBER_ME_LOGIN_ATTR);
}

}
Expand Down Expand Up @@ -141,19 +142,6 @@ static class ReactiveSessionRepositoryConfiguration {

}

/**
* Customization for {@link SpringSessionRememberMeServices} that is only instantiated
* when Spring Security is on the classpath.
*/
static class RememberMeServicesCookieSerializerCustomizer implements CookieSerializerCustomizer {

@Override
public void customize(DefaultCookieSerializer cookieSerializer) {
cookieSerializer.setRememberMeRequestAttribute(SpringSessionRememberMeServices.REMEMBER_ME_LOGIN_ATTR);
}

}

/**
* Condition to trigger the creation of a {@link DefaultCookieSerializer}. This kicks
* in if either no {@link HttpSessionIdResolver} and {@link CookieSerializer} beans
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import javax.servlet.DispatcherType;

import org.junit.jupiter.api.Test;
import org.mockito.InOrder;

import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.autoconfigure.web.ServerProperties;
Expand All @@ -30,6 +31,7 @@
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.session.MapSessionRepository;
import org.springframework.session.SessionRepository;
import org.springframework.session.config.annotation.web.http.EnableSpringHttpSession;
Expand All @@ -42,6 +44,8 @@
import org.springframework.test.util.ReflectionTestUtils;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.inOrder;
import static org.mockito.Mockito.mock;

/**
Expand Down Expand Up @@ -205,6 +209,16 @@ void autoConfiguredCookieSerializerIsConfiguredWithRememberMeRequestAttribute()
});
}

@Test
void cookieSerializerCustomization() {
this.contextRunner.withBean(CookieSerializerCustomization.class).run((context) -> {
CookieSerializerCustomization customization = context.getBean(CookieSerializerCustomization.class);
InOrder inOrder = inOrder(customization.customizer1, customization.customizer2);
inOrder.verify(customization.customizer1).customize(any());
inOrder.verify(customization.customizer2).customize(any());
});
}

@Configuration(proxyBeanMethods = false)
@EnableSpringHttpSession
static class SessionRepositoryConfiguration {
Expand Down Expand Up @@ -276,4 +290,26 @@ SpringSessionRememberMeServices rememberMeServices() {

}

@Configuration(proxyBeanMethods = false)
@EnableSpringHttpSession
static class CookieSerializerCustomization extends SessionRepositoryConfiguration {

private final CookieSerializerCustomizer customizer1 = mock(CookieSerializerCustomizer.class);

private final CookieSerializerCustomizer customizer2 = mock(CookieSerializerCustomizer.class);

@Bean
@Order(1)
CookieSerializerCustomizer customizer1() {
return this.customizer1;
}

@Bean
@Order(2)
CookieSerializerCustomizer customizer2() {
return this.customizer2;
}

}

}

0 comments on commit d65d951

Please sign in to comment.