sprngboot2.0 + shiro + custom filter jwt abnormality global exception can not be intercepted

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/AinUser/article/details/100038270

Situation:

     Other local custom exception interception, are crippled, but the filter does not work, look for a couple of days, only to find the problem here

This is why I want to do, really scary

     Internet is not a lot of that intercept, and then I found a long time can be intercepted, but after the interception thrown is 500, not the custom exception

But the process can also be disguised, get to writing custom exception thrown once again packed thrown interception scheme is as follows

package com.read.data.bi_statistics.cms.exception;

import org.springframework.boot.autoconfigure.web.ErrorProperties;
import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.boot.web.servlet.error.ErrorAttributes;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
  * @author: tianyong
  * @time: 2019/8/21 10:46
  * @description:
  */
@RestControllerAdvice
public class ChainExceptionHandler extends BasicErrorController {


    public ChainExceptionHandler() {
        super(new DefaultErrorAttributes(), new ErrorProperties());
    }


    public ChainExceptionHandler(ErrorAttributes errorAttributes, ErrorProperties errorProperties) {
        super(errorAttributes, errorProperties);
    }


    public ChainExceptionHandler(ErrorAttributes errorAttributes, ErrorProperties errorProperties, List<ErrorViewResolver> errorViewResolvers) {
        super(errorAttributes, errorProperties, errorViewResolvers);
    }



    /**
      * @author: tianyong
      * @time: 2019/8/21 10:46
      * @description:处理传过来的异常(解决自定义全局异常捕获不到的问题)
      */
    @Override
    @RequestMapping(produces = {MediaType.APPLICATION_JSON_VALUE})
    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
        Map<String, Object> body = getErrorAttributes(request, isIncludeStackTrace(request, MediaType.ALL));
        HttpStatus status = getStatus(request);
        String code=body.get("status").toString();
        String message = body.get("message").toString();
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("code",code);
        map.put("msg",message);
        return new ResponseEntity<Map<String, Object>>(map, status);
    }

}

     Since I have a code inside for error status code, thrown exception error code will first come, will come after the interception of here, so

We did not use the above intercept program

package com.read.data.bi_statistics.cms.exception;

import com.read.data.bi_statistics.cms.data.enums.CodeInfo;
import com.read.data.bi_statistics.cms.entity.ReturnBone;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import static com.read.data.bi_statistics.authority.utils.JwtUtil.checkChar;
import static com.read.data.bi_statistics.cms.common.Utils.error;

/**
 * @author: tianyong
 * @Time: 2019/7/15 15:54
 * @description:状态码配置类
 */
@Controller
public class StatusCodeExceptionHandler implements ErrorController {


    /**
      * @author: tianyong
      * @time: 2019/7/16 11:36
      * @description:指定跳转路径/error
      */
    @Override
    public String getErrorPath() {
        return "/error";
    }

    /**
      * @author: tianyong
      * @time: 2019/7/16 11:37
      * @description:处理错误状态码
      */
    @RequestMapping("/error")
    public String handleError(HttpServletRequest request) {
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        Exception attribute = (Exception)request.getAttribute("javax.servlet.error.exception");
        if(statusCode == 400) {
            return "/400";
        }else if(statusCode == 401) {
            return "/401";
        }else if(statusCode == 403) {
            return "/403";
        }else if(statusCode == 404) {
            return "/404";
        }else if(statusCode == 405) {
            return "/405";
        }else if(statusCode == 500) {
            //处理全局异常无法捕获过滤器中自定义异常问题
            String message = attribute.getCause().getMessage();
            if(checkChar(message)){
                return "/401";
            }else{
                return "/500";
            }
        }else{
            return "/500";
        }
    }


    /**
      * @author: tianyong
      * @time: 2019/7/15 16:14
      * @description:400 请求无效
      */
    @ResponseBody
    @RequestMapping("/400")
    public ReturnBone error400 (){
        return error(CodeInfo.STATUS_CODE_400);
    }


    /**
      * @author: tianyong
      * @time: 2019/8/15 14:16
      * @description:401 用户认证失败
      */
    @ResponseBody
    @RequestMapping("/401")
    public ReturnBone error401 (){
        return error(CodeInfo.STATUS_CODE_401);
    }


    /**
     * @author: tianyong
     * @time: 2019/7/15 16:14
     * @description:403 禁止访问
     */
    @ResponseBody
    @RequestMapping("/403")
    public ReturnBone error403 (){
        return error(CodeInfo.STATUS_CODE_403);
    }


    /**
     * @author: tianyong
     * @time: 2019/7/15 16:14
     * @description:404 请求的网页不存在
     */
    @ResponseBody
    @RequestMapping("/404")
    public ReturnBone error404 (){
        return error(CodeInfo.STATUS_CODE_404);
    }


    /**
     * @author: tianyong
     * @time: 2019/7/15 16:14
     * @description:405 资源被禁止
     */
    @ResponseBody
    @RequestMapping("/405")
    public ReturnBone error405 (){
        return error(CodeInfo.STATUS_CODE_405);
    }


    /**
     * @author: tianyong
     * @time: 2019/7/15 16:14
     * @description:500 内部服务器错误,请联系管理员
     */
    @ResponseBody
    @RequestMapping("/500")
    public ReturnBone error500 (){
        return error(CodeInfo.STATUS_CODE_500);
    }

}

I last used the program are: first block of code, which the 500 error, and I and according to custom exception thrown in the text, the distinction between ordinary and abnormal 500

Then as long as there are characters jump 401, did not make a specific distinction, but you can also be based on specific text message and then encapsulation throw custom exception

if(authorization == null){
    loggers.error("获取token失败!");
    throw new AuthenticationException("获取token失败!");
}

Or there are ways, I see some other article does not address in the filter, but the interceptor inside processing requests -> Filter -> interceptor is the access order

Guess you like

Origin blog.csdn.net/AinUser/article/details/100038270