Compare commits
2 Commits
b446d327b1
...
91eab74af8
Author | SHA1 | Date |
---|---|---|
黄昕 | 91eab74af8 | |
黄昕 | 7149e5597b |
|
@ -5,6 +5,8 @@ import com.cf.cs.authentication.exception.CommonAuthException;
|
|||
import com.cf.cs.base.common.ConstValue;
|
||||
import com.cf.cs.base.common.ErrorCode;
|
||||
import com.cf.cs.base.config.JwtConfigure;
|
||||
import com.cf.cs.base.misc.ApiContextUtils;
|
||||
import com.cf.cs.base.misc.MessageUtil;
|
||||
import com.cf.cs.crypto.arithmetic.CryptoHelper;
|
||||
import com.cf.cs.database.pojo.entity.User;
|
||||
import com.cf.cs.database.service.UserDataBaseService;
|
||||
|
@ -108,13 +110,13 @@ public class JwtUtils {
|
|||
|
||||
return user;
|
||||
} catch (MalformedJwtException e) {
|
||||
throw new CommonAuthException(ErrorCode.ERR_TOKEN_KEY, "密钥算法或者密钥转换错误");
|
||||
throw new CommonAuthException(ErrorCode.ERR_TOKEN_KEY, MessageUtil.get("err.auth.key.convert", ApiContextUtils.getLanguare()));
|
||||
} catch (MissingClaimException e) {
|
||||
throw new CommonAuthException(ErrorCode.ERR_TOKEN_KEY, "密钥缺少校验数据");
|
||||
throw new CommonAuthException(ErrorCode.ERR_TOKEN_KEY, MessageUtil.get("err.auth.key.verify", ApiContextUtils.getLanguare()));
|
||||
} catch (ExpiredJwtException e) {
|
||||
throw new CommonAuthException(ErrorCode.ERR_TOKEN_KEY, "密钥已过期");
|
||||
throw new CommonAuthException(ErrorCode.ERR_TOKEN_KEY, MessageUtil.get("err.auth.key.timeout", ApiContextUtils.getLanguare()));
|
||||
} catch (JwtException e) {
|
||||
throw new CommonAuthException(ErrorCode.ERR_TOKEN_KEY, "密钥解析错误");
|
||||
throw new CommonAuthException(ErrorCode.ERR_TOKEN_KEY, MessageUtil.get("err.auth.key.prase", ApiContextUtils.getLanguare()));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -52,3 +52,7 @@ ERR_DECRYPT_AES256=AES256 decryption failure
|
|||
ERR_CRYPTO_KEY=Wrong secret key
|
||||
ERR_USER_ROLE_NOTEXISTS=The user role does not exist
|
||||
ERR_RESOURCE_USED=Resource used
|
||||
err.auth.key.convert=Key algorithm or key conversion error
|
||||
err.auth.key.verify=Key missing verification data
|
||||
err.auth.key.timeout=Key expired
|
||||
err.auth.key.prase=Key resolution error
|
|
@ -52,4 +52,8 @@ ERR_ENCRYPT_AES256=AES256\u52A0\u5BC6\u5931\u8D25
|
|||
ERR_DECRYPT_AES256=AES256\u89E3\u5BC6\u5931\u8D25
|
||||
ERR_CRYPTO_KEY=\u9519\u8BEF\u7684\u79D8\u94A5
|
||||
ERR_USER_ROLE_NOTEXISTS=\u7528\u6237\u89D2\u8272\u4E0D\u5B58\u5728
|
||||
ERR_RESOURCE_USED=\u8D44\u6E90\u88AB\u5360\u7528
|
||||
ERR_RESOURCE_USED=\u8D44\u6E90\u88AB\u5360\u7528
|
||||
err.auth.key.convert=\u5BC6\u94A5\u7B97\u6CD5\u6216\u8005\u5BC6\u94A5\u8F6C\u6362\u9519\u8BEF
|
||||
err.auth.key.verify=\u5BC6\u94A5\u7F3A\u5C11\u6821\u9A8C\u6570\u636E
|
||||
err.auth.key.timeout=\u5BC6\u94A5\u5DF2\u8FC7\u671F
|
||||
err.auth.key.prase=\u5BC6\u94A5\u89E3\u6790\u9519\u8BEF
|
|
@ -0,0 +1,129 @@
|
|||
package com.cf.cs.restful.exception;
|
||||
|
||||
|
||||
import com.cf.cs.base.common.ConstValue;
|
||||
import com.cf.cs.base.common.ErrorCode;
|
||||
import com.cf.cs.base.exception.CommonErrorCodeException;
|
||||
import com.cf.cs.base.misc.HelperUtils;
|
||||
import com.cf.cs.protocol.exception.SecurityProtocolException;
|
||||
import com.cf.cs.protocol.pojo.po.BaseRespStatus;
|
||||
import com.cf.cs.protocol.pojo.vo.ProtocolResp;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.MethodArgumentNotValidException;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.ResponseBody;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.servlet.NoHandlerFoundException;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
|
||||
/**
|
||||
* The type Controller exception handler.
|
||||
*
|
||||
* @author xajhuang @163.com
|
||||
*/
|
||||
@ControllerAdvice
|
||||
@Slf4j
|
||||
public class ControllerExceptionHandler {
|
||||
/**
|
||||
* Controller not found protocol resp.
|
||||
*
|
||||
* @param e the e
|
||||
* @return the protocol resp
|
||||
*/
|
||||
@ExceptionHandler(NoHandlerFoundException.class)
|
||||
@ResponseStatus(HttpStatus.NOT_FOUND)
|
||||
@ResponseBody
|
||||
public ProtocolResp<BaseRespStatus> controllerNotFound(NoHandlerFoundException e) {
|
||||
List<String> errMsg = new ArrayList<>();
|
||||
errMsg.add(e.getMessage());
|
||||
|
||||
return ProtocolResp.result(ErrorCode.ERR_NOSUCHITEM,
|
||||
HttpStatus.NOT_FOUND.value(),
|
||||
errMsg.toArray(new String[0]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Controller global exception protocol resp.
|
||||
*
|
||||
* @param e the e
|
||||
* @return the protocol resp
|
||||
*/
|
||||
@ExceptionHandler({MethodArgumentNotValidException.class})
|
||||
@ResponseBody
|
||||
public ProtocolResp<BaseRespStatus> controllerGlobalException(MethodArgumentNotValidException e) {
|
||||
AtomicInteger idx = new AtomicInteger();
|
||||
List<String> errMsg = e.getBindingResult()
|
||||
.getFieldErrors()
|
||||
.stream()
|
||||
.map(v -> idx.getAndIncrement() + ": " + v.getDefaultMessage())
|
||||
.toList();
|
||||
return ProtocolResp.result(ErrorCode.ERR_PARAMEXCEPTION,
|
||||
ErrorCode.ERR_PARAMEXCEPTION.getHttpCode(),
|
||||
errMsg.toArray(new String[0]));
|
||||
}
|
||||
|
||||
/**
|
||||
* Common error exception protocol resp.
|
||||
*
|
||||
* @param ex the ex
|
||||
* @return the protocol resp
|
||||
*/
|
||||
@ExceptionHandler({CommonErrorCodeException.class})
|
||||
@ResponseBody
|
||||
public ProtocolResp<BaseRespStatus> commonErrorException(Exception ex) {
|
||||
return ProtocolResp.result(ErrorCode.ERR_PARAMEXCEPTION,
|
||||
ErrorCode.ERR_PARAMEXCEPTION.getHttpCode(),
|
||||
new String[] {ex.getMessage()});
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle exception protocol resp.
|
||||
*
|
||||
* @param rsp the rsp
|
||||
* @param req the req
|
||||
* @param ex the ex
|
||||
* @return the protocol resp
|
||||
*/
|
||||
@ExceptionHandler(SecurityProtocolException.class)
|
||||
@ResponseBody
|
||||
public ProtocolResp<BaseRespStatus> handleException(HttpServletResponse rsp,
|
||||
HttpServletRequest req,
|
||||
SecurityProtocolException ex) {
|
||||
try {
|
||||
String reqType = req.getMethod();
|
||||
String reqPath = req.getRequestURI();
|
||||
String reqIp = req.getRemoteAddr();
|
||||
String reqToken = req.getHeader("Authorization");
|
||||
|
||||
if (reqToken != null && !reqToken.isEmpty()) {
|
||||
reqToken = reqToken.replace(ConstValue.STRING_HTTP_AUTH_HEAD, "");
|
||||
}
|
||||
log.error("""
|
||||
Interface [{}] request <{}> from {}, token = <{}>
|
||||
+++ Request: {}
|
||||
--- Exception information: {}""",
|
||||
reqType, reqPath, reqIp, reqToken, HelperUtils.inputStream2String(req.getInputStream()),
|
||||
ex.getMessage());
|
||||
} catch (Exception ignored) {
|
||||
// Do nothing...
|
||||
}
|
||||
|
||||
List<String> errMeg = new ArrayList<>();
|
||||
rsp.setStatus(ErrorCode.ERR_PARAMEXCEPTION.getHttpCode());
|
||||
|
||||
if (ex.getMessage() != null && !ex.getMessage().isEmpty()) {
|
||||
errMeg.add(ex.getErr().getStringValue() + ": " + ex.getDescription());
|
||||
}
|
||||
|
||||
return ProtocolResp.result(ErrorCode.ERR_PARAMEXCEPTION,
|
||||
ErrorCode.ERR_PARAMEXCEPTION.getHttpCode(),
|
||||
errMeg.toArray(new String[0]));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue