Skip to content

Commit 49affeb

Browse files
committed
添加Oauth授权方式为自定义UserDetails验证
1 parent b452407 commit 49affeb

File tree

4 files changed

+70
-27
lines changed

4 files changed

+70
-27
lines changed

api-boot-project/api-boot-plugins/api-boot-plugin-oauth/src/main/java/org/minbox/framework/api/boot/plugin/oauth/ApiBootAuthorizationServerConfiguration.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.minbox.framework.api.boot.plugin.oauth;
22

33
import org.minbox.framework.api.boot.plugin.oauth.grant.ApiBootOauthTokenGranter;
4+
import org.minbox.framework.api.boot.plugin.oauth.grant.DefaultApiBootOauthTokenGranter;
45
import org.springframework.beans.factory.ObjectProvider;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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.exception;
19+
20+
import lombok.Getter;
21+
import org.springframework.security.core.AuthenticationException;
22+
23+
/**
24+
* ApiBoot Token Exception
25+
*
26+
* @author:恒宇少年 - 于起宇
27+
* <p>
28+
* DateTime:2019-05-28 11:00
29+
* Blog:http://blog.yuqiyu.com
30+
* WebSite:http://www.jianshu.com/u/092df3f77bca
31+
* Gitee:https://gitee.com/hengboy
32+
* GitHub:https://github.com/hengboy
33+
*/
34+
@Getter
35+
public class ApiBootTokenException extends AuthenticationException {
36+
public ApiBootTokenException(String msg) {
37+
super(msg);
38+
}
39+
40+
public ApiBootTokenException(String msg, Throwable t) {
41+
super(msg, t);
42+
}
43+
}

api-boot-project/api-boot-plugins/api-boot-plugin-oauth/src/main/java/org/minbox/framework/api/boot/plugin/oauth/grant/ApiBootOauthTokenGranter.java

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

1818
package org.minbox.framework.api.boot.plugin.oauth.grant;
1919

20-
import org.springframework.security.oauth2.common.exceptions.InvalidGrantException;
20+
import org.minbox.framework.api.boot.plugin.oauth.exception.ApiBootTokenException;
21+
import org.springframework.security.core.userdetails.UserDetails;
2122

2223
import java.util.Map;
2324

@@ -41,10 +42,12 @@ public interface ApiBootOauthTokenGranter {
4142
String grantType();
4243

4344
/**
44-
* Verify whether token can be generated
45+
* load userDetails by parameter
4546
*
46-
* @param parameters request parameters
47-
* @throws InvalidGrantException grant exception
47+
* @param parameters parameter map
48+
* @return UserDetails
49+
* @throws ApiBootTokenException
50+
* @see UserDetails
4851
*/
49-
void valid(Map<String, String> parameters) throws InvalidGrantException;
52+
UserDetails loadByParameter(Map<String, String> parameters) throws ApiBootTokenException;
5053
}
Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,13 @@
1515
*
1616
*/
1717

18-
package org.minbox.framework.api.boot.plugin.oauth;
18+
package org.minbox.framework.api.boot.plugin.oauth.grant;
1919

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;
20+
import org.springframework.security.authentication.AbstractAuthenticationToken;
21+
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
22+
import org.springframework.security.core.Authentication;
23+
import org.springframework.security.core.userdetails.UserDetails;
24+
import org.springframework.security.oauth2.provider.*;
2625
import org.springframework.security.oauth2.provider.token.AbstractTokenGranter;
2726
import org.springframework.security.oauth2.provider.token.AuthorizationServerTokenServices;
2827

@@ -60,25 +59,22 @@ public DefaultApiBootOauthTokenGranter(AuthorizationServerTokenServices tokenSer
6059
}
6160

6261
/**
63-
* grant access token
62+
* get oauth2 authentication
6463
*
65-
* @param grantType grant type
66-
* @param tokenRequest create token parameter
67-
* @return
64+
* @param client client detail
65+
* @param tokenRequest token request
66+
* @return oauth2 authentication
6867
*/
6968
@Override
70-
public OAuth2AccessToken grant(String grantType, TokenRequest tokenRequest) {
71-
// create token request parameters
72-
Map<String, String> parameters = new LinkedHashMap(tokenRequest.getRequestParameters());
69+
protected OAuth2Authentication getOAuth2Authentication(ClientDetails client, TokenRequest tokenRequest) {
70+
Map<String, String> parameters = new LinkedHashMap<String, String>(tokenRequest.getRequestParameters());
7371

74-
// valid
75-
apiBootOauthTokenGranter.valid(parameters);
72+
UserDetails userDetails = apiBootOauthTokenGranter.loadByParameter(parameters);
7673

77-
// create token
78-
OAuth2AccessToken token = super.grant(grantType, tokenRequest);
79-
if (token != null) {
80-
token = new DefaultOAuth2AccessToken(token);
81-
}
82-
return token;
74+
Authentication userAuth = new UsernamePasswordAuthenticationToken(userDetails, null, userDetails.getAuthorities());
75+
((AbstractAuthenticationToken) userAuth).setDetails(parameters);
76+
77+
OAuth2Request storedOAuth2Request = getRequestFactory().createOAuth2Request(client, tokenRequest);
78+
return new OAuth2Authentication(storedOAuth2Request, userAuth);
8379
}
8480
}

0 commit comments

Comments
 (0)