Skip to content

Commit b452407

Browse files
committed
ApiBoot Security Oauth 支持自定义Oauth Grant Type
1 parent 49348a0 commit b452407

File tree

8 files changed

+280
-21
lines changed

8 files changed

+280
-21
lines changed

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

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.minbox.framework.api.boot.autoconfigure.oauth;
1818

1919
import org.minbox.framework.api.boot.plugin.oauth.ApiBootAuthorizationServerConfiguration;
20+
import org.minbox.framework.api.boot.plugin.oauth.grant.ApiBootOauthTokenGranter;
21+
import org.springframework.beans.factory.ObjectProvider;
2022
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
2123
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2224
import org.springframework.boot.context.properties.EnableConfigurationProperties;
@@ -27,6 +29,8 @@
2729
import org.springframework.security.oauth2.provider.token.TokenStore;
2830
import org.springframework.security.oauth2.provider.token.store.InMemoryTokenStore;
2931

32+
import java.util.List;
33+
3034
import static org.minbox.framework.api.boot.autoconfigure.oauth.ApiBootOauthProperties.API_BOOT_OAUTH_PREFIX;
3135

3236

@@ -47,8 +51,8 @@
4751
@EnableAuthorizationServer
4852
@ConditionalOnProperty(prefix = API_BOOT_OAUTH_PREFIX, name = "away", havingValue = "memory", matchIfMissing = true)
4953
public class ApiBootAuthorizationMemoryServerAutoConfiguration extends ApiBootAuthorizationServerAutoConfiguration {
50-
public ApiBootAuthorizationMemoryServerAutoConfiguration(ApiBootOauthProperties apiBootOauthProperties) {
51-
super(apiBootOauthProperties);
54+
public ApiBootAuthorizationMemoryServerAutoConfiguration(ObjectProvider<List<ApiBootOauthTokenGranter>> objectProvider, ApiBootOauthProperties apiBootOauthProperties) {
55+
super(objectProvider, apiBootOauthProperties);
5256
}
5357

5458
@Override

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,16 @@
1717
package org.minbox.framework.api.boot.autoconfigure.oauth;
1818

1919
import org.minbox.framework.api.boot.plugin.oauth.ApiBootAuthorizationServerConfiguration;
20+
import org.minbox.framework.api.boot.plugin.oauth.grant.ApiBootOauthTokenGranter;
21+
import org.springframework.beans.factory.ObjectProvider;
2022
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
2123
import org.springframework.context.annotation.Bean;
2224
import org.springframework.security.oauth2.provider.token.AccessTokenConverter;
2325
import org.springframework.security.oauth2.provider.token.DefaultAccessTokenConverter;
2426
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
2527

28+
import java.util.List;
29+
2630
import static org.minbox.framework.api.boot.autoconfigure.oauth.ApiBootOauthProperties.API_BOOT_OAUTH_PREFIX;
2731

2832
/**
@@ -42,7 +46,8 @@ public class ApiBootAuthorizationServerAutoConfiguration extends ApiBootAuthoriz
4246
*/
4347
protected ApiBootOauthProperties apiBootOauthProperties;
4448

45-
public ApiBootAuthorizationServerAutoConfiguration(ApiBootOauthProperties apiBootOauthProperties) {
49+
public ApiBootAuthorizationServerAutoConfiguration(ObjectProvider<List<ApiBootOauthTokenGranter>> objectProvider, ApiBootOauthProperties apiBootOauthProperties) {
50+
super(objectProvider);
4651
this.apiBootOauthProperties = apiBootOauthProperties;
4752
}
4853

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717
package org.minbox.framework.api.boot.autoconfigure.oauth;
1818

1919
import org.minbox.framework.api.boot.plugin.oauth.ApiBootAuthorizationServerConfiguration;
20+
import org.minbox.framework.api.boot.plugin.oauth.grant.ApiBootOauthTokenGranter;
21+
import org.springframework.beans.factory.ObjectProvider;
2022
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
2123
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
2224
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
@@ -32,6 +34,8 @@
3234

3335
import javax.sql.DataSource;
3436

37+
import java.util.List;
38+
3539
import static org.minbox.framework.api.boot.autoconfigure.oauth.ApiBootOauthProperties.API_BOOT_OAUTH_PREFIX;
3640

3741
/**
@@ -52,11 +56,11 @@
5256
@ConditionalOnClass(ApiBootAuthorizationServerConfiguration.class)
5357
@ConditionalOnProperty(prefix = API_BOOT_OAUTH_PREFIX, name = "away", havingValue = "jdbc")
5458
@AutoConfigureAfter(DataSourceAutoConfiguration.class)
55-
public class ApiBootAuthorizationServerJdbcAutoConfiguration extends ApiBootAuthorizationServerAutoConfiguration{
59+
public class ApiBootAuthorizationServerJdbcAutoConfiguration extends ApiBootAuthorizationServerAutoConfiguration {
5660
private DataSource dataSource;
5761

58-
public ApiBootAuthorizationServerJdbcAutoConfiguration(ApiBootOauthProperties apiBootOauthProperties, DataSource dataSource) {
59-
super(apiBootOauthProperties);
62+
public ApiBootAuthorizationServerJdbcAutoConfiguration(ObjectProvider<List<ApiBootOauthTokenGranter>> objectProvider, ApiBootOauthProperties apiBootOauthProperties, DataSource dataSource) {
63+
super(objectProvider, apiBootOauthProperties);
6064
this.dataSource = dataSource;
6165
}
6266

api-boot-project/api-boot-maven-plugins/api-boot-mybatis-enhance-maven-codegen/src/main/java/org/minbox/framework/api/boot/maven/plugin/mybatis/enhance/codegen/builder/impl/AbstractClassBuilder.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public abstract class AbstractClassBuilder implements ClassBuilder {
5454
/**
5555
* Column Insertable Annotation
5656
*/
57-
public static final String COLUMN_INSERTABLE_ANNOTATION = "@Column(name = \"%s\",insertable = true)";
57+
public static final String COLUMN_INSERTABLE_ANNOTATION = "@Column(name = \"%s\",insertable = false)";
5858
/**
5959
* Id auto Annotation
6060
*/

api-boot-project/api-boot-maven-plugins/api-boot-mybatis-enhance-maven-codegen/src/main/java/org/minbox/framework/api/boot/maven/plugin/mybatis/enhance/codegen/builder/impl/EntityClassBuilder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public String getPrefixDir() {
136136
* get column annotation definition
137137
*
138138
* @param column column
139-
* @return
139+
* @return column annotation
140140
*/
141141
private String getColumnAnnotation(com.gitee.hengboy.builder.core.database.model.Column column) {
142142
// append content to @column(name=xxx after
@@ -154,7 +154,7 @@ private String getColumnAnnotation(com.gitee.hengboy.builder.core.database.model
154154
* int default value
155155
*
156156
* @param column column
157-
* @return
157+
* @return column default value
158158
*/
159159
private String getColumnDefaultValue(com.gitee.hengboy.builder.core.database.model.Column column) {
160160
String pattern = " = %s";
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,32 @@
11
package org.minbox.framework.api.boot.plugin.oauth;
22

3+
import org.minbox.framework.api.boot.plugin.oauth.grant.ApiBootOauthTokenGranter;
4+
import org.springframework.beans.factory.ObjectProvider;
35
import org.springframework.beans.factory.annotation.Autowired;
46
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
57
import org.springframework.context.annotation.Bean;
68
import org.springframework.security.authentication.AuthenticationManager;
79
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
810
import org.springframework.security.crypto.password.PasswordEncoder;
11+
import org.springframework.security.oauth2.common.OAuth2AccessToken;
912
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
1013
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
1114
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
12-
import org.springframework.security.oauth2.provider.token.AccessTokenConverter;
13-
import org.springframework.security.oauth2.provider.token.TokenStore;
15+
import org.springframework.security.oauth2.provider.*;
16+
import org.springframework.security.oauth2.provider.client.ClientCredentialsTokenGranter;
17+
import org.springframework.security.oauth2.provider.code.AuthorizationCodeServices;
18+
import org.springframework.security.oauth2.provider.code.AuthorizationCodeTokenGranter;
19+
import org.springframework.security.oauth2.provider.code.InMemoryAuthorizationCodeServices;
20+
import org.springframework.security.oauth2.provider.implicit.ImplicitTokenGranter;
21+
import org.springframework.security.oauth2.provider.password.ResourceOwnerPasswordTokenGranter;
22+
import org.springframework.security.oauth2.provider.refresh.RefreshTokenGranter;
23+
import org.springframework.security.oauth2.provider.request.DefaultOAuth2RequestFactory;
24+
import org.springframework.security.oauth2.provider.token.*;
25+
import org.springframework.security.oauth2.provider.token.store.JwtAccessTokenConverter;
26+
import org.springframework.util.ObjectUtils;
27+
28+
import java.util.ArrayList;
29+
import java.util.List;
1430

1531
/**
1632
* ApiBoot 集成Oauth2 相关配置实现
@@ -25,24 +41,36 @@
2541
*/
2642
public class ApiBootAuthorizationServerConfiguration extends AuthorizationServerConfigurerAdapter {
2743
/**
28-
* 认证管理
29-
* 整合SpringSecurity
44+
* authentication manager
3045
*/
3146
@Autowired
3247
private AuthenticationManager authenticationManager;
3348
/**
34-
* 令牌存储
49+
* Token Store
3550
*/
3651
@Autowired
3752
private TokenStore tokenStore;
3853
/**
39-
* 令牌转换
54+
* Access Token Converter
4055
*/
4156
@Autowired
4257
private AccessTokenConverter accessTokenConverter;
58+
/**
59+
* Oauth Client Detail Service
60+
*/
61+
@Autowired
62+
private ClientDetailsService clientDetailsService;
63+
/**
64+
* Instance of custom authorization provided by ApiBoot
65+
*/
66+
private List<ApiBootOauthTokenGranter> apiBootOauthTokenGranters;
67+
68+
public ApiBootAuthorizationServerConfiguration(ObjectProvider<List<ApiBootOauthTokenGranter>> objectProvider) {
69+
this.apiBootOauthTokenGranters = objectProvider.getIfAvailable();
70+
}
4371

4472
/**
45-
* 配置secret的加密方式与ApiBoot Security一致
73+
* Configure secret encryption in the same way as ApiBoot Security
4674
*
4775
* @param security AuthorizationServerSecurityConfigurer
4876
* @throws Exception 异常信息
@@ -51,15 +79,15 @@ public class ApiBootAuthorizationServerConfiguration extends AuthorizationServer
5179
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
5280
security
5381
.passwordEncoder(passwordEncoder())
54-
// 配置开放/oauth/token_key访问地址
82+
// Configure open/oauth/token_key access address
5583
.tokenKeyAccess("permitAll()")
56-
// 配置开放/oauth/check_token访问地址
57-
// 必须登录有权限后才可以访问
84+
// Configure Open /oauth/check_token Access Address
85+
// Access must be accessible after login privileges
5886
.checkTokenAccess("isAuthenticated()");
5987
}
6088

6189
/**
62-
* 配置整合SpringSecurity完成用户有效性认证
90+
* Configuration and Integration of Spring Security to Complete User Validity Authentication
6391
*
6492
* @param endpoints AuthorizationServerEndpointsConfigurer
6593
* @throws Exception 异常信息
@@ -69,11 +97,13 @@ public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws E
6997
endpoints
7098
.authenticationManager(authenticationManager)
7199
.tokenStore(tokenStore)
100+
// ApiBoot custom token granter
101+
.tokenGranter(tokenGranter())
72102
.accessTokenConverter(accessTokenConverter);
73103
}
74104

75105
/**
76-
* 用户登录或者获取Token时的密码加密方式
106+
* Password Encryption for Users Logging in or Obtaining Token
77107
*
78108
* @return BCryptPasswordEncoder
79109
*/
@@ -82,4 +112,86 @@ public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws E
82112
public PasswordEncoder passwordEncoder() {
83113
return new BCryptPasswordEncoder();
84114
}
115+
116+
/**
117+
* token granter
118+
*
119+
* @return TokenGranter
120+
*/
121+
private TokenGranter tokenGranter() {
122+
TokenGranter tokenGranter = new TokenGranter() {
123+
private CompositeTokenGranter delegate;
124+
125+
@Override
126+
public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
127+
if (delegate == null) {
128+
delegate = new CompositeTokenGranter(getDefaultTokenGranters());
129+
}
130+
return delegate.grant(grantType, tokenRequest);
131+
}
132+
};
133+
return tokenGranter;
134+
}
135+
136+
/**
137+
* token enhancer
138+
*
139+
* @return TokenEnhancer
140+
*/
141+
private TokenEnhancer tokenEnhancer() {
142+
if (accessTokenConverter instanceof JwtAccessTokenConverter) {
143+
return (TokenEnhancer) accessTokenConverter;
144+
}
145+
return null;
146+
}
147+
148+
private DefaultTokenServices tokenServices() {
149+
DefaultTokenServices tokenServices = new DefaultTokenServices();
150+
tokenServices.setTokenStore(tokenStore);
151+
tokenServices.setSupportRefreshToken(true);
152+
tokenServices.setReuseRefreshToken(true);
153+
tokenServices.setClientDetailsService(clientDetailsService);
154+
tokenServices.setTokenEnhancer(tokenEnhancer());
155+
return tokenServices;
156+
}
157+
158+
private AuthorizationCodeServices authorizationCodeServices() {
159+
return new InMemoryAuthorizationCodeServices();
160+
}
161+
162+
private OAuth2RequestFactory requestFactory() {
163+
return new DefaultOAuth2RequestFactory(clientDetailsService);
164+
}
165+
166+
/**
167+
* Return all granters within oauth2
168+
* Contains custom
169+
*
170+
* @return TokenGranter
171+
*/
172+
private List<TokenGranter> getDefaultTokenGranters() {
173+
ClientDetailsService clientDetails = clientDetailsService;
174+
AuthorizationServerTokenServices tokenServices = tokenServices();
175+
AuthorizationCodeServices authorizationCodeServices = authorizationCodeServices();
176+
OAuth2RequestFactory requestFactory = requestFactory();
177+
178+
List<TokenGranter> tokenGranters = new ArrayList<TokenGranter>();
179+
tokenGranters.add(new AuthorizationCodeTokenGranter(tokenServices, authorizationCodeServices, clientDetails,
180+
requestFactory));
181+
tokenGranters.add(new RefreshTokenGranter(tokenServices, clientDetails, requestFactory));
182+
ImplicitTokenGranter implicit = new ImplicitTokenGranter(tokenServices, clientDetails, requestFactory);
183+
tokenGranters.add(implicit);
184+
tokenGranters.add(new ClientCredentialsTokenGranter(tokenServices, clientDetails, requestFactory));
185+
if (authenticationManager != null) {
186+
tokenGranters.add(new ResourceOwnerPasswordTokenGranter(authenticationManager, tokenServices,
187+
clientDetails, requestFactory));
188+
}
189+
190+
// have custom token granter
191+
if (!ObjectUtils.isEmpty(apiBootOauthTokenGranters)) {
192+
apiBootOauthTokenGranters.stream().forEach(apiBootOauthTokenGranter -> tokenGranters.add(new DefaultApiBootOauthTokenGranter(tokenServices, clientDetailsService, requestFactory, apiBootOauthTokenGranter)));
193+
}
194+
195+
return tokenGranters;
196+
}
85197
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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.plugin.oauth;
19+
20+
import org.minbox.framework.api.boot.plugin.oauth.grant.ApiBootOauthTokenGranter;
21+
import org.springframework.security.oauth2.common.DefaultOAuth2AccessToken;
22+
import org.springframework.security.oauth2.common.OAuth2AccessToken;
23+
import org.springframework.security.oauth2.provider.ClientDetailsService;
24+
import org.springframework.security.oauth2.provider.OAuth2RequestFactory;
25+
import org.springframework.security.oauth2.provider.TokenRequest;
26+
import org.springframework.security.oauth2.provider.token.AbstractTokenGranter;
27+
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
28+
29+
import java.util.LinkedHashMap;
30+
import java.util.Map;
31+
32+
/**
33+
* default api boot oauth token granter
34+
*
35+
* @author:恒宇少年 - 于起宇
36+
* <p>
37+
* DateTime:2019-05-27 18:00
38+
* Blog:http://blog.yuqiyu.com
39+
* WebSite:http://www.jianshu.com/u/092df3f77bca
40+
* Gitee:https://gitee.com/hengboy
41+
* GitHub:https://github.com/hengboy
42+
*/
43+
public class DefaultApiBootOauthTokenGranter extends AbstractTokenGranter {
44+
/**
45+
* Instance of custom authorization provided by ApiBoot
46+
*/
47+
private ApiBootOauthTokenGranter apiBootOauthTokenGranter;
48+
49+
/**
50+
* instance default ApiBoot Oauth Token Granter
51+
*
52+
* @param tokenServices token service
53+
* @param clientDetailsService client detail service
54+
* @param requestFactory oauth2 request factory
55+
* @param apiBootOauthTokenGranter Instance of custom authorization provided by ApiBoot
56+
*/
57+
public DefaultApiBootOauthTokenGranter(AuthorizationServerTokenServices tokenServices, ClientDetailsService clientDetailsService, OAuth2RequestFactory requestFactory, ApiBootOauthTokenGranter apiBootOauthTokenGranter) {
58+
super(tokenServices, clientDetailsService, requestFactory, apiBootOauthTokenGranter.grantType());
59+
this.apiBootOauthTokenGranter = apiBootOauthTokenGranter;
60+
}
61+
62+
/**
63+
* grant access token
64+
*
65+
* @param grantType grant type
66+
* @param tokenRequest create token parameter
67+
* @return
68+
*/
69+
@Override
70+
public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
71+
// create token request parameters
72+
Map<String, String> parameters = new LinkedHashMap(tokenRequest.getRequestParameters());
73+
74+
// valid
75+
apiBootOauthTokenGranter.valid(parameters);
76+
77+
// create token
78+
OAuth2AccessToken token = super.grant(grantType, tokenRequest);
79+
if (token != null) {
80+
token = new DefaultOAuth2AccessToken(token);
81+
}
82+
return token;
83+
}
84+
}

0 commit comments

Comments
 (0)