|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2020 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.
|
|
20 | 20 | import org.springframework.context.annotation.Import;
|
21 | 21 | import org.springframework.context.annotation.ImportSelector;
|
22 | 22 | import org.springframework.core.type.AnnotationMetadata;
|
| 23 | +import org.springframework.security.oauth2.client.OAuth2AuthorizedClientManager; |
23 | 24 | import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProvider;
|
24 | 25 | import org.springframework.security.oauth2.client.OAuth2AuthorizedClientProviderBuilder;
|
25 | 26 | import org.springframework.security.oauth2.client.endpoint.OAuth2AccessTokenResponseClient;
|
|
33 | 34 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
34 | 35 |
|
35 | 36 | import java.util.List;
|
36 |
| -import java.util.Optional; |
37 | 37 |
|
38 | 38 | /**
|
39 | 39 | * {@link Configuration} for OAuth 2.0 Client support.
|
@@ -67,47 +67,69 @@ static class OAuth2ClientWebMvcSecurityConfiguration implements WebMvcConfigurer
|
67 | 67 | private ClientRegistrationRepository clientRegistrationRepository;
|
68 | 68 | private OAuth2AuthorizedClientRepository authorizedClientRepository;
|
69 | 69 | private OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> accessTokenResponseClient;
|
| 70 | + private OAuth2AuthorizedClientManager authorizedClientManager; |
70 | 71 |
|
71 | 72 | @Override
|
72 | 73 | public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
|
73 |
| - if (this.clientRegistrationRepository != null && this.authorizedClientRepository != null) { |
74 |
| - OAuth2AuthorizedClientProviderBuilder authorizedClientProviderBuilder = |
75 |
| - OAuth2AuthorizedClientProviderBuilder.builder() |
76 |
| - .authorizationCode() |
77 |
| - .refreshToken() |
78 |
| - .password(); |
79 |
| - if (this.accessTokenResponseClient != null) { |
80 |
| - authorizedClientProviderBuilder.clientCredentials(configurer -> |
81 |
| - configurer.accessTokenResponseClient(this.accessTokenResponseClient)); |
82 |
| - } else { |
83 |
| - authorizedClientProviderBuilder.clientCredentials(); |
84 |
| - } |
85 |
| - OAuth2AuthorizedClientProvider authorizedClientProvider = authorizedClientProviderBuilder.build(); |
86 |
| - DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager( |
87 |
| - this.clientRegistrationRepository, this.authorizedClientRepository); |
88 |
| - authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider); |
| 74 | + OAuth2AuthorizedClientManager authorizedClientManager = getAuthorizedClientManager(); |
| 75 | + if (authorizedClientManager != null) { |
89 | 76 | argumentResolvers.add(new OAuth2AuthorizedClientArgumentResolver(authorizedClientManager));
|
90 | 77 | }
|
91 | 78 | }
|
92 | 79 |
|
93 | 80 | @Autowired(required = false)
|
94 |
| - public void setClientRegistrationRepository(List<ClientRegistrationRepository> clientRegistrationRepositories) { |
| 81 | + void setClientRegistrationRepository(List<ClientRegistrationRepository> clientRegistrationRepositories) { |
95 | 82 | if (clientRegistrationRepositories.size() == 1) {
|
96 | 83 | this.clientRegistrationRepository = clientRegistrationRepositories.get(0);
|
97 | 84 | }
|
98 | 85 | }
|
99 | 86 |
|
100 | 87 | @Autowired(required = false)
|
101 |
| - public void setAuthorizedClientRepository(List<OAuth2AuthorizedClientRepository> authorizedClientRepositories) { |
| 88 | + void setAuthorizedClientRepository(List<OAuth2AuthorizedClientRepository> authorizedClientRepositories) { |
102 | 89 | if (authorizedClientRepositories.size() == 1) {
|
103 | 90 | this.authorizedClientRepository = authorizedClientRepositories.get(0);
|
104 | 91 | }
|
105 | 92 | }
|
106 | 93 |
|
107 |
| - @Autowired |
108 |
| - public void setAccessTokenResponseClient( |
109 |
| - Optional<OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest>> accessTokenResponseClient) { |
110 |
| - accessTokenResponseClient.ifPresent(client -> this.accessTokenResponseClient = client); |
| 94 | + @Autowired(required = false) |
| 95 | + void setAccessTokenResponseClient(OAuth2AccessTokenResponseClient<OAuth2ClientCredentialsGrantRequest> accessTokenResponseClient) { |
| 96 | + this.accessTokenResponseClient = accessTokenResponseClient; |
| 97 | + } |
| 98 | + |
| 99 | + @Autowired(required = false) |
| 100 | + void setAuthorizedClientManager(List<OAuth2AuthorizedClientManager> authorizedClientManagers) { |
| 101 | + if (authorizedClientManagers.size() == 1) { |
| 102 | + this.authorizedClientManager = authorizedClientManagers.get(0); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private OAuth2AuthorizedClientManager getAuthorizedClientManager() { |
| 107 | + if (this.authorizedClientManager != null) { |
| 108 | + return this.authorizedClientManager; |
| 109 | + } |
| 110 | + |
| 111 | + OAuth2AuthorizedClientManager authorizedClientManager = null; |
| 112 | + if (this.clientRegistrationRepository != null && this.authorizedClientRepository != null) { |
| 113 | + if (this.accessTokenResponseClient != null) { |
| 114 | + OAuth2AuthorizedClientProvider authorizedClientProvider = |
| 115 | + OAuth2AuthorizedClientProviderBuilder.builder() |
| 116 | + .authorizationCode() |
| 117 | + .refreshToken() |
| 118 | + .clientCredentials(configurer -> |
| 119 | + configurer.accessTokenResponseClient(this.accessTokenResponseClient)) |
| 120 | + .password() |
| 121 | + .build(); |
| 122 | + DefaultOAuth2AuthorizedClientManager defaultAuthorizedClientManager = |
| 123 | + new DefaultOAuth2AuthorizedClientManager( |
| 124 | + this.clientRegistrationRepository, this.authorizedClientRepository); |
| 125 | + defaultAuthorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider); |
| 126 | + authorizedClientManager = defaultAuthorizedClientManager; |
| 127 | + } else { |
| 128 | + authorizedClientManager = new DefaultOAuth2AuthorizedClientManager( |
| 129 | + this.clientRegistrationRepository, this.authorizedClientRepository); |
| 130 | + } |
| 131 | + } |
| 132 | + return authorizedClientManager; |
111 | 133 | }
|
112 | 134 | }
|
113 | 135 | }
|
0 commit comments