自定義 403 錯誤頁面


開始吧

1、 准備 403 錯誤頁面

<div class="layui-body">
	<!-- 內容主體區域 -->
	<div style="padding: 15px;">
		<h1>非常抱歉!您沒有訪問這個功能的權限!(回家照照鏡子)</h1>
		<h2>${message }</h2>
	</div>
</div>

2、 HttpSecurity對象配置跳轉頁面(全部代碼)

主要看步驟3的細節內容


    //重寫configure方法進行配置
    @Override
    protected void configure(HttpSecurity httpSecurity) throws Exception {

        httpSecurity
                //配置入口
                .authorizeRequests()                    //對請求進行授權
                .antMatchers("index.jsp", "/layui/**")  //針對 /index.jsp以及layui下的內容進行授權(對哪些資源開放)
                .permitAll()                            //授權的級別:可以無條件訪問(開放的級別)
                //以下是用來設置擁有什么角色才能訪問什么資源
                .antMatchers("/level1/**")   //設置匹配/level1/**的地址
                .hasRole("學徒")                         //要求具備“學徒”角色
                .antMatchers("/level2/**")
                .hasRole("大師")
                .antMatchers("/level3/**")
                .hasRole("宗師")
                .anyRequest()                           //任意請求
                .authenticated()                        //需要登陸后才可以訪問(如果此句代碼和上句代碼先調用,會把前面設置的角色代碼的設置覆蓋,導致角色代碼無效。所以要 先做具體小范圍設置,再做大范圍模糊設置。)
                //以下是用戶登錄方法實現
                .and()
                .formLogin()                            //設置未授權請求跳轉到登錄頁面
                .loginPage("/index.jsp")                //指定登錄頁面
                .loginProcessingUrl("/do/login.html")   //loginProcessingUrl()方法指定了登錄地址,就會覆蓋 loginPage()方法中設置的默認值 /index.jsp POST
                .permitAll()                            //為登錄頁面設置所有人都可以訪問
                .usernameParameter("loginAcct")         //定制登錄賬號的請求參數名
                .passwordParameter("userPswd")          //定制登錄密碼的請求參數名
                .defaultSuccessUrl("/main.html")        //設置登錄成功后默認前往的 URL 地址
                //以下是用戶注銷方法實現
                .and()
                .csrf()
                .disable()                              //禁用CSRF功能
                .logout()                               //開啟退出功能
                .logoutUrl("/do/logout.html")           //指定處理退出請求的URL地址
                .logoutSuccessUrl("/index.jsp")         //退出成功后前往的 URL 地址
                //以下是訪問不到后跳轉到自定義的頁面
                .and()
                .exceptionHandling()                        //出現異常后,方法入口
//                .accessDeniedPage("/to/no/auth/page.html")  //訪問被拒絕去的地方(系統默認自定義跳轉的方法)
                .accessDeniedHandler(new AccessDeniedHandler() {
                    @Override
                    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
                        //匿名內部類,重寫handle方法。自定義內容信息
                        httpServletRequest.setAttribute("message", e.getMessage() + "⭐⭐⭐自定義信息內容⭐⭐⭐我是訪問被拒絕去的地方,不是系統默認自定義跳轉的方法⭐⭐⭐");
                        httpServletRequest.getRequestDispatcher("/WEB-INF/views/no_auth.jsp").forward(httpServletRequest, httpServletResponse);
                    }
                })
        ;
    }

    //重寫另外一個父類的方法,來設置登錄系統的賬號密碼(單機版,不走數據庫)
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        //super.configure(auth);禁用默認規則

        auth
                //配置入口
                .inMemoryAuthentication()           //在內存身份驗證(單機版,不走數據庫)
                //設置第一個用戶
                .withUser("zhou")      //設置登錄賬號
                .password("123")                    //設置登錄密碼
                .roles("ADMIN")                     //設置角色
                //設置第二個用戶
                .and()
                .withUser("qiong")     //設置另一個登錄賬號
                .password("234")                     //設置另一個登錄密碼
                .authorities("SAVE", "DEIT")         //設置權限
                //設置第三個用戶
                .and()
                .withUser("yuan")
                .password("345")
                .roles("大師")
                .authorities("SAVE")
        ;
    }

3、 一共有兩種方式

第一種:系統默認自定義跳轉的方法。但是"/to/no/auth/page.html"這玩意兒得自己寫handler控制跳轉頁面位置

    .and()
    .exceptionHandling()                        //出現異常后,方法入口
    .accessDeniedPage("/to/no/auth/page.html")  //訪問被拒絕去的地方(系統默認自定義跳轉的方法)
	@RequestMapping("/to/no/auth/page.html")
	public String toNoAuthPage() {
		return "no_auth";
	}

第二種:自定義內容信息和跳轉位置。其實不難看出只要是帶Handler的都可以自定義!

    .and()
    .exceptionHandling()                        //出現異常后,方法入口
    .accessDeniedHandler(new AccessDeniedHandler() {
        @Override
        public void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {
            //匿名內部類,重寫handle方法。自定義內容信息
            httpServletRequest.setAttribute("message", e.getMessage() + "⭐⭐⭐自定義信息內容⭐⭐⭐我是訪問被拒絕去的地方,不是系統默認自定義跳轉的方法⭐⭐⭐");
            httpServletRequest.getRequestDispatcher("/WEB-INF/views/no_auth.jsp").forward(httpServletRequest, httpServletResponse);
        }
    })

測試成功圖


免責聲明!

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



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