Register OAuth2AuthorizedClientArgumentResolver as custom resolver for XML config

Issue gh-8669
This commit is contained in:
Joe Grandja 2020-07-01 11:07:14 -04:00
parent 3e25714dc6
commit 0b5a14a900
3 changed files with 7 additions and 5 deletions

View File

@ -38,7 +38,7 @@ import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandl
* @since 5.4
*/
final class OAuth2ClientWebMvcSecurityPostProcessor implements BeanDefinitionRegistryPostProcessor, BeanFactoryAware {
private static final String ARGUMENT_RESOLVERS_PROPERTY = "argumentResolvers";
private static final String CUSTOM_ARGUMENT_RESOLVERS_PROPERTY = "customArgumentResolvers";
private BeanFactory beanFactory;
@Override
@ -56,7 +56,7 @@ final class OAuth2ClientWebMvcSecurityPostProcessor implements BeanDefinitionReg
BeanDefinition beanDefinition = registry.getBeanDefinition(beanName);
if (RequestMappingHandlerAdapter.class.getName().equals(beanDefinition.getBeanClassName())) {
PropertyValue currentArgumentResolvers =
beanDefinition.getPropertyValues().getPropertyValue(ARGUMENT_RESOLVERS_PROPERTY);
beanDefinition.getPropertyValues().getPropertyValue(CUSTOM_ARGUMENT_RESOLVERS_PROPERTY);
ManagedList<Object> argumentResolvers = new ManagedList<>();
if (currentArgumentResolvers != null) {
argumentResolvers.addAll((ManagedList<?>) currentArgumentResolvers.getValue());
@ -74,7 +74,7 @@ final class OAuth2ClientWebMvcSecurityPostProcessor implements BeanDefinitionReg
beanDefinitionBuilder.addConstructorArgReference(authorizedClientRepositoryBeanNames[0]);
}
argumentResolvers.add(beanDefinitionBuilder.getBeanDefinition());
beanDefinition.getPropertyValues().add(ARGUMENT_RESOLVERS_PROPERTY, argumentResolvers);
beanDefinition.getPropertyValues().add(CUSTOM_ARGUMENT_RESOLVERS_PROPERTY, argumentResolvers);
break;
}
}

View File

@ -41,6 +41,7 @@ import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
@ -226,7 +227,7 @@ public class OAuth2ClientBeanDefinitionParserTests {
static class AuthorizedClientController {
@GetMapping("/authorized-client")
String authorizedClient(@RegisteredOAuth2AuthorizedClient("google") OAuth2AuthorizedClient authorizedClient) {
String authorizedClient(Model model, @RegisteredOAuth2AuthorizedClient("google") OAuth2AuthorizedClient authorizedClient) {
return authorizedClient != null ? "resolved" : "not-resolved";
}
}

View File

@ -62,6 +62,7 @@ import org.springframework.security.web.savedrequest.RequestCache;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.ui.Model;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.bind.annotation.GetMapping;
@ -522,7 +523,7 @@ public class OAuth2LoginBeanDefinitionParserTests {
static class AuthorizedClientController {
@GetMapping("/authorized-client")
String authorizedClient(@RegisteredOAuth2AuthorizedClient("google") OAuth2AuthorizedClient authorizedClient) {
String authorizedClient(Model model, @RegisteredOAuth2AuthorizedClient("google") OAuth2AuthorizedClient authorizedClient) {
return authorizedClient != null ? "resolved" : "not-resolved";
}
}