|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2021 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.
|
|
51 | 51 | import org.hamcrest.core.StringStartsWith;
|
52 | 52 | import org.junit.jupiter.api.Test;
|
53 | 53 | import org.junit.jupiter.api.extension.ExtendWith;
|
| 54 | +import org.mockito.verification.VerificationMode; |
54 | 55 |
|
55 | 56 | import org.springframework.beans.factory.BeanCreationException;
|
56 | 57 | import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
|
|
82 | 83 | import org.springframework.security.authentication.AuthenticationProvider;
|
83 | 84 | import org.springframework.security.authentication.AuthenticationServiceException;
|
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;
|
|
93 | 95 | import org.springframework.security.core.Authentication;
|
94 | 96 | import org.springframework.security.core.GrantedAuthority;
|
95 | 97 | import org.springframework.security.core.authority.SimpleGrantedAuthority;
|
| 98 | +import org.springframework.security.core.context.SecurityContextChangedListener; |
| 99 | +import org.springframework.security.core.context.SecurityContextHolderStrategy; |
96 | 100 | import org.springframework.security.core.userdetails.UserDetailsService;
|
97 | 101 | import org.springframework.security.oauth2.client.registration.ClientRegistration;
|
98 | 102 | import org.springframework.security.oauth2.client.registration.ClientRegistrationRepository;
|
|
152 | 156 | import static org.mockito.ArgumentMatchers.anyString;
|
153 | 157 | import static org.mockito.ArgumentMatchers.eq;
|
154 | 158 | import static org.mockito.BDDMockito.given;
|
| 159 | +import static org.mockito.Mockito.atLeastOnce; |
155 | 160 | import static org.mockito.Mockito.mock;
|
156 | 161 | import static org.mockito.Mockito.never;
|
157 | 162 | import static org.mockito.Mockito.verify;
|
@@ -217,6 +222,33 @@ public void getWhenUsingDefaultsWithValidBearerTokenThenAcceptsRequest() throws
|
217 | 222 | // @formatter:on
|
218 | 223 | }
|
219 | 224 |
|
| 225 | + @Test |
| 226 | + public void getWhenCustomSecurityContextHolderStrategyThenUses() throws Exception { |
| 227 | + this.spring.register(RestOperationsConfig.class, DefaultConfig.class, BasicController.class, SecurityContextChangedListenerConfig.class).autowire(); |
| 228 | + mockRestOperations(jwks("Default")); |
| 229 | + String token = this.token("ValidNoScopes"); |
| 230 | + // @formatter:off |
| 231 | + this.mvc.perform(get("/").with(bearerToken(token))) |
| 232 | + .andExpect(status().isOk()) |
| 233 | + .andExpect(content().string("ok")); |
| 234 | + // @formatter:on |
| 235 | + verifyBean(SecurityContextHolderStrategy.class, atLeastOnce()).getContext(); |
| 236 | + } |
| 237 | + |
| 238 | + @Test |
| 239 | + public void getWhenSecurityContextHolderStrategyThenUses() throws Exception { |
| 240 | + this.spring.register(RestOperationsConfig.class, DefaultConfig.class, |
| 241 | + SecurityContextChangedListenerConfig.class, BasicController.class).autowire(); |
| 242 | + mockRestOperations(jwks("Default")); |
| 243 | + String token = this.token("ValidNoScopes"); |
| 244 | + // @formatter:off |
| 245 | + this.mvc.perform(get("/").with(bearerToken(token))) |
| 246 | + .andExpect(status().isOk()) |
| 247 | + .andExpect(content().string("ok")); |
| 248 | + // @formatter:on |
| 249 | + verifyBean(SecurityContextChangedListener.class, atLeastOnce()).securityContextChanged(any()); |
| 250 | + } |
| 251 | + |
220 | 252 | @Test
|
221 | 253 | public void getWhenUsingDefaultsInLambdaWithValidBearerTokenThenAcceptsRequest() throws Exception {
|
222 | 254 | this.spring.register(RestOperationsConfig.class, DefaultInLambdaConfig.class, BasicController.class).autowire();
|
@@ -1418,6 +1450,10 @@ private <T> T verifyBean(Class<T> beanClass) {
|
1418 | 1450 | return verify(this.spring.getContext().getBean(beanClass));
|
1419 | 1451 | }
|
1420 | 1452 |
|
| 1453 | + private <T> T verifyBean(Class<T> beanClass, VerificationMode mode) { |
| 1454 | + return verify(this.spring.getContext().getBean(beanClass), mode); |
| 1455 | + } |
| 1456 | + |
1421 | 1457 | private String json(String name) throws IOException {
|
1422 | 1458 | return resource(name + ".json");
|
1423 | 1459 | }
|
|
0 commit comments