|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2022 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
42 | 42 | import org.springframework.security.core.userdetails.UserDetailsService;
|
43 | 43 | import org.springframework.security.provisioning.InMemoryUserDetailsManager;
|
44 | 44 | import org.springframework.security.test.web.servlet.response.SecurityMockMvcResultMatchers;
|
| 45 | +import org.springframework.security.web.SecurityFilterChain; |
45 | 46 | import org.springframework.security.web.authentication.RememberMeServices;
|
46 | 47 | import org.springframework.security.web.authentication.rememberme.RememberMeAuthenticationFilter;
|
47 | 48 | import org.springframework.security.web.authentication.rememberme.TokenBasedRememberMeServices;
|
@@ -117,6 +118,20 @@ public void rememberMeWhenInvokedTwiceThenUsesOriginalUserDetailsService() throw
|
117 | 118 | verify(DuplicateDoesNotOverrideConfig.userDetailsService).loadUserByUsername("user");
|
118 | 119 | }
|
119 | 120 |
|
| 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 | + |
120 | 135 | @Test
|
121 | 136 | public void loginWhenRememberMeTrueThenRespondsWithRememberMeCookie() throws Exception {
|
122 | 137 | this.spring.register(RememberMeConfig.class).autowire();
|
@@ -370,6 +385,26 @@ public UserDetailsService userDetailsService() {
|
370 | 385 |
|
371 | 386 | }
|
372 | 387 |
|
| 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 | + |
373 | 408 | @EnableWebSecurity
|
374 | 409 | static class RememberMeConfig extends WebSecurityConfigurerAdapter {
|
375 | 410 |
|
|
0 commit comments