Skip to content

Commit 556afd8

Browse files
committed
ApiBoot Oauth 支持Redis存储token,支持多客户端配置,支持配置token过期时间
1 parent c5d2db5 commit 556afd8

File tree

6 files changed

+190
-14
lines changed

6 files changed

+190
-14
lines changed

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

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
import org.springframework.boot.context.properties.EnableConfigurationProperties;
2525
import org.springframework.context.annotation.Bean;
2626
import org.springframework.context.annotation.Configuration;
27+
import org.springframework.security.oauth2.config.annotation.builders.InMemoryClientDetailsServiceBuilder;
2728
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
2829
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
2930
import org.springframework.security.oauth2.provider.token.TokenStore;
@@ -35,7 +36,7 @@
3536

3637

3738
/**
38-
* ApiBoot 授权服务器内存方式实现
39+
* ApiBoot OAuth Memory Away Support
3940
*
4041
* @author:恒宇少年 - 于起宇
4142
* <p>
@@ -55,18 +56,25 @@ public ApiBootAuthorizationMemoryServerAutoConfiguration(ObjectProvider<List<Api
5556
super(objectProvider, apiBootOauthProperties);
5657
}
5758

59+
/**
60+
* configuration clients
61+
*
62+
* @param clients client details service configuration
63+
* @throws Exception exception
64+
*/
5865
@Override
5966
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
60-
clients.inMemory()
61-
.withClient(apiBootOauthProperties.getClientId())
62-
.authorizedGrantTypes(apiBootOauthProperties.getGrantTypes())
63-
.secret(passwordEncoder().encode(apiBootOauthProperties.getClientSecret()))
64-
.scopes(apiBootOauthProperties.getScopes())
65-
.resourceIds(apiBootOauthProperties.getResourceId());
67+
InMemoryClientDetailsServiceBuilder inMemoryClientDetailsServiceBuilder = clients.inMemory();
68+
apiBootOauthProperties.getClients().stream().forEach(client -> inMemoryClientDetailsServiceBuilder.withClient(client.getClientId())
69+
.secret(passwordEncoder().encode(client.getClientSecret()))
70+
.authorizedGrantTypes(client.getGrantTypes())
71+
.scopes(client.getScopes())
72+
.resourceIds(client.getResourceId())
73+
.accessTokenValiditySeconds(client.getAccessTokenValiditySeconds()));
6674
}
6775

6876
/**
69-
* 配置内存方式令牌存储
77+
* memory away token store
7078
*
7179
* @return TokenStore
7280
*/
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*
2+
* Copyright [2019] [恒宇少年 - 于起宇]
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*/
17+
18+
package org.minbox.framework.api.boot.autoconfigure.oauth;
19+
20+
import org.minbox.framework.api.boot.plugin.oauth.ApiBootAuthorizationServerConfiguration;
21+
import org.minbox.framework.api.boot.plugin.oauth.grant.ApiBootOauthTokenGranter;
22+
import org.springframework.beans.factory.ObjectProvider;
23+
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
24+
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
25+
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
26+
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
27+
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
28+
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
29+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
30+
import org.springframework.context.annotation.Bean;
31+
import org.springframework.context.annotation.Configuration;
32+
import org.springframework.data.redis.connection.RedisConnectionFactory;
33+
import org.springframework.data.redis.core.RedisTemplate;
34+
import org.springframework.security.oauth2.config.annotation.builders.InMemoryClientDetailsServiceBuilder;
35+
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
36+
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
37+
import org.springframework.security.oauth2.provider.token.TokenStore;
38+
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
39+
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;
40+
41+
import javax.sql.DataSource;
42+
43+
import java.util.List;
44+
45+
import static org.minbox.framework.api.boot.autoconfigure.oauth.ApiBootOauthProperties.API_BOOT_OAUTH_PREFIX;
46+
47+
/**
48+
* @author:恒宇少年 - 于起宇
49+
* <p>
50+
* DateTime:2019-07-13 09:35
51+
* Blog:http://blog.yuqiyu.com
52+
* WebSite:http://www.jianshu.com/u/092df3f77bca
53+
* Gitee:https://gitee.com/hengboy
54+
* GitHub:https://github.com/hengboy
55+
*/
56+
@Configuration
57+
@EnableConfigurationProperties(ApiBootOauthProperties.class)
58+
@EnableAuthorizationServer
59+
@ConditionalOnBean(RedisConnectionFactory.class)
60+
@ConditionalOnClass({ApiBootAuthorizationServerConfiguration.class})
61+
@ConditionalOnProperty(prefix = API_BOOT_OAUTH_PREFIX, name = "away", havingValue = "redis")
62+
@AutoConfigureAfter(RedisAutoConfiguration.class)
63+
public class ApiBootAuthorizationServerRedisAutoConfiguration extends ApiBootAuthorizationServerAutoConfiguration {
64+
/**
65+
* redis connection factory
66+
*/
67+
private RedisConnectionFactory redisConnectionFactory;
68+
69+
/**
70+
* constructor instance redis connection factory
71+
*
72+
* @param objectProvider ApiBoot Token Granter
73+
* @param apiBootOauthProperties ApiBoot Oauth Properties
74+
* @param redisConnectionFactory Redis Connection Factory
75+
*/
76+
public ApiBootAuthorizationServerRedisAutoConfiguration(ObjectProvider<List<ApiBootOauthTokenGranter>> objectProvider, ApiBootOauthProperties apiBootOauthProperties, RedisConnectionFactory redisConnectionFactory) {
77+
super(objectProvider, apiBootOauthProperties);
78+
this.redisConnectionFactory = redisConnectionFactory;
79+
}
80+
81+
/**
82+
* configuration clients
83+
*
84+
* @param clients client details service configuration
85+
* @throws Exception exception
86+
*/
87+
@Override
88+
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
89+
InMemoryClientDetailsServiceBuilder inMemoryClientDetailsServiceBuilder = clients.inMemory();
90+
apiBootOauthProperties.getClients().stream().forEach(client -> inMemoryClientDetailsServiceBuilder.withClient(client.getClientId())
91+
.secret(passwordEncoder().encode(client.getClientSecret()))
92+
.authorizedGrantTypes(client.getGrantTypes())
93+
.scopes(client.getScopes())
94+
.resourceIds(client.getResourceId())
95+
.accessTokenValiditySeconds(client.getAccessTokenValiditySeconds()));
96+
}
97+
98+
/**
99+
* Redis Token Store
100+
*
101+
* @return TokenStore
102+
*/
103+
@Bean
104+
public TokenStore redisTokenStore() {
105+
return new RedisTokenStore(redisConnectionFactory);
106+
}
107+
}

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

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
import org.springframework.boot.context.properties.ConfigurationProperties;
2222
import org.springframework.context.annotation.Configuration;
2323

24+
import java.util.ArrayList;
25+
import java.util.List;
26+
2427
import static org.minbox.framework.api.boot.autoconfigure.oauth.ApiBootOauthProperties.API_BOOT_OAUTH_PREFIX;
2528

2629
/**
@@ -48,30 +51,84 @@ public class ApiBootOauthProperties {
4851
* @see SecurityAway
4952
*/
5053
private SecurityAway away = SecurityAway.memory;
54+
5155
/**
5256
* Oauth2 clientId
57+
* 2.1.1. After the RELEASE version, the attribute is discarded and replaced by clients.
5358
*/
59+
@Deprecated
5460
private String clientId = "ApiBoot";
5561
/**
5662
* Oauth2 clientSecret
63+
* 2.1.1. After the RELEASE version, the attribute is discarded and replaced by clients.
5764
*/
65+
@Deprecated
5866
private String clientSecret = "ApiBootSecret";
5967
/**
6068
* 客户端授权类型集合
69+
* 2.1.1. After the RELEASE version, the attribute is discarded and replaced by clients.
6170
*/
71+
@Deprecated
6272
private String[] grantTypes = new String[]{"password", "refresh_token"};
6373
/**
6474
* 客户端作用域集合
75+
* 2.1.1. After the RELEASE version, the attribute is discarded and replaced by clients.
6576
*/
77+
@Deprecated
6678
private String[] scopes = new String[]{"api"};
6779
/**
6880
* 资源编号
81+
* 2.1.1. After the RELEASE version, the attribute is discarded and replaced by clients.
6982
*/
83+
@Deprecated
7084
private String resourceId = "api";
85+
7186
/**
7287
* 配置JWT格式化Oauth2返回的token
7388
*/
7489
private Jwt jwt = new Jwt();
90+
/**
91+
* configure multiple clients
92+
*/
93+
private List<Client> clients = new ArrayList() {{
94+
add(new Client());
95+
}};
96+
97+
/**
98+
* Oauth2 Client
99+
* Used to configure multiple clients
100+
*/
101+
@Data
102+
public static class Client {
103+
/**
104+
* oauth2 client id
105+
*/
106+
private String clientId = "ApiBoot";
107+
/**
108+
* oauth2 client secret
109+
*/
110+
private String clientSecret = "ApiBootSecret";
111+
/**
112+
* oauth2 client grant types
113+
* default value is "password,refresh_token"
114+
*/
115+
private String[] grantTypes = new String[]{"password", "refresh_token"};
116+
/**
117+
* oauth2 client scope
118+
* default value is "api"
119+
*/
120+
private String[] scopes = new String[]{"api"};
121+
/**
122+
* oauth2 application resource id
123+
* default value is "api"
124+
*/
125+
private String[] resourceId = new String[]{"api"};
126+
/**
127+
* oauth2 access token validity seconds
128+
* default value is 7200 second
129+
*/
130+
private int accessTokenValiditySeconds = 7200;
131+
}
75132

76133
/**
77134
* 自定义Jwt相关的配置

api-boot-project/api-boot-autoconfigure/src/main/java/org/minbox/framework/api/boot/autoconfigure/security/SecurityAway.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,5 +38,9 @@ public enum SecurityAway {
3838
/**
3939
* jdbc方式
4040
*/
41-
jdbc
41+
jdbc,
42+
/**
43+
* redis方式
44+
*/
45+
redis
4246
}

api-boot-project/api-boot-autoconfigure/src/main/resources/META-INF/spring.factories

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
55
org.minbox.framework.api.boot.autoconfigure.oauth.ApiBootResourceServerAutoConfiguration,\
66
org.minbox.framework.api.boot.autoconfigure.oauth.ApiBootAuthorizationMemoryServerAutoConfiguration,\
77
org.minbox.framework.api.boot.autoconfigure.oauth.ApiBootAuthorizationServerJdbcAutoConfiguration,\
8+
org.minbox.framework.api.boot.autoconfigure.oauth.ApiBootAuthorizationServerRedisAutoConfiguration,\
89
org.minbox.framework.api.boot.autoconfigure.converter.HttpMessageConverterAutoConfiguration,\
910
org.minbox.framework.api.boot.autoconfigure.oss.ApiBootOssAutoConfiguration,\
1011
org.minbox.framework.api.boot.autoconfigure.sms.ApiBootSmsAutoConfiguration,\

api-boot-project/api-boot-plugins/api-boot-plugin-security/src/main/java/org/minbox/framework/api/boot/plugin/security/userdetails/ApiBootUserDetailsService.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,11 +47,6 @@ public class ApiBootUserDetailsService implements UserDetailsService {
4747
*/
4848
@Autowired
4949
private ApplicationContext applicationContext;
50-
/**
51-
* ApiBoot数据委托类
52-
*/
53-
@Autowired
54-
private ApiBootStoreDelegate apiBootStoreDelegate;
5550

5651
/**
5752
* 根据用户名读取用户基本信息
@@ -65,6 +60,10 @@ public class ApiBootUserDetailsService implements UserDetailsService {
6560
@Override
6661
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
6762
logger.info("Login user:[{}]", username);
63+
64+
// find ApiBootStoreDelegate support instance
65+
// default is org.minbox.framework.api.boot.plugin.security.delegate.ApiBootDefaultStoreDelegate
66+
ApiBootStoreDelegate apiBootStoreDelegate = applicationContext.getBean(ApiBootStoreDelegate.class);
6867
UserDetails userDetails = apiBootStoreDelegate.loadUserByUsername(username);
6968

7069
// publish loadUserEvent

0 commit comments

Comments
 (0)