fix: 修改全局异常捕获处理类

This commit is contained in:
weihongyang 2022-06-22 09:57:39 +08:00
parent e130377e8c
commit 4fe4936dfd

View File

@ -1,34 +1,30 @@
package com.cnbm.admin.exception; package com.cnbm.admin.exception;
import cn.hutool.core.map.MapUtil;
import com.cnbm.admin.entity.SysLogErrorEntity;
import com.cnbm.admin.service.SysLogErrorService; import com.cnbm.admin.service.SysLogErrorService;
import com.cnbm.common.exception.ErrorCode; import com.cnbm.common.exception.ErrorCode;
import com.cnbm.common.exception.ExceptionUtils;
import com.cnbm.common.exception.RenException; import com.cnbm.common.exception.RenException;
import com.cnbm.common.utils.HttpContextUtils;
import com.cnbm.common.utils.IpUtils;
import com.cnbm.common.utils.JsonUtils;
import com.cnbm.common.utils.Result; import com.cnbm.common.utils.Result;
import lombok.extern.log4j.Log4j2;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException; import org.springframework.dao.DuplicateKeyException;
import org.springframework.http.HttpHeaders; import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.InternalAuthenticationServiceException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler; import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice; import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import java.util.Map;
/** /**
* @Author weihongyang * @Author weihongyang
* @Date 2022/6/10 2:05 PM * @Date 2022/6/10 2:05 PM
* @Version 1.0 * @Version 1.0
*/ */
@RestControllerAdvice @RestControllerAdvice
public class RenExceptionHandler { @Log4j2
private static final Logger logger = LoggerFactory.getLogger(RenExceptionHandler.class); public class YmExceptionHandler {
private static final Logger logger = LoggerFactory.getLogger(YmExceptionHandler.class);
@Autowired @Autowired
private SysLogErrorService sysLogErrorService; private SysLogErrorService sysLogErrorService;
@ -44,6 +40,35 @@ public class RenExceptionHandler {
return result; return result;
} }
@ExceptionHandler(BadCredentialsException.class)
public Result handleBadCredentialsException(BadCredentialsException ex){
Result result = new Result();
result.error("密码错误");
return result;
}
@ExceptionHandler(UsernameNotFoundException.class)
public Result handleUsernameNotFoundException(UsernameNotFoundException ex){
Result result = new Result();
result.error(ex.getMessage());
return result;
}
@ExceptionHandler(InternalAuthenticationServiceException.class)
public Result handleInternalAuthenticationServiceException(InternalAuthenticationServiceException ex){
Result result = new Result();
result.error(ex.getMessage());
return result;
}
@ExceptionHandler(LoginStatusException.class)
public Result handleLoginStatusException(LoginStatusException ex){
Result result = new Result();
result.error(ex.getMessage());
return result;
}
@ExceptionHandler(DuplicateKeyException.class) @ExceptionHandler(DuplicateKeyException.class)
public Result handleDuplicateKeyException(DuplicateKeyException ex){ public Result handleDuplicateKeyException(DuplicateKeyException ex){
Result result = new Result(); Result result = new Result();