springboot+security拦截异常,进入异常页面

2020-12-07 14:58:52 浏览数 (1)

代码语言:javascript复制
package cn.**.controller;
import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;




@Controller
public class MyException implements ErrorController {

    private static Logger log = LoggerFactory.getLogger(MyException.class);

    @RequestMapping("/error")
    public String handleError(HttpServletRequest request) {
        log.info("进入异常跳转");
        Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");
        switch (statusCode) {
            case 404:
                log.info("404异常跳转");
                return "fail";
            case 403:
                log.info("403异常跳转");
                return "403";
            case 500:
                log.info("500异常跳转");
                return "/error/500";
            default:
                log.info("默认异常跳转");
                return "/error/404";
        }

    }

    @Override
    public String getErrorPath() {
        return "/error";
    }
}

0 人点赞