From e8fdd0f3fe70e4d1dca96ca0b5bba642fab1278c Mon Sep 17 00:00:00 2001 From: "yadong.zhang" Date: Tue, 26 Jan 2021 14:55:56 +0800 Subject: [PATCH] :art: Improving the code. --- .../src/main/java/com/fujieid/jap/oauth2/OAuthConfig.java | 2 +- .../java/com/fujieid/jap/oauth2/Oauth2ResponseType.java | 4 ++++ .../com/fujieid/jap/oauth2/helper/AccessTokenHelper.java | 8 ++++---- 3 files changed, 9 insertions(+), 5 deletions(-) diff --git a/jap-oauth2/src/main/java/com/fujieid/jap/oauth2/OAuthConfig.java b/jap-oauth2/src/main/java/com/fujieid/jap/oauth2/OAuthConfig.java index 8f1f43c..d1d457d 100644 --- a/jap-oauth2/src/main/java/com/fujieid/jap/oauth2/OAuthConfig.java +++ b/jap-oauth2/src/main/java/com/fujieid/jap/oauth2/OAuthConfig.java @@ -68,7 +68,7 @@ public class OAuthConfig extends AuthenticateConfig { * "token" for requesting an access token (implicit grant) as described by Section 4.2.1 (https://tools.ietf.org/html/rfc6749#section-4.2.1), * or a registered extension value as described by Section 8.4 (https://tools.ietf.org/html/rfc6749#section-8.4). */ - private Oauth2ResponseType responseType = Oauth2ResponseType.code; + private Oauth2ResponseType responseType = Oauth2ResponseType.none; /** * The optional value is: {@code authorization_code}, {@code password}, {@code client_credentials} diff --git a/jap-oauth2/src/main/java/com/fujieid/jap/oauth2/Oauth2ResponseType.java b/jap-oauth2/src/main/java/com/fujieid/jap/oauth2/Oauth2ResponseType.java index f37a842..e286233 100644 --- a/jap-oauth2/src/main/java/com/fujieid/jap/oauth2/Oauth2ResponseType.java +++ b/jap-oauth2/src/main/java/com/fujieid/jap/oauth2/Oauth2ResponseType.java @@ -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 */ diff --git a/jap-oauth2/src/main/java/com/fujieid/jap/oauth2/helper/AccessTokenHelper.java b/jap-oauth2/src/main/java/com/fujieid/jap/oauth2/helper/AccessTokenHelper.java index 147baaa..ff2109c 100644 --- a/jap-oauth2/src/main/java/com/fujieid/jap/oauth2/helper/AccessTokenHelper.java +++ b/jap-oauth2/src/main/java/com/fujieid/jap/oauth2/helper/AccessTokenHelper.java @@ -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 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 tokenMap = JsonUtil.toBean(tokenResponse, Map.class);