fix some exception handling logic

This commit is contained in:
Gao Wei 2022-06-17 21:35:28 +08:00
parent 253dd6f79a
commit c02a0b3544

View File

@ -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<Map<String, Object>> mapTypeReference = new TypeReference<Map<String, Object>>() {
};
Map<String, Object> 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<Map<String, Object>> mapTypeReference = new TypeReference<Map<String, Object>>() {
};
Map<String, Object> 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;
}
}
}