Spring security 自定义的AccessDeniedHandler无效,抛出AccessDeniedException 不允许访问

原因:全局异常处理比AccessDeniedHandler先捕获AccessDeniedException异常

处理办法:

在全局异常处向上抛出AccessDeniedException异常, 有AccessDeniedException捕获

    @ExceptionHandler(value =Exception.class)
    public ResultJson exceptionHandler(Exception e) throws Exception {
        //抛出AccessDeniedException异常
        if(e instanceof AccessDeniedException){
            throw e;
        }

        log.error("未知异常!原因是:",e);
        return ResultJson.error(CommonEnum.INTERNAL_SERVER_ERROR);
    }


 

你可能感兴趣的:(Java,springsecurity,spring,java)