|
31 | 31 | import org.springframework.security.oauth2.core.TestOAuth2AccessTokens;
|
32 | 32 | import org.springframework.security.oauth2.core.TestOAuth2RefreshTokens;
|
33 | 33 | import org.springframework.security.oauth2.core.endpoint.OAuth2ParameterNames;
|
| 34 | +import org.springframework.util.StringUtils; |
34 | 35 |
|
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.Map; |
35 | 38 | import java.util.function.Function;
|
36 | 39 |
|
37 | 40 | import static org.assertj.core.api.Assertions.assertThat;
|
@@ -206,9 +209,17 @@ public void authorizeWhenRequestParameterUsernamePasswordThenMappedToContext() {
|
206 | 209 |
|
207 | 210 | when(this.authorizedClientProvider.authorize(any(OAuth2AuthorizationContext.class))).thenReturn(this.authorizedClient);
|
208 | 211 |
|
209 |
| - // Override the mock with the default |
210 |
| - this.authorizedClientManager.setContextAttributesMapper( |
211 |
| - new DefaultOAuth2AuthorizedClientManager.DefaultContextAttributesMapper()); |
| 212 | + // Set custom contextAttributesMapper |
| 213 | + this.authorizedClientManager.setContextAttributesMapper(authorizeRequest -> { |
| 214 | + Map<String, Object> contextAttributes = new HashMap<>(); |
| 215 | + String username = authorizeRequest.getServletRequest().getParameter(OAuth2ParameterNames.USERNAME); |
| 216 | + String password = authorizeRequest.getServletRequest().getParameter(OAuth2ParameterNames.PASSWORD); |
| 217 | + if (StringUtils.hasText(username) && StringUtils.hasText(password)) { |
| 218 | + contextAttributes.put(OAuth2AuthorizationContext.USERNAME_ATTRIBUTE_NAME, username); |
| 219 | + contextAttributes.put(OAuth2AuthorizationContext.PASSWORD_ATTRIBUTE_NAME, password); |
| 220 | + } |
| 221 | + return contextAttributes; |
| 222 | + }); |
212 | 223 |
|
213 | 224 | this.request.addParameter(OAuth2ParameterNames.USERNAME, "username");
|
214 | 225 | this.request.addParameter(OAuth2ParameterNames.PASSWORD, "password");
|
|
0 commit comments