Skip to content

Commit 245cce5

Browse files
committed
🚀 新增Redis方式集成OAuth2时客户端存储方式选择性配置
1 parent e90d020 commit 245cce5

File tree

4 files changed

+51
-18
lines changed

4 files changed

+51
-18
lines changed

api-boot-project/api-boot-autoconfigure/src/main/java/org/minbox/framework/api/boot/autoconfigure/oauth/ApiBootAuthorizationServerRedisAutoConfiguration.java

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
package org.minbox.framework.api.boot.autoconfigure.oauth;
1919

20+
import org.minbox.framework.api.boot.common.exception.ApiBootException;
2021
import org.minbox.framework.oauth.AuthorizationServerConfiguration;
2122
import org.minbox.framework.oauth.grant.OAuth2TokenGranter;
2223
import org.slf4j.Logger;
@@ -37,6 +38,7 @@
3738
import org.springframework.security.oauth2.provider.token.TokenStore;
3839
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
3940

41+
import javax.sql.DataSource;
4042
import java.util.List;
4143

4244
import static org.minbox.framework.api.boot.autoconfigure.oauth.ApiBootOauthProperties.API_BOOT_OAUTH_PREFIX;
@@ -62,17 +64,15 @@ public class ApiBootAuthorizationServerRedisAutoConfiguration extends ApiBootAut
6264
* redis connection factory
6365
*/
6466
private RedisConnectionFactory redisConnectionFactory;
67+
private DataSource dataSource;
6568

66-
/**
67-
* constructor instance redis connection factory
68-
*
69-
* @param objectProvider ApiBoot Token Granter
70-
* @param apiBootOauthProperties ApiBoot Oauth Properties
71-
* @param redisConnectionFactory Redis Connection Factory
72-
*/
73-
public ApiBootAuthorizationServerRedisAutoConfiguration(ObjectProvider<List<OAuth2TokenGranter>> objectProvider, ApiBootOauthProperties apiBootOauthProperties, RedisConnectionFactory redisConnectionFactory) {
69+
public ApiBootAuthorizationServerRedisAutoConfiguration(ObjectProvider<List<OAuth2TokenGranter>> objectProvider,
70+
ApiBootOauthProperties apiBootOauthProperties,
71+
ObjectProvider<RedisConnectionFactory> connectionFactoryProvider,
72+
ObjectProvider<DataSource> dataSourceProvider) {
7473
super(objectProvider, apiBootOauthProperties);
75-
this.redisConnectionFactory = redisConnectionFactory;
74+
this.redisConnectionFactory = connectionFactoryProvider.getIfAvailable();
75+
this.dataSource = dataSourceProvider.getIfAvailable();
7676
logger.info("ApiBoot Oauth2 initialize using redis.");
7777
}
7878

@@ -84,14 +84,24 @@ public ApiBootAuthorizationServerRedisAutoConfiguration(ObjectProvider<List<OAut
8484
*/
8585
@Override
8686
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
87-
InMemoryClientDetailsServiceBuilder inMemoryClientDetailsServiceBuilder = clients.inMemory();
88-
apiBootOauthProperties.getClients().stream().forEach(client -> inMemoryClientDetailsServiceBuilder.withClient(client.getClientId())
89-
.secret(passwordEncoder().encode(client.getClientSecret()))
90-
.authorizedGrantTypes(client.getGrantTypes())
91-
.scopes(client.getScopes())
92-
.resourceIds(client.getResourceId())
93-
.accessTokenValiditySeconds(client.getAccessTokenValiditySeconds())
94-
.refreshTokenValiditySeconds(client.getRefreshTokenValiditySeconds()));
87+
if (OAuthClientStorageAway.jdbc == apiBootOauthProperties.getClientStorageAway() && dataSource == null) {
88+
throw new ApiBootException("If you use jdbc to store the client, please instantiate the datasource.");
89+
}
90+
switch (apiBootOauthProperties.getClientStorageAway()) {
91+
case memory:
92+
InMemoryClientDetailsServiceBuilder inMemoryClientDetailsServiceBuilder = clients.inMemory();
93+
apiBootOauthProperties.getClients().stream().forEach(client -> inMemoryClientDetailsServiceBuilder.withClient(client.getClientId())
94+
.secret(passwordEncoder().encode(client.getClientSecret()))
95+
.authorizedGrantTypes(client.getGrantTypes())
96+
.scopes(client.getScopes())
97+
.resourceIds(client.getResourceId())
98+
.accessTokenValiditySeconds(client.getAccessTokenValiditySeconds())
99+
.refreshTokenValiditySeconds(client.getRefreshTokenValiditySeconds()));
100+
break;
101+
case jdbc:
102+
clients.jdbc(dataSource);
103+
break;
104+
}
95105
}
96106

97107
/**

api-boot-project/api-boot-autoconfigure/src/main/java/org/minbox/framework/api/boot/autoconfigure/oauth/ApiBootOauthProperties.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,12 @@ public class ApiBootOauthProperties {
4545
*
4646
* @see OAuthAway
4747
*/
48+
@Deprecated
4849
private OAuthAway away = OAuthAway.memory;
50+
/**
51+
* 配置OAuth2客户端列表存储方式
52+
*/
53+
private OAuthClientStorageAway clientStorageAway = OAuthClientStorageAway.memory;
4954
/**
5055
* Whether to generate a new token every time the "/oauth/token" interface is called
5156
* <p>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package org.minbox.framework.api.boot.autoconfigure.oauth;
2+
3+
/**
4+
* OAuth2的客户端存储方式
5+
*
6+
* @author 恒宇少年
7+
* @version 2.3.7
8+
*/
9+
public enum OAuthClientStorageAway {
10+
/**
11+
* Store client's config in memory
12+
*/
13+
memory,
14+
/**
15+
* Store client's config in jdbc
16+
*/
17+
jdbc,
18+
}

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<parent>
66
<groupId>org.minbox.framework</groupId>
77
<artifactId>minbox-parent</artifactId>
8-
<version>1.0.7-SNAPSHOT</version>
8+
<version>1.0.7</version>
99
<relativePath/>
1010
</parent>
1111
<packaging>pom</packaging>

0 commit comments

Comments
 (0)