huanghongfa
2020-12-20 def38240262b4403377d4c16beac3ea048f1e658
springcloud_k8s_panzhihuazhihuishequ/auth/src/main/java/com/panzhihua/auth/exceptions/GlobalExceptionCapture.java
New file
@@ -0,0 +1,68 @@
package com.panzhihua.auth.exceptions;
import com.panzhihua.common.exceptions.TokenException;
import com.panzhihua.common.exceptions.UnAuthenticationException;
import com.panzhihua.common.exceptions.UnAuthorizationException;
import com.panzhihua.common.model.vos.R;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
/**
 * @program: springcloud_k8s_panzhihuazhihuishequ
 * @description: 全局异常捕获
 * @author: huang.hongfa weixin hhf9596 qq 959656820
 * @create: 2020-11-25 14:36
 **/
@RestControllerAdvice
public class GlobalExceptionCapture {
    /**
     * 拦截捕捉自定义异常 TokenException.class
     * @param ex token 异常
     * @return R 401
     */
    @ExceptionHandler(value = TokenException.class)
    public R myErrorHandlerTokenException(TokenException ex) {
        return R.fail(ex.getCode(),ex.getMsg());
    }
    /**
     * 拦截捕捉自定义异常 UnAuthenticationException.class
     * @param ex 认证 异常
     * @return R 401
     */
    @ExceptionHandler(value = UnAuthenticationException.class)
    public R myErrorHandlerUnAuthenticationException(UnAuthenticationException ex) {
        return R.fail(ex.getCode(),ex.getMsg());
    }
    /**
     * 拦截捕捉自定义异常 UnAuthorizationException.class
     * @param ex 权限 异常
     * @return R 403
     */
    @ExceptionHandler(value = UnAuthorizationException.class)
    public R myErrorHandlerUnAuthorizationException(UnAuthenticationException ex) {
        return R.fail(ex.getCode(),ex.getMsg());
    }
    /**
     * securtity认证异常 AuthenticationException
     * @param ex 权限 异常
     * @return R 500
     */
    @ExceptionHandler(value = AuthenticationException.class)
    public R myErrorHandlerUnAuthorizationException(AuthenticationException ex) {
        return R.fail(ex.getMessage());
    }
    /**
     * 全局异常捕捉处理
     * @param ex 所有运行时异常
     * @return R 500
     */
    @ExceptionHandler(value = Exception.class)
    public R errorHandler(Exception ex) {
        return R.fail();
    }
}