|
50 | 50 | import org.hamcrest.core.StringStartsWith;
|
51 | 51 | import org.junit.jupiter.api.Test;
|
52 | 52 | import org.junit.jupiter.api.extension.ExtendWith;
|
| 53 | +import org.mockito.verification.VerificationMode; |
53 | 54 |
|
54 | 55 | import org.springframework.beans.factory.BeanCreationException;
|
55 | 56 | import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
|
|
82 | 83 | import org.springframework.security.authentication.AuthenticationServiceException;
|
83 | 84 | import org.springframework.security.authentication.TestingAuthenticationToken;
|
84 | 85 | import org.springframework.security.config.annotation.ObjectPostProcessor;
|
| 86 | +import org.springframework.security.config.annotation.SecurityContextChangedListenerConfig; |
85 | 87 | import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
|
86 | 88 | import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
|
87 | 89 | import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
|
92 | 94 | import org.springframework.security.core.Authentication;
|
93 | 95 | import org.springframework.security.core.GrantedAuthority;
|
94 | 96 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
| 97 | +import org.springframework.security.core.context.SecurityContextChangedListener; |
| 98 | +import org.springframework.security.core.context.SecurityContextHolderStrategy; |
95 | 99 | import org.springframework.security.core.userdetails.UserDetailsService;
|
96 | 100 | import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
97 | 101 | import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
|
|
153 | 157 | import static org.mockito.ArgumentMatchers.anyString;
|
154 | 158 | import static org.mockito.ArgumentMatchers.eq;
|
155 | 159 | import static org.mockito.BDDMockito.given;
|
| 160 | +import static org.mockito.Mockito.atLeastOnce; |
156 | 161 | import static org.mockito.Mockito.mock;
|
157 | 162 | import static org.mockito.Mockito.never;
|
158 | 163 | import static org.mockito.Mockito.verify;
|
@@ -218,6 +223,33 @@ public void getWhenUsingDefaultsWithValidBearerTokenThenAcceptsRequest() throws
|
218 | 223 | // @formatter:on
|
219 | 224 | }
|
220 | 225 |
|
| 226 | + @Test |
| 227 | + public void getWhenCustomSecurityContextHolderStrategyThenUses() throws Exception { |
| 228 | + this.spring.register(RestOperationsConfig.class, DefaultConfig.class, BasicController.class, SecurityContextChangedListenerConfig.class).autowire(); |
| 229 | + mockRestOperations(jwks("Default")); |
| 230 | + String token = this.token("ValidNoScopes"); |
| 231 | + // @formatter:off |
| 232 | + this.mvc.perform(get("/").with(bearerToken(token))) |
| 233 | + .andExpect(status().isOk()) |
| 234 | + .andExpect(content().string("ok")); |
| 235 | + // @formatter:on |
| 236 | + verifyBean(SecurityContextHolderStrategy.class, atLeastOnce()).getContext(); |
| 237 | + } |
| 238 | + |
| 239 | + @Test |
| 240 | + public void getWhenSecurityContextHolderStrategyThenUses() throws Exception { |
| 241 | + this.spring.register(RestOperationsConfig.class, DefaultConfig.class, |
| 242 | + SecurityContextChangedListenerConfig.class, BasicController.class).autowire(); |
| 243 | + mockRestOperations(jwks("Default")); |
| 244 | + String token = this.token("ValidNoScopes"); |
| 245 | + // @formatter:off |
| 246 | + this.mvc.perform(get("/").with(bearerToken(token))) |
| 247 | + .andExpect(status().isOk()) |
| 248 | + .andExpect(content().string("ok")); |
| 249 | + // @formatter:on |
| 250 | + verifyBean(SecurityContextChangedListener.class, atLeastOnce()).securityContextChanged(any()); |
| 251 | + } |
| 252 | + |
221 | 253 | @Test
|
222 | 254 | public void getWhenUsingDefaultsInLambdaWithValidBearerTokenThenAcceptsRequest() throws Exception {
|
223 | 255 | this.spring.register(RestOperationsConfig.class, DefaultInLambdaConfig.class, BasicController.class).autowire();
|
@@ -1435,6 +1467,10 @@ private <T> T verifyBean(Class<T> beanClass) {
|
1435 | 1467 | return verify(this.spring.getContext().getBean(beanClass));
|
1436 | 1468 | }
|
1437 | 1469 |
|
| 1470 | + private <T> T verifyBean(Class<T> beanClass, VerificationMode mode) { |
| 1471 | + return verify(this.spring.getContext().getBean(beanClass), mode); |
| 1472 | + } |
| 1473 | + |
1438 | 1474 | private String json(String name) throws IOException {
|
1439 | 1475 | return resource(name + ".json");
|
1440 | 1476 | }
|
|
0 commit comments