try-catch response error

This commit is contained in:
Gao Wei 2022-06-14 13:57:11 +08:00
parent 6085cf9323
commit a510ac877c
4 changed files with 86 additions and 20 deletions

View File

@ -6,10 +6,13 @@ package cn.axzo.framework.auth;
* @date 2022/5/11 3:39 PM
**/
public class AuthException extends RuntimeException {
public AuthException(String msg) {
super(msg);
public AuthException(String msg, Exception e) {
super(msg, e);
}
public AuthException(String msg) {
super(msg);
}
public static void error(boolean condition, String msg) {
if (!condition) {
throw new AuthException(msg);

View File

@ -1,7 +1,11 @@
package cn.axzo.framework.auth.domain;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
public class TerminalInfo {
/**
@ -111,7 +115,8 @@ public class TerminalInfo {
private static Map<String, String> aliasMap = new HashMap<String, String>();
private static Map<String, String> aliasMap = new HashMap<>();
private static Map<String, List<String>> ntLegacyMap = new HashMap<>();
/**
* 企业工作台Web-总包
@ -209,6 +214,18 @@ public class TerminalInfo {
aliasMap.put(STR_TERMINAL_SCREEN, NT_SCREEN);
}
static {
for(Entry<String, String> entry : aliasMap.entrySet()) {
String nt = entry.getValue();
List<String> list = ntLegacyMap.get(nt);
if(list == null) {
list = new ArrayList<String>();
ntLegacyMap.put(nt, list);
}
list.add(entry.getKey());
}
}
private String rawTerminalString;
private String newTerminalString;
@ -275,6 +292,12 @@ public class TerminalInfo {
return NT_CM_APP_CM_WORKER.equals(newTerminalString);
}
/**
* 新代码逻辑不要用这个
* @param legacyTerminalString
* @return
*/
@Deprecated
public boolean isLegacy(String legacyTerminalString) {
if (legacyTerminalString == null)
return false;
@ -287,6 +310,18 @@ public class TerminalInfo {
return false;
}
/**
* 新代码逻辑不要用这个
* @return
*/
@Deprecated
public List<String> allNames() {
List<String> list = ntLegacyMap.get(this.newTerminalString);
if(list == null)
return Collections.emptyList();
return list;
}
/**
*
* @param terminal
@ -306,4 +341,9 @@ public class TerminalInfo {
TerminalInfo tm1 = new TerminalInfo(t1);
return tm1.isLegacy(t2);
}
public static List<String> allNamesOf(String terminal) {
TerminalInfo tm = new TerminalInfo(terminal);
return tm.allNames();
}
}

View File

@ -34,7 +34,18 @@ public class UserInfo {
private String realName;
private String username;
/**
* 实名认证的头像历史代码里的faceUrl也是它
*/
private String idFaceUrl;
/**
* 这个是新东西用户自定义的头像630以前没有过
* 估计是平台业务会用到
*/
private String avatarUrl;
/**
* 手机号
*/

View File

@ -158,15 +158,26 @@ public class ContextInfoBuilderAspect {
// 有的URI缺少部分header是正常的我就没必要guess有的有必要
String response = request.execute().body();
TypeReference<Map<String, Object>> mapTypeReference = new TypeReference<Map<String, Object>>() {
};
Map<String, Object> map = JSONUtil.toBean(response, mapTypeReference, false);
response = String.valueOf(map.get(AuthConstants.USER_INFO));
// 使用token获取用户信息
AuthException.error(CharSequenceUtil.isNotEmpty(response), "not find user by token from pudge");
return response;
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);
}
} catch (HttpException e) {
throw new AuthException("call pudge get/user error.");
throw new AuthException("call pudge get/user error. token=" + contextInfo.getToken());
}
}
@ -219,30 +230,31 @@ public class ContextInfoBuilderAspect {
contextInfo.setSystemAndDeviceInfo(sdInfo);
contextInfo.setToken(request.getHeader(AuthConstants.HEADER_TOKEN));
contextInfo.setTenantId(nullSafeParseLong(request.getHeader(AuthConstants.HEADER_TENANT_ID)));
contextInfo.setSaasTenantId(nullSafeParseLong(request.getHeader(AuthConstants.HEADER_SAAS_TENANT_ID)));
contextInfo.setOuId(nullSafeParseLong(request.getHeader(AuthConstants.HEADER_OU_ID)));
contextInfo.setWorkspaceId(nullSafeParseLong(request.getHeader(AuthConstants.HEADER_WORKSPACE_ID)));
String terminalHeader = request.getHeader(AuthConstants.HEADER_TERMINAL);
String terminalSession = StpUtil.getLoginDevice();
String terminal = terminalHeader;
if(null == terminalHeader || "".equals(terminalHeader)){
log.warn("请求缺少terminal headertoken={}。暂时使用session中的补齐terminal={}。", contextInfo.getToken(), terminalSession);
if (null == terminalHeader || "".equals(terminalHeader)) {
log.warn("请求缺少terminal headertoken={}。暂时使用session中的补齐terminal={}。", contextInfo.getToken(),
terminalSession);
terminal = terminalSession;
} else if (TerminalInfo.legacyEquals(terminalHeader, terminalSession)) {
log.warn("请求缺少terminal header与session中不一致token={}。暂时使用request中的terminal={}session terminal={}", contextInfo.getToken(), terminalHeader, terminalSession);
log.warn("请求缺少terminal header与session中不一致token={}。暂时使用request中的terminal={}session terminal={}",
contextInfo.getToken(), terminalHeader, terminalSession);
terminal = terminalSession;
}
contextInfo.setTerminalInfo(new TerminalInfo(terminal));
contextInfo.setVisitTo(request.getHeader(AuthConstants.VISIT_TO));
}
public void buildUserInfo(ContextInfo contextInfo, String authResultJson) {
log.info("buildUserInfo-->authResultJson:{},contextInfo:{}",authResultJson,JSONUtil.toJsonStr(contextInfo));
log.info("buildUserInfo-->authResultJson:{},contextInfo:{}", authResultJson, JSONUtil.toJsonStr(contextInfo));
UserInfo userInfo = new UserInfo();
TypeReference<Map<String, Object>> mapTypeReference = new TypeReference<Map<String, Object>>() {
};