麻豆黑色丝袜jk制服福利网站-麻豆精品传媒视频观看-麻豆精品传媒一二三区在线视频-麻豆精选传媒4区2021-在线视频99-在线视频a

千鋒教育-做有情懷、有良心、有品質的職業教育機構

手機站
千鋒教育

千鋒學習站 | 隨時隨地免費學

千鋒教育

掃一掃進入千鋒手機站

領取全套視頻
千鋒教育

關注千鋒學習站小程序
隨時隨地免費學習課程

當前位置:首頁  >  技術干貨  > HttpSecurity詳解

HttpSecurity詳解

來源:千鋒教育
發布人:xqq
時間: 2023-11-23 14:27:15 1700720835

一、HttpSecurity源碼

HttpSecurity類是Spring Security核心配置類之一,用來配置Spring Security的行為,包括認證方式、權限控制、CSRF保護等。我們先來看一下HttpSecurity類的源碼結構:


@Configuration
public final class HttpSecurity extends
        AbstractConfiguredSecurityBuilder
        implements SecurityBuilder, SecurityConfigurerAware, HttpSecurity>, HttpSecurityBuilder {

    // 構造方法
    public HttpSecurity(ObjectPostProcessor objectPostProcessor, AuthenticationManagerBuilder builder,
                        Map, Object> sharedObjects) {
        super(objectPostProcessor, true, sharedObjects);
        // come code here...
    }

    // 配置方法
    public HttpSecurity requestMatcher(RequestMatcher requestMatcher) {
        // some code here...
        return this;
    }

    // some other methods...

}

如上代碼所示,HttpSecurity是一個@Configuration配置類,繼承了AbstractConfiguredSecurityBuilder類,并實現了SecurityBuilder和SecurityConfigurerAware接口。其中,requestMatcher方法用于匹配請求的URL,根據匹配結果來對請求進行處理。

二、HttpSecurity的用法

使用方法可以分為以下幾步:

1、創建一個SecurityConfigurer,并在其中重寫configure方法,實現對HttpSecurity的配置;

2、通過調用HttpSecurity的apply方法,并傳入第一步創建的SecurityConfigurer,來將SecurityConfigurer中的配置應用到HttpSecurity中;

3、配置成功后,HttpSecurity會返回一個DefaultSecurityFilterChain類型的對象,用于構建安全過濾器鏈。

三、HttpSecurity配置

3.1 HttpSecurity默認配置

當我們沒有對HttpSecurity進行額外的配置時,Spring Security會使用默認配置。默認配置包括以下內容:


http
    .authorizeRequests()
        .anyRequest().authenticated()
        .and()
    .formLogin()
        .and()
    .httpBasic();

以上代碼表示,所有請求需要進行身份驗證,并且要求用戶使用表單登錄或HTTP基本認證。

3.2 HttpSecurity權限配置詳解

針對不同路徑和請求類型,我們可以配置不同的授權策略。


http.authorizeRequests()
    .antMatchers("/admin/**").hasRole("ADMIN")
    .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
    .anyRequest().authenticated()
    .and()
.formLogin()
    .loginPage("/login")
    .permitAll()
    .and()
.logout()
    .permitAll();

以上代碼表示,對于"/admin/**"路徑下的請求,需要有"ADMIN"角色方可訪問;對于"/db/**"路徑下的請求,需要有"ADMIN"和"DBA"兩個角色方可訪問;而其他請求只需要在通過身份驗證后即可訪問。同時,我們還可以設置登錄頁和退出頁。

3.3 HttpSecurity解密失敗怎么回事

解密失敗可能是因為客戶端傳遞的數據被篡改或者密鑰不正確,當出現解密失敗的情況,可以使用以下代碼對HttpSecurity進行配置:


http.exceptionHandling()
    .authenticationEntryPoint(new Http403ForbiddenEntryPoint())
    .accessDeniedHandler(new AccessDeniedHandlerImpl());

以上代碼為設置Http403ForbiddenEntryPoint和AccessDeniedHandlerImpl類,用于處理解密失敗后的異常情況。

四、完整代碼示例

下面是一個完整的HttpSecurity配置示例:


@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests()
            .antMatchers("/admin/**").hasRole("ADMIN")
            .antMatchers("/db/**").access("hasRole('ADMIN') and hasRole('DBA')")
            .anyRequest().authenticated()
            .and()
        .formLogin()
            .loginPage("/login")
            .permitAll()
            .and()
        .logout()
            .permitAll()
            .and()
        .exceptionHandling()
            .authenticationEntryPoint(new Http403ForbiddenEntryPoint())
            .accessDeniedHandler(new AccessDeniedHandlerImpl());
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService);
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

以上代碼為一個完整的Spring Security配置類示例,其中包含了對HttpSecurity和AuthenticationManagerBuilder的配置。

tags: httpsecurity
聲明:本站稿件版權均屬千鋒教育所有,未經許可不得擅自轉載。
10年以上業內強師集結,手把手帶你蛻變精英
請您保持通訊暢通,專屬學習老師24小時內將與您1V1溝通
免費領取
今日已有369人領取成功
劉同學 138****2860 剛剛成功領取
王同學 131****2015 剛剛成功領取
張同學 133****4652 剛剛成功領取
李同學 135****8607 剛剛成功領取
楊同學 132****5667 剛剛成功領取
岳同學 134****6652 剛剛成功領取
梁同學 157****2950 剛剛成功領取
劉同學 189****1015 剛剛成功領取
張同學 155****4678 剛剛成功領取
鄒同學 139****2907 剛剛成功領取
董同學 138****2867 剛剛成功領取
周同學 136****3602 剛剛成功領取
相關推薦HOT
主站蜘蛛池模板: 午夜羞羞影院| 中国国语毛片免费观看视频| 好妻子韩国片在线| 免费被靠视频动漫| 日本护士xxxx视频免费| 国产精品久久久久久久久齐齐| 亚洲欧美日韩久久精品第一区| 全彩里番acg里番本子| 三级毛片在线| 亚洲国产成人久久综合区| 欧美日韩无线码在线观看| 狠狠色综合网站久久久久久久| 亚洲免费mv| 日韩欧美亚洲国产精品字幕久久久| 一本色道久久综合亚洲精品| 午夜剧场1000| 色国产精品一区在线观看| 国产乱码卡一卡2卡三卡四| 亚洲处破女| 国产午夜视频高清| 波多结衣一区二区三区| 久久国产精品-国产精品| 啦啦啦手机完整免费高清观看 | 性大毛片视频| 神宫寺奈绪jul055在线播放| 最近高清中文在线国语字幕| 国产精品久久久久久福利| 日本理论片午午伦夜理片2021| 国产精品人成在线播放新网站| 日韩av片无码一区二区不卡电影| 亚洲精品国产成人| 啊公交车坐最后一排被c视频| 果冻传媒电影免费看| 久久精品国产一区二区三区| 天天影院良辰美景好时光电视剧| 黄色一级片日本| 国产三级免费观看| 狠狠色噜噜狠狠狠合久| 日本videoshd高清黑人| 极品校花yin乱合集| 国产精品igao视频|