Skip to content

Override the key to avoid CookieTheftException #5509

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,11 @@ private UserDetailsService getUserDetailsService(H http) {
*/
private String getKey() {
if (this.key == null) {
this.key = UUID.randomUUID().toString();
if (this.rememberMeServices instanceof AbstractRememberMeServices) {
this.key = ((AbstractRememberMeServices) rememberMeServices).getKey();
} else {
this.key = UUID.randomUUID().toString();
}
}
return this.key;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,11 @@
import org.springframework.security.core.userdetails.PasswordEncodedUser;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.factory.PasswordEncoderFactories;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.web.authentication.RememberMeServices;
import org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter;
import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;

Expand Down Expand Up @@ -189,7 +191,8 @@ protected void configure(HttpSecurity http) throws Exception {
@Bean
public UserDetailsService userDetailsService() {
return new InMemoryUserDetailsManager(
User.withDefaultPasswordEncoder()
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this change necessary? User.withDefaultPasswordEncoder is deprecated, but it is acceptable to use in tests.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

IDEA told me there is a warning when I commit this file, so I changed it. Should I revert this change?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, please revert this change.
The IDE is correct to show a warning, but in this specific case it is allowed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done

User.builder()
.passwordEncoder(it-> PasswordEncoderFactories.createDelegatingPasswordEncoder().encode(it))
.username("user")
.password("password")
.roles("USER")
Expand Down Expand Up @@ -453,4 +456,36 @@ public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception
// @formatter:on
}
}

@Test
public void getWhenRememberMeCookieThenAuthenticationIsRememberMeAuthenticationTokenWithFallbackKeyConfiguration()
throws Exception {
this.spring.register(FallbackRememberMeKeyConfig.class).autowire();

MvcResult mvcResult = this.mvc.perform(post("/login")
.with(csrf())
.param("username", "user")
.param("password", "password")
.param("remember-me", "true"))
.andReturn();
Cookie rememberMeCookie = mvcResult.getResponse().getCookie("remember-me");

this.mvc.perform(get("/abc")
.cookie(rememberMeCookie))
.andExpect(authenticated().withAuthentication(auth ->
assertThat(auth).isInstanceOf(RememberMeAuthenticationToken.class)));
}

@EnableWebSecurity
static class FallbackRememberMeKeyConfig extends RememberMeConfig {

@Override
protected void configure(HttpSecurity http) throws Exception {
super.configure(http);
// @formatter:off
http.rememberMe()
.rememberMeServices(new TokenBasedRememberMeServices("key", userDetailsService()));
// @formatter:on
}
}
}