handle auth exception in AOP

This commit is contained in:
Gao Wei 2022-06-14 14:39:54 +08:00
parent 14e5d72285
commit 2800f3f5b3
3 changed files with 17 additions and 58 deletions

View File

@ -1,13 +1,11 @@
package cn.axzo.framework.auth.config;
import cn.axzo.framework.auth.service.AuthExceptionHandler;
import cn.axzo.framework.auth.service.ContextInfoBuilderAspect;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import cn.axzo.framework.auth.service.ContextInfoBuilderAspect;
@Configuration
@Import(AuthExceptionHandler.class)
public class ContextInfoConfiguration {
@Bean

View File

@ -1,48 +0,0 @@
package cn.axzo.framework.auth.service;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import cn.axzo.framework.auth.AuthException;
import cn.azxo.framework.common.model.CommonResponse;
import cn.azxo.framework.common.utils.LogUtil;
import cn.dev33.satoken.exception.NotLoginException;
@ResponseBody
@ControllerAdvice
public class AuthExceptionHandler {
/**
* 捕捉AuthException异常
*
* @param e
* @return
*/
@ExceptionHandler(AuthException.class)
public CommonResponse<Void> exceptionHandler(Exception e) {
return CommonResponse.error(e.getMessage());
}
@ExceptionHandler(NotLoginException.class)
public CommonResponse<Void> error(NotLoginException nle) {
LogUtil.error("NotLoginException: {}", nle.getMessage(), nle);
// 判断场景值定制化异常信息
String message = "";
if (nle.getType().equals(NotLoginException.NOT_TOKEN)) {
message = "未提供token";
} else if (nle.getType().equals(NotLoginException.INVALID_TOKEN)) {
message = "token无效";
} else if (nle.getType().equals(NotLoginException.TOKEN_TIMEOUT)) {
message = "token已过期";
} else if (nle.getType().equals(NotLoginException.BE_REPLACED)) {
message = "token已被顶下线";
} else if (nle.getType().equals(NotLoginException.KICK_OUT)) {
message = "token已被踢下线";
} else {
message = "当前会话未登录";
}
return CommonResponse.error(message);
}
}

View File

@ -28,6 +28,7 @@ import cn.axzo.framework.auth.domain.UserInfo;
import cn.axzo.framework.auth.domain.UserInfoMap;
import cn.axzo.framework.auth.enums.EnvEnum;
import cn.azxo.framework.common.logger.logback.PodNamespacePropertyDefiner;
import cn.azxo.framework.common.model.CommonResponse;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
@ -67,22 +68,33 @@ public class ContextInfoBuilderAspect {
}
AuthException.error(Objects.nonNull(httpRequest), "httpRequest cant be null, this is error");
Object[] args = parseContextInfoAndReturnArgs(httpRequest, pjp);
ContextInfo contextInfo = null;
try {
contextInfo = fillContextInfoByRequest(httpRequest);
} catch (Exception e) {
//一般情况是AuthException
return CommonResponse.error(e.getMessage());
}
try {
// 把ContextInfo放到ThreadLocal中
ContextInfoHolder.set(contextInfo);
Object[] args = parseContextInfoAndReturnArgs(httpRequest, pjp, contextInfo);
return pjp.proceed(args);
} catch (Exception e) {
return CommonResponse.error(e.getMessage());
} finally {
ContextInfoHolder.clear();
}
}
public Object[] parseContextInfoAndReturnArgs(HttpServletRequest request, ProceedingJoinPoint pjp) {
public Object[] parseContextInfoAndReturnArgs(HttpServletRequest request, ProceedingJoinPoint pjp, ContextInfo contextInfo) {
// 把ContextInfo注入到函数的入参中
MethodSignature methodSignature = (MethodSignature) pjp.getSignature();
Method method = methodSignature.getMethod();
Class<?>[] parameterTypes = method.getParameterTypes();
Object[] pjpArgs = pjp.getArgs();
ContextInfo contextInfo = fillContextInfoByRequest(request);
if (contextInfo == null)
return pjpArgs;
@ -128,9 +140,6 @@ public class ContextInfoBuilderAspect {
// 定制一些信息的处理
contextInfo.buildCustomInfoByRequest(request);
// 把ContextInfo放到ThreadLocal中
ContextInfoHolder.set(contextInfo);
return contextInfo;
}