Skip to content

Commit 8e34ced

Browse files
committed
Detect UserDetailsService bean in remember me
Closes gh-11170
1 parent a3e7e54 commit 8e34ced

File tree

2 files changed

+61
-3
lines changed

2 files changed

+61
-3
lines changed

config/src/main/java/org/springframework/security/config/annotation/web/configurers/RememberMeConfigurer.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2015 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -18,6 +18,8 @@
1818

1919
import java.util.UUID;
2020

21+
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
22+
import org.springframework.context.ApplicationContext;
2123
import org.springframework.security.authentication.AuthenticationManager;
2224
import org.springframework.security.authentication.RememberMeAuthenticationProvider;
2325
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
@@ -403,7 +405,7 @@ private AbstractRememberMeServices createPersistentRememberMeServices(H http, St
403405
*/
404406
private UserDetailsService getUserDetailsService(H http) {
405407
if (this.userDetailsService == null) {
406-
this.userDetailsService = http.getSharedObject(UserDetailsService.class);
408+
this.userDetailsService = getSharedOrBean(http, UserDetailsService.class);
407409
}
408410
Assert.state(this.userDetailsService != null,
409411
() -> "userDetailsService cannot be null. Invoke " + RememberMeConfigurer.class.getSimpleName()
@@ -431,4 +433,25 @@ private String getKey() {
431433
return this.key;
432434
}
433435

436+
private <C> C getSharedOrBean(H http, Class<C> type) {
437+
C shared = http.getSharedObject(type);
438+
if (shared != null) {
439+
return shared;
440+
}
441+
return getBeanOrNull(type);
442+
}
443+
444+
private <T> T getBeanOrNull(Class<T> type) {
445+
ApplicationContext context = getBuilder().getSharedObject(ApplicationContext.class);
446+
if (context == null) {
447+
return null;
448+
}
449+
try {
450+
return context.getBean(type);
451+
}
452+
catch (NoSuchBeanDefinitionException ex) {
453+
return null;
454+
}
455+
}
456+
434457
}

config/src/test/java/org/springframework/security/config/annotation/web/configurers/RememberMeConfigurerTests.java

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -42,6 +42,7 @@
4242
import org.springframework.security.core.userdetails.UserDetailsService;
4343
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
4444
import org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers;
45+
import org.springframework.security.web.SecurityFilterChain;
4546
import org.springframework.security.web.authentication.RememberMeServices;
4647
import org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter;
4748
import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices;
@@ -117,6 +118,20 @@ public void rememberMeWhenInvokedTwiceThenUsesOriginalUserDetailsService() throw
117118
verify(DuplicateDoesNotOverrideConfig.userDetailsService).loadUserByUsername("user");
118119
}
119120

121+
@Test
122+
public void rememberMeWhenUserDetailsServiceNotConfiguredThenUsesBean() throws Exception {
123+
this.spring.register(UserDetailsServiceBeanConfig.class).autowire();
124+
MvcResult mvcResult = this.mvc.perform(post("/login").with(csrf()).param("username", "user")
125+
.param("password", "password").param("remember-me", "true")).andReturn();
126+
Cookie rememberMeCookie = mvcResult.getResponse().getCookie("remember-me");
127+
// @formatter:off
128+
MockHttpServletRequestBuilder request = get("/abc").cookie(rememberMeCookie);
129+
SecurityMockMvcResultMatchers.AuthenticatedMatcher remembermeAuthentication = authenticated()
130+
.withAuthentication((auth) -> assertThat(auth).isInstanceOf(RememberMeAuthenticationToken.class));
131+
// @formatter:on
132+
this.mvc.perform(request).andExpect(remembermeAuthentication);
133+
}
134+
120135
@Test
121136
public void loginWhenRememberMeTrueThenRespondsWithRememberMeCookie() throws Exception {
122137
this.spring.register(RememberMeConfig.class).autowire();
@@ -370,6 +385,26 @@ public UserDetailsService userDetailsService() {
370385

371386
}
372387

388+
@EnableWebSecurity
389+
static class UserDetailsServiceBeanConfig {
390+
391+
@Bean
392+
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
393+
// @formatter:off
394+
http
395+
.formLogin(withDefaults())
396+
.rememberMe(withDefaults());
397+
// @formatter:on
398+
return http.build();
399+
}
400+
401+
@Bean
402+
UserDetailsService customUserDetailsService() {
403+
return new InMemoryUserDetailsManager(PasswordEncodedUser.user());
404+
}
405+
406+
}
407+
373408
@EnableWebSecurity
374409
static class RememberMeConfig extends WebSecurityConfigurerAdapter {
375410

0 commit comments

Comments
 (0)