11package 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 ;
35import org .springframework .beans .factory .annotation .Autowired ;
46import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
57import org .springframework .context .annotation .Bean ;
68import org .springframework .security .authentication .AuthenticationManager ;
79import org .springframework .security .crypto .bcrypt .BCryptPasswordEncoder ;
810import org .springframework .security .crypto .password .PasswordEncoder ;
11+ import org .springframework .security .oauth2 .common .OAuth2AccessToken ;
912import org .springframework .security .oauth2 .config .annotation .web .configuration .AuthorizationServerConfigurerAdapter ;
1013import org .springframework .security .oauth2 .config .annotation .web .configurers .AuthorizationServerEndpointsConfigurer ;
1114import 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 相关配置实现
2541 */
2642public 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}
0 commit comments