mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-08-29 03:06:45 +08:00
Migrate to OAuth2Client to use PropertyMapper
See gh-9018
This commit is contained in:
parent
ba86b68484
commit
1ea921bfdd
@ -18,12 +18,10 @@ package org.springframework.boot.autoconfigure.security.oauth2.client;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Provider;
|
||||
import org.springframework.boot.autoconfigure.security.oauth2.client.OAuth2ClientProperties.Registration;
|
||||
import org.springframework.boot.context.properties.PropertyMapper;
|
||||
import org.springframework.boot.context.properties.bind.convert.BinderConversionService;
|
||||
import org.springframework.core.convert.ConversionException;
|
||||
import org.springframework.security.config.oauth2.client.CommonOAuth2Provider;
|
||||
@ -56,16 +54,19 @@ final class OAuth2ClientPropertiesRegistrationAdapter {
|
||||
private static ClientRegistration getClientRegistration(String registrationId,
|
||||
Registration properties, Map<String, Provider> providers) {
|
||||
Builder builder = getBuilder(registrationId, properties.getProvider(), providers);
|
||||
copyIfNotNull(properties::getClientId, builder::clientId);
|
||||
copyIfNotNull(properties::getClientSecret, builder::clientSecret);
|
||||
copyIfNotNull(properties::getClientAuthenticationMethod,
|
||||
builder::clientAuthenticationMethod, ClientAuthenticationMethod::new);
|
||||
copyIfNotNull(properties::getAuthorizationGrantType,
|
||||
builder::authorizationGrantType, AuthorizationGrantType::new);
|
||||
copyIfNotNull(properties::getRedirectUriTemplate, builder::redirectUriTemplate);
|
||||
copyIfNotNull(properties::getScope, builder::scope,
|
||||
(scope) -> scope.toArray(new String[scope.size()]));
|
||||
copyIfNotNull(properties::getClientName, builder::clientName);
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
map.from(properties::getClientId).to(builder::clientId);
|
||||
map.from(properties::getClientSecret).to(builder::clientSecret);
|
||||
map.from(properties::getClientAuthenticationMethod)
|
||||
.as(ClientAuthenticationMethod::new)
|
||||
.to(builder::clientAuthenticationMethod);
|
||||
map.from(properties::getAuthorizationGrantType).as(AuthorizationGrantType::new)
|
||||
.to(builder::authorizationGrantType);
|
||||
map.from(properties::getRedirectUriTemplate).to(builder::redirectUriTemplate);
|
||||
map.from(properties::getScope)
|
||||
.as((scope) -> scope.toArray(new String[scope.size()]))
|
||||
.to(builder::scope);
|
||||
map.from(properties::getClientName).to(builder::clientName);
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
@ -95,11 +96,12 @@ final class OAuth2ClientPropertiesRegistrationAdapter {
|
||||
}
|
||||
|
||||
private static Builder getBuilder(Builder builder, Provider provider) {
|
||||
copyIfNotNull(provider::getAuthorizationUri, builder::authorizationUri);
|
||||
copyIfNotNull(provider::getTokenUri, builder::tokenUri);
|
||||
copyIfNotNull(provider::getUserInfoUri, builder::userInfoUri);
|
||||
copyIfNotNull(provider::getJwkSetUri, builder::jwkSetUri);
|
||||
copyIfNotNull(provider::getUserNameAttribute, builder::userNameAttributeName);
|
||||
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
|
||||
map.from(provider::getAuthorizationUri).to(builder::authorizationUri);
|
||||
map.from(provider::getTokenUri).to(builder::tokenUri);
|
||||
map.from(provider::getUserInfoUri).to(builder::userInfoUri);
|
||||
map.from(provider::getJwkSetUri).to(builder::jwkSetUri);
|
||||
map.from(provider::getUserNameAttribute).to(builder::userNameAttributeName);
|
||||
return builder;
|
||||
}
|
||||
|
||||
@ -113,16 +115,4 @@ final class OAuth2ClientPropertiesRegistrationAdapter {
|
||||
}
|
||||
}
|
||||
|
||||
private static <T> void copyIfNotNull(Supplier<T> supplier, Consumer<T> consumer) {
|
||||
copyIfNotNull(supplier, consumer, Function.identity());
|
||||
}
|
||||
|
||||
private static <S, C> void copyIfNotNull(Supplier<S> supplier, Consumer<C> consumer,
|
||||
Function<S, C> converter) {
|
||||
S value = supplier.get();
|
||||
if (value != null) {
|
||||
consumer.accept(converter.apply(value));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user