From c02a0b35441f11aca2ad05d67f9d30aea871562b Mon Sep 17 00:00:00 2001 From: Gao Wei Date: Fri, 17 Jun 2022 21:35:28 +0800 Subject: [PATCH] fix some exception handling logic --- .../service/ContextInfoBuilderAspect.java | 74 ++++++++++--------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/axzo-auth-spring-boot-starter/src/main/java/cn/axzo/framework/auth/service/ContextInfoBuilderAspect.java b/axzo-auth-spring-boot-starter/src/main/java/cn/axzo/framework/auth/service/ContextInfoBuilderAspect.java index 08dc18b..7590205 100644 --- a/axzo-auth-spring-boot-starter/src/main/java/cn/axzo/framework/auth/service/ContextInfoBuilderAspect.java +++ b/axzo-auth-spring-boot-starter/src/main/java/cn/axzo/framework/auth/service/ContextInfoBuilderAspect.java @@ -83,9 +83,9 @@ public class ContextInfoBuilderAspect { try { ContextInfo contextInfo = new ContextInfo(); fillContextInfoWithRequestBeforeRemoteAuth(contextInfo, httpRequest); - + String authResultJson = remoteAuth(httpRequest, contextInfo); - + buildContextInfoByRemoteAuthResponse(contextInfo, authResultJson); // 定制一些信息的处理 @@ -122,7 +122,6 @@ public class ContextInfoBuilderAspect { } return pjpArgs; } - @SneakyThrows private void fillContextInfoWithRequestBeforeRemoteAuth(ContextInfo contextInfo, HttpServletRequest request) { @@ -149,8 +148,7 @@ public class ContextInfoBuilderAspect { contextInfo.setVisitTo(request.getHeader(AuthConstants.VISIT_TO)); } - ////以上都是还没有去remote auth之前的动作 - + //// 以上都是还没有去remote auth之前的动作 private String remoteAuth(HttpServletRequest httpRequest, ContextInfo contextInfo) { String authResultJson; @@ -173,7 +171,7 @@ public class ContextInfoBuilderAspect { } return authResultJson; } - + /** * 这个函数基本上都是在本地调试的时候才会走到,如果是apisix网关,不会走这个函数。 * @@ -188,6 +186,7 @@ public class ContextInfoBuilderAspect { String url = EnvEnum.getValueByKey(customerEnv); AuthException.error(CharSequenceUtil.isNotEmpty(url), "There is no correct path to request"); + String response = null; try { HttpRequest request = HttpRequest.get(url).header(AuthConstants.HEADER_TOKEN, contextInfo.getToken()) .header(AuthConstants.HEADER_TERMINAL, contextInfo.getTerminalInfo().getRawTerminalString()) @@ -203,30 +202,33 @@ public class ContextInfoBuilderAspect { // 有的URI,是归属于企业级的、有的归属于项目级,guess=newTerminalString // 有的URI,缺少部分header是正常的,我就没必要guess,有的有必要。 - String response = request.execute().body(); - - try { - TypeReference> mapTypeReference = new TypeReference>() { - }; - Map map = JSONUtil.toBean(response, mapTypeReference, false); - if (map.containsKey(AuthConstants.USER_INFO)) { - response = String.valueOf(map.get(AuthConstants.USER_INFO)); - // 使用token获取用户信息 - AuthException.error(CharSequenceUtil.isNotEmpty(response), "not find user by token from pudge"); - return response; - } else if (map.containsKey("error")) { - throw new AuthException(map.get("error").toString()); - } else { - throw new AuthException("无法解析鉴权认证请求的返回值,token=" + contextInfo.getToken()); - } - } catch (Exception e) { - throw new AuthException("解析鉴权认证请求出错, token=" + contextInfo.getToken(), e); - } + response = request.execute().body(); } catch (HttpException e) { throw new AuthException("call pudge get/user error. token=" + contextInfo.getToken()); } + + try { + TypeReference> mapTypeReference = new TypeReference>() { + }; + Map map = JSONUtil.toBean(response, mapTypeReference, false); + if (map.containsKey(AuthConstants.USER_INFO)) { + response = String.valueOf(map.get(AuthConstants.USER_INFO)); + // 使用token获取用户信息 + AuthException.error(CharSequenceUtil.isNotEmpty(response), "not find user by token from pudge"); + return response; + } else if (map.containsKey("error")) { + throw new AuthException(map.get("error").toString()); + } else { + throw new AuthException("无法解析鉴权认证请求的返回值,token=" + contextInfo.getToken()); + } + } catch (AuthException e) { + throw e; // re-throw + } catch (Exception e) { + throw new AuthException("解析鉴权认证请求出错, token=" + contextInfo.getToken(), e); + } + } - + /** * 期待着前端上线之后,header都补齐,这些代码可以全都删掉 * @@ -235,7 +237,8 @@ public class ContextInfoBuilderAspect { * @return */ @Deprecated - private String buildLegacyGuessMissedReqStringForRemoteAuth(ContextInfo contextInfo, HttpServletRequest originalRequest) { + private String buildLegacyGuessMissedReqStringForRemoteAuth(ContextInfo contextInfo, + HttpServletRequest originalRequest) { LegacyGuessMissedReq req = new LegacyGuessMissedReq(); req.setOriginalUrl(originalRequest.getRequestURI()); req.setHeaderOuId(contextInfo.getOuId()); @@ -247,8 +250,7 @@ public class ContextInfoBuilderAspect { return JSONUtil.toJsonStr(req); } - - ///以下都是 remote auth 之后的动作 + /// 以下都是 remote auth 之后的动作 public void buildContextInfoByRemoteAuthResponse(ContextInfo contextInfo, String authResultJson) { log.info("buildUserInfo-->authResultJson:{},contextInfo:{}", authResultJson, JSONUtil.toJsonStr(contextInfo)); @@ -296,8 +298,7 @@ public class ContextInfoBuilderAspect { contextInfo.setSaasTenantId(rsp.getSaasTenantId()); } if (LegacyGuessMissedRsp.ST_GUESS_OK.equals(rsp.getGuessTerminal())) { - contextInfo.setTerminalInfo( - new TerminalInfo(rsp.getNewTerminal())); + contextInfo.setTerminalInfo(new TerminalInfo(rsp.getNewTerminal())); } if (LegacyGuessMissedRsp.ST_GUESS_OK.equals(rsp.getGuessWorkspace())) { contextInfo.setWorkspaceId(rsp.getWorkspaceId()); @@ -309,9 +310,8 @@ public class ContextInfoBuilderAspect { } } - - ///以下都是工具函数 - + /// 以下都是工具函数 + public static Long nullSafeParseLong(String input) { if (input == null) return 0L; @@ -319,7 +319,11 @@ public class ContextInfoBuilderAspect { return 0L; if ("undefined".equals(input)) return 0L; - return Long.valueOf(input); + try { + return Long.valueOf(input); + } catch (NumberFormatException e) { + return 0L; + } } }