# SpringSecurity抛出异常但AccessDeniedHandler不生效

参考地址

https://blog.csdn.net/qq_45848700/article/details/122319021

在自定义SpringSecurity捕获的异常,发现配置生效了,但是就是没有进行捕捉,通过打印控制台后发现,异常被全局异常处理器给捕获了,并且直接return返回了

        //配置异常处理器
        http.exceptionHandling()
                .authenticationEntryPoint(authenticationEntryPoint)
                .accessDeniedHandler(accessDeniedHandler);

解决方法,在全局异常处理器中把改异常抛出

    /**
     * SpringSecurity抛出异常但AccessDeniedHandler不生效
     * 全局异常中捕获AccessDeniedException再抛出就可以被AccessDeniedHandler捕获到了
     * @param e
     * @throws AccessDeniedException
     */
    @ExceptionHandler(AccessDeniedException.class)
    public void accessDeniedException(AccessDeniedException e) throws AccessDeniedException {
        throw e;
    }
posted @ 2022-03-21 23:30  成强  阅读(1259)  评论(0编辑  收藏  举报