# 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;
    }


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM