add auth exception handler

This commit is contained in:
Gao Wei 2022-06-14 14:25:44 +08:00
parent a510ac877c
commit 14e5d72285
3 changed files with 51 additions and 1 deletions

View File

@ -1,10 +1,13 @@
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;
@Configuration
@Import(AuthExceptionHandler.class)
public class ContextInfoConfiguration {
@Bean

View File

@ -0,0 +1,48 @@
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

@ -33,7 +33,6 @@ import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.codec.Base64;
import cn.hutool.core.lang.TypeReference;
import cn.hutool.core.text.CharSequenceUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpException;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;