最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時間:2022年11月13日 11:06:33   作者:EdurtIO  
這篇文章主要為大家介紹了SpringBoot?Security權(quán)限控制自定義failureHandler實(shí)例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(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實(shí)例的詳細(xì)內(nèi)容,更多關(guān)于SpringBoot Security failureHandler的資料請關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • MyBatis-Plus找不到Mapper.xml文件的幾種解決方法

    MyBatis-Plus找不到Mapper.xml文件的幾種解決方法

    mybatis-plus今天遇到一個問題,就是mybatis 沒有讀取到mapper.xml 文件,所以下面這篇文章主要給大家介紹了關(guān)于MyBatis-Plus找不到Mapper.xml文件的幾種解決方法,需要的朋友可以參考下
    2022-06-06
  • Spring Boot 2.4版本前后的分組配置變化及對多環(huán)境配置結(jié)構(gòu)的影響(推薦)

    Spring Boot 2.4版本前后的分組配置變化及對多環(huán)境配置結(jié)構(gòu)的影響(推薦)

    這篇文章主要介紹了Spring Boot 2.4版本前后的分組配置變化及對多環(huán)境配置結(jié)構(gòu)的影響,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-12-12
  • JAVA項目如何打包部署到Linux服務(wù)器上

    JAVA項目如何打包部署到Linux服務(wù)器上

    本文詳細(xì)介紹了在服務(wù)器上部署環(huán)境包括JDK、MySQL、Tomcat的設(shè)置,以及使用Idea-Maven-SpringBoot進(jìn)行jar包打包部署的流程,內(nèi)容涵蓋了MySQL配置注意事項、pom.xml配置、打包命令等關(guān)鍵步驟,同時,也提供了如何將jar包上傳到Linux服務(wù)器并運(yùn)行的具體方法
    2024-10-10
  • Mybatis動態(tài)sql超詳細(xì)講解

    Mybatis動態(tài)sql超詳細(xì)講解

    動態(tài)SQL是MyBatis的強(qiáng)大特性之一,顧名思義就是會動的SQL,即是能夠靈活的根據(jù)某種條件拼接出完整的SQL語句,下面這篇文章主要給大家介紹了關(guān)于Mybatis動態(tài)sql的相關(guān)資料,需要的朋友可以參考下
    2023-04-04
  • Spring在SingleTon模式下的線程安全詳解

    Spring在SingleTon模式下的線程安全詳解

    這篇文章主要介紹了Spring在SingleTon模式下的線程安全詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-01-01
  • Springboot2 session設(shè)置超時時間無效的解決

    Springboot2 session設(shè)置超時時間無效的解決

    這篇文章主要介紹了Springboot2 session設(shè)置超時時間無效的解決方案,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-07-07
  • Java使用POI-TL和JFreeChart動態(tài)生成Word報告

    Java使用POI-TL和JFreeChart動態(tài)生成Word報告

    本文介紹了使用POI-TL和JFreeChart生成包含動態(tài)數(shù)據(jù)和圖表的Word報告的方法,并分享了實(shí)際開發(fā)中的踩坑經(jīng)驗,通過代碼示例講解的非常詳細(xì),具有一定的參考價值,需要的朋友可以參考下
    2025-02-02
  • Java servlet 使用 PrintWriter 時的編碼與亂碼的示例代碼

    Java servlet 使用 PrintWriter 時的編碼與亂碼的示例代碼

    本篇文章主要介紹了Java servlet 使用 PrintWriter 時的編碼與亂碼的示例代碼,探討了 PrintWriter 的缺省編碼與普通字符流的缺省編碼的差異,具有一定的參考價值,有興趣的可以了解一下
    2017-11-11
  • 關(guān)于MyBatis Plus中使用or和and問題

    關(guān)于MyBatis Plus中使用or和and問題

    這篇文章主要介紹了關(guān)于MyBatis Plus中使用or和and問題,需要的朋友可以參考下
    2020-12-12
  • 用Java實(shí)現(xiàn)簡單ATM機(jī)功能

    用Java實(shí)現(xiàn)簡單ATM機(jī)功能

    這篇文章主要為大家詳細(xì)介紹了用Java實(shí)現(xiàn)簡單ATM機(jī)功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-01-01

最新評論

大洼县| 辉南县| 青铜峡市| 改则县| 成安县| 漯河市| 新邵县| 龙州县| 阳谷县| 翁牛特旗| 安徽省| 白山市| 东宁县| 余江县| 四子王旗| 尚志市| 资讯 | 剑川县| 青田县| 吴忠市| 咸宁市| 乌兰浩特市| 邢台县| 惠州市| 涞水县| 双牌县| 奈曼旗| 灌阳县| 宜兰县| 修武县| 温宿县| 洪泽县| 义马市| 忻城县| 玉龙| 永顺县| 富平县| 宁安市| 含山县| 美姑县| 江口县|