欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

SpringBoot?Security權(quán)限控制自定義failureHandler實例

 更新時間:2022年11月13日 11:06:33   作者:EdurtIO  
這篇文章主要為大家介紹了SpringBoot?Security權(quán)限控制自定義failureHandler實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪

創(chuàng)建hander文件夾

在 java 源碼目錄下創(chuàng)建hander文件夾, 在該文件夾下創(chuàng)建CustomAuthenticationFailHander類文件

/**
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edurt.hander;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.WebAttributes;
import org.springframework.security.web.authentication.SimpleUrlAuthenticationFailureHandler;
import org.springframework.stereotype.Component;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
/**
 * CustomAuthenticationFailHander <br/>
 * 描述 : CustomAuthenticationFailHander <br/>
 * 作者 : qianmoQ <br/>
 * 版本 : 1.0 <br/>
 * 創(chuàng)建時間 : 2018-03-20 下午4:08 <br/>
 */
@Component(value = "customAuthenticationFailHander")
public class CustomAuthenticationFailHander extends SimpleUrlAuthenticationFailureHandler {
    @Override
    public void onAuthenticationFailure(HttpServletRequest request, HttpServletResponse response, AuthenticationException exception) throws IOException, ServletException {
        System.out.println("登錄失敗!!!");
        this.returnJson(response, exception);
    }
    /**
     * 直接返回需要返回的 json 數(shù)據(jù)
     */
    private void returnJson(HttpServletResponse response,
                            AuthenticationException exception) throws IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("application/json");
        response.getWriter().println("{\"ok\":0,\"msg\":\"" + exception.getLocalizedMessage() + "\"}");
    }
    /**
     * 直接返會錯誤頁面
     */
    private void returnErrorPage(HttpServletRequest request, HttpServletResponse response,
                                 AuthenticationException exception) throws IOException, ServletException {
        String strUrl = request.getContextPath() + "/loginErrorPath";
        request.getSession().setAttribute("status", 0);
        request.getSession().setAttribute("message", exception.getLocalizedMessage());
        request.getSession().setAttribute(WebAttributes.AUTHENTICATION_EXCEPTION, exception);
        // 使用該方法會出現(xiàn)錯誤
//        request.getRequestDispatcher(strUrl).forward(request, response);
        response.sendRedirect(strUrl);
    }
}

修改WebSecurityConfig配置

修改WebSecurityConfig配置文件支持自定義Handler

@Autowired
private CustomAuthenticationFailHander customAuthenticationFailHander;
@Override
protected void configure(HttpSecurity http) throws Exception {
    http.csrf().disable()
            // 允許直接訪問/路徑
            .authorizeRequests().antMatchers("/").permitAll()
            // 使其支持跨域
            .requestMatchers(CorsUtils::isPreFlightRequest).permitAll()
            // 其他路徑需要授權(quán)訪問
            .anyRequest().authenticated()
            // 指定登錄頁面
            .and().formLogin().loginPage("/user/login")
            // 指定登錄失敗跳轉(zhuǎn)地址, 使用自定義錯誤信息
            .failureHandler(customAuthenticationFailHander)
            // 登錄成功后的默認(rèn)路徑
            .defaultSuccessUrl("/").permitAll()
            // 退出登錄后的默認(rèn)路徑
            .and().logout().logoutSuccessUrl("/user/login").permitAll();
}

以上就是SpringBoot Security權(quán)限控制自定義failureHandler實例的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot Security failureHandler的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • RestTemplate get請求攜帶headers自動拼接參數(shù)方式

    RestTemplate get請求攜帶headers自動拼接參數(shù)方式

    這篇文章主要介紹了RestTemplate get請求攜帶headers自動拼接參數(shù)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • VSCode?配置?Spring?Boot?項目開發(fā)環(huán)境的全過程

    VSCode?配置?Spring?Boot?項目開發(fā)環(huán)境的全過程

    兩三年前曾經(jīng)試過配置Java環(huán)境, 存在不少問題作罷. 最近搜了下相關(guān)的文章, 感覺VSCode對Java項目的支持比三年前完善了不少. 今天實際配置了一下環(huán)境, 把自己常用的功能過了一遍, 基本能跑通開發(fā)流程, 做個筆記,需要的朋友可以參考下
    2024-03-03
  • Java實現(xiàn)圖片倒影的源碼實例內(nèi)容

    Java實現(xiàn)圖片倒影的源碼實例內(nèi)容

    在本篇文章里小編給大家整理的是關(guān)于Java實現(xiàn)圖片倒影的源碼以及相關(guān)知識點,有需要的朋友們學(xué)習(xí)下。
    2019-09-09
  • Java中繼承、多態(tài)、重載和重寫介紹

    Java中繼承、多態(tài)、重載和重寫介紹

    這篇文章主要介紹了Java中繼承、多態(tài)、重載和重寫介紹,需要的朋友可以參考下
    2014-07-07
  • Java畢業(yè)設(shè)計實戰(zhàn)之在線蛋糕銷售商城的實現(xiàn)

    Java畢業(yè)設(shè)計實戰(zhàn)之在線蛋糕銷售商城的實現(xiàn)

    這是一個使用了java+JSP+Springboot+maven+mysql+ThymeLeaf+FTP開發(fā)的在線蛋糕銷售商城,是一個畢業(yè)設(shè)計的實戰(zhàn)練習(xí),具有線上蛋糕商城該有的所有功能,感興趣的朋友快來看看吧
    2022-01-01
  • Retrofit+RxJava實現(xiàn)帶進(jìn)度下載文件

    Retrofit+RxJava實現(xiàn)帶進(jìn)度下載文件

    這篇文章主要為大家詳細(xì)介紹了Retrofit+RxJava實現(xiàn)帶進(jìn)度下載文件,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-05-05
  • Java 使用 HttpClient 發(fā)送 GET請求和 POST請求

    Java 使用 HttpClient 發(fā)送 GET請求和 POST請求

    本文主要介紹了Java 使用 HttpClient 發(fā)送 GET請求和 POST請求,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-08-08
  • XFire構(gòu)建web service客戶端的五種方式

    XFire構(gòu)建web service客戶端的五種方式

    本篇文章主要介紹了XFire構(gòu)建web service客戶端的五種方式。具有很好的參考價值,下面跟著小編一起來看下吧
    2017-01-01
  • MapReduce中ArrayWritable 使用指南

    MapReduce中ArrayWritable 使用指南

    MapReduce是一種編程模型,用于大規(guī)模數(shù)據(jù)集的并行運算。概念"Map(映射)"和"Reduce(歸約)"和他們的主要思想,都是從函數(shù)式編程語言里借來的,還有從矢量編程語言里借來的特性。他極大地方便了編程人員在不會分布式并行編程的情況下,將自己的程序運行在分布式系統(tǒng)上。
    2014-08-08
  • java五子棋小游戲?qū)崿F(xiàn)代碼

    java五子棋小游戲?qū)崿F(xiàn)代碼

    這篇文章主要為大家詳細(xì)介紹了java五子棋實現(xiàn)代碼,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-07-07

最新評論