From 56f70f90d6506543ed89f1d03befe30fee4fab2b Mon Sep 17 00:00:00 2001 From: zhou-hao Date: Tue, 5 Dec 2017 22:31:30 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96OAuth2.0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../web/authorization/oauth2/client/request/ReTry.java | 2 ++ .../oauth2/client/simple/request/SimpleOAuth2Request.java | 8 ++++++++ .../client/simple/request/SimpleOAuth2Response.java | 3 +-- .../web/oauth2/authorization/OAuth2UserTokenParser.java | 4 ++++ 4 files changed, 15 insertions(+), 2 deletions(-) diff --git a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/ReTry.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/ReTry.java index db57d4db0..461a97518 100644 --- a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/ReTry.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/request/ReTry.java @@ -19,9 +19,11 @@ package org.hswebframework.web.authorization.oauth2.client.request; + /** * @author zhouhao */ +@FunctionalInterface public interface ReTry { void doReTry(); } diff --git a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/SimpleOAuth2Request.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/SimpleOAuth2Request.java index 6480b53e3..d9a72756d 100644 --- a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/SimpleOAuth2Request.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/SimpleOAuth2Request.java @@ -154,6 +154,14 @@ public class SimpleOAuth2Request implements OAuth2Request { //返回重试后的response return auth2Response; }); + + //如果是invalid token 也将重新生成token + auth2Response.judgeError(ErrorType.INVALID_TOKEN,() -> { + //调用回调,并指定重试的操作(重新请求) + refreshTokenExpiredCallBack.call(() -> createNativeResponse(responseSupplier)); + //返回重试后的response + return auth2Response; + }); } return auth2Response; } diff --git a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/SimpleOAuth2Response.java b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/SimpleOAuth2Response.java index f1013af81..4c7b1c64f 100644 --- a/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/SimpleOAuth2Response.java +++ b/hsweb-authorization/hsweb-authorization-oauth2/hsweb-authorization-oauth2-client/src/main/java/org/hswebframework/web/authorization/oauth2/client/simple/request/SimpleOAuth2Response.java @@ -64,8 +64,7 @@ public class SimpleOAuth2Response implements OAuth2Response { if (type == ifError) { //重试后依然是相同的错误,可能是错误类型判断错误或者服务端的问题? - logger.error("still error [{}], maybe judge error or auth server error! response:{}" - ,ifError,retryResponse.asString()); + logger.error("still error [{}], maybe judge error or auth server error! {}",ifError,retryResponse,Thread.currentThread().getStackTrace()); } else { errorType = type; } diff --git a/hsweb-system/hsweb-system-oauth2-server/hsweb-system-oauth2-server-starter/src/main/java/org/hswebframework/web/oauth2/authorization/OAuth2UserTokenParser.java b/hsweb-system/hsweb-system-oauth2-server/hsweb-system-oauth2-server-starter/src/main/java/org/hswebframework/web/oauth2/authorization/OAuth2UserTokenParser.java index a389a6771..11537ea3c 100644 --- a/hsweb-system/hsweb-system-oauth2-server/hsweb-system-oauth2-server-starter/src/main/java/org/hswebframework/web/oauth2/authorization/OAuth2UserTokenParser.java +++ b/hsweb-system/hsweb-system-oauth2-server/hsweb-system-oauth2-server-starter/src/main/java/org/hswebframework/web/oauth2/authorization/OAuth2UserTokenParser.java @@ -28,10 +28,14 @@ public class OAuth2UserTokenParser implements UserTokenParser { @Override public ParsedToken parseToken(HttpServletRequest request) { + if (request.getRequestURI().contains("oauth2")&&request.getParameter(OAuth2Constants.grant_type) != null) { + return null; + } String accessToken = request.getHeader(OAuth2Constants.authorization); if (StringUtils.isEmpty(accessToken)) { accessToken = request.getParameter(OAuth2Constants.access_token); } else { + String[] arr = accessToken.split("[ ]"); if (arr.length > 1) { accessToken = arr[1];