|
16 | 16 |
|
17 | 17 | package org.springframework.security.config.annotation.web.configurers;
|
18 | 18 |
|
| 19 | +import jakarta.servlet.http.HttpServletRequest; |
19 | 20 | import org.junit.jupiter.api.Test;
|
20 | 21 | import org.junit.jupiter.api.extension.ExtendWith;
|
21 | 22 |
|
22 | 23 | import org.springframework.beans.factory.annotation.Autowired;
|
23 | 24 | import org.springframework.context.annotation.Bean;
|
24 | 25 | import org.springframework.context.annotation.Configuration;
|
25 | 26 | import org.springframework.security.authentication.AnonymousAuthenticationToken;
|
| 27 | +import org.springframework.security.authentication.AuthenticationDetailsSource; |
26 | 28 | import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig;
|
27 | 29 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
28 | 30 | import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
|
39 | 41 | import org.springframework.web.bind.annotation.RestController;
|
40 | 42 | import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
41 | 43 |
|
| 44 | +import static org.mockito.ArgumentMatchers.any; |
| 45 | +import static org.mockito.Mockito.mock; |
42 | 46 | import static org.mockito.Mockito.verify;
|
43 | 47 | import static org.springframework.security.config.Customizer.withDefaults;
|
44 | 48 | import static org.springframework.security.config.annotation.SecurityContextChangedListenerArgumentMatchers.setAuthentication;
|
@@ -101,6 +105,37 @@ public void shouldReturnMyCustomAnonymousConfig() throws Exception {
|
101 | 105 | this.mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(content().string("myAnonymousUser"));
|
102 | 106 | }
|
103 | 107 |
|
| 108 | + @Test |
| 109 | + public void anonymousAuthenticationWhenUsingAuthenticationDetailsSourceRefThenMatchesNamespace() throws Exception { |
| 110 | + this.spring.register(AuthenticationDetailsSourceAnonymousConfig.class).autowire(); |
| 111 | + AuthenticationDetailsSource<HttpServletRequest, ?> source = this.spring.getContext() |
| 112 | + .getBean(AuthenticationDetailsSource.class); |
| 113 | + this.mockMvc.perform(get("/")); |
| 114 | + verify(source).buildDetails(any(HttpServletRequest.class)); |
| 115 | + } |
| 116 | + |
| 117 | + @Configuration |
| 118 | + @EnableWebSecurity |
| 119 | + @EnableWebMvc |
| 120 | + static class AuthenticationDetailsSourceAnonymousConfig { |
| 121 | + |
| 122 | + AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource = mock( |
| 123 | + AuthenticationDetailsSource.class); |
| 124 | + |
| 125 | + @Bean |
| 126 | + SecurityFilterChain filterChain(HttpSecurity http) throws Exception { |
| 127 | + return http |
| 128 | + .anonymous((anonymous) -> anonymous.authenticationDetailsSource(this.authenticationDetailsSource)) |
| 129 | + .build(); |
| 130 | + } |
| 131 | + |
| 132 | + @Bean |
| 133 | + AuthenticationDetailsSource<HttpServletRequest, ?> authenticationDetailsSource() { |
| 134 | + return this.authenticationDetailsSource; |
| 135 | + } |
| 136 | + |
| 137 | + } |
| 138 | + |
104 | 139 | @Configuration
|
105 | 140 | @EnableWebSecurity
|
106 | 141 | @EnableWebMvc
|
|
0 commit comments