麻豆黑色丝袜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
主站蜘蛛池模板: 亚洲国产成人久久精品app | 成年女人a毛片免费视频| 污污的网站在线免费观看| 中国陆超帅精瘦ktv直男少爷 | 性生活片免费看| 两人夜晚打扑克剧烈运动| 国产影片中文字幕| 欧美性69式xxxx护士| 午夜时刻免费入口| 亚洲国产美女精品久久久久| 中文字幕在线资源| 三奸在线看| 渣男渣女抹胸渣男渣女软件| 2021天天干| 久久精品国产99国产精品澳门| 最近最新好看的中文字幕2019| 天下第一社区视频welcome | 国产三级精品三级| 粗大的内捧猛烈进出在线视频| 久久成人国产精品一区二区| 又粗又硬又大又爽免费观看| 正在播放国产美人| 性一交一乱一视频免费看| 久久国产小视频| 午夜dj在线观看免费高清在线| 日本阿v视频在线观看高清| 黄页网站在线免费观看| 色欲香天天天综合网站| 色噜噜狠狠色综合免费视频| 欧美综合自拍亚洲综合图片区| 久爱免费观看在线网站| 中文字幕成人在线观看| 兽皇videos极品另类| 麻豆免费高清完整版视频| 午夜视频91| 大学寝室沈樵无删减| 久久天天躁日日躁狠狠躁| 欧美三级在线看中文字幕| 国产国产人免费人成成免视频| 国产精品久久久久免费a∨| 中文字幕一二三四区2021|