🎨 Improving the code.

This commit is contained in:
yadong.zhang 2021-01-26 14:55:56 +08:00
parent 6077c2cf54
commit e8fdd0f3fe
3 changed files with 9 additions and 5 deletions

View File

@ -68,7 +68,7 @@ public class OAuthConfig extends AuthenticateConfig {
* "token" for requesting an access token (implicit grant) as described by Section 4.2.1 (<a href="https://tools.ietf.org/html/rfc6749#section-4.2.1" target="_blank">https://tools.ietf.org/html/rfc6749#section-4.2.1</a>),
* or a registered extension value as described by Section 8.4 (<a href="https://tools.ietf.org/html/rfc6749#section-8.4" target="_blank">https://tools.ietf.org/html/rfc6749#section-8.4</a>).
*/
private Oauth2ResponseType responseType = Oauth2ResponseType.code;
private Oauth2ResponseType responseType = Oauth2ResponseType.none;
/**
* The optional value is: {@code authorization_code}, {@code password}, {@code client_credentials}

View File

@ -23,6 +23,10 @@ package com.fujieid.jap.oauth2;
*/
public enum Oauth2ResponseType {
/**
* When authorization code mode or implicit authorization mode is not used, ResponseType needs to be set to {@code none}
*/
none,
/**
* Authorization Code Grant
*/

View File

@ -136,11 +136,12 @@ public class AccessTokenHelper {
params.put("grant_type", Oauth2GrantType.password.name());
params.put("username", oAuthConfig.getUsername());
params.put("password", oAuthConfig.getPassword());
params.put("client_id", oAuthConfig.getClientId());
params.put("client_secret", oAuthConfig.getClientSecret());
if (ArrayUtil.isNotEmpty(oAuthConfig.getScopes())) {
params.put("scope", String.join(Oauth2Const.SCOPE_SEPARATOR, oAuthConfig.getScopes()));
}
String query = URLUtil.buildQuery(params, StandardCharsets.UTF_8);
String url = oAuthConfig.getTokenUrl().concat("?").concat(query);
String url = oAuthConfig.getTokenUrl();
String tokenResponse = HttpUtil.post(url, params, false);
Map<String, Object> tokenMap = JsonUtil.toBean(tokenResponse, Map.class);
Oauth2Util.checkOauthResponse(tokenResponse, tokenMap, "Oauth2Strategy failed to get AccessToken.");
@ -164,8 +165,7 @@ public class AccessTokenHelper {
if (ArrayUtil.isNotEmpty(oAuthConfig.getScopes())) {
params.put("scope", String.join(Oauth2Const.SCOPE_SEPARATOR, oAuthConfig.getScopes()));
}
String query = URLUtil.buildQuery(params, StandardCharsets.UTF_8);
String url = oAuthConfig.getTokenUrl().concat("?").concat(query);
String url = oAuthConfig.getTokenUrl();
String tokenResponse = HttpUtil.post(url, params, false);
Map<String, Object> tokenMap = JsonUtil.toBean(tokenResponse, Map.class);