久久精品国产亚洲高清|精品日韩中文乱码在线|亚洲va中文字幕无码久|伊人久久综合狼伊人久久|亚洲不卡av不卡一区二区|精品久久久久久久蜜臀AV|国产精品19久久久久久不卡|国产男女猛烈视频在线观看麻豆

    1. <style id="76ofp"></style>

      <style id="76ofp"></style>
      <rt id="76ofp"></rt>
      <form id="76ofp"><optgroup id="76ofp"></optgroup></form>
      1. 千鋒教育-做有情懷、有良心、有品質(zhì)的職業(yè)教育機構(gòu)

        手機站
        千鋒教育

        千鋒學(xué)習(xí)站 | 隨時隨地免費學(xué)

        千鋒教育

        掃一掃進入千鋒手機站

        領(lǐng)取全套視頻
        千鋒教育

        關(guān)注千鋒學(xué)習(xí)站小程序
        隨時隨地免費學(xué)習(xí)課程

        當(dāng)前位置:首頁  >  技術(shù)干貨  > HttpSecurity詳解

        HttpSecurity詳解

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

        一、HttpSecurity源碼

        HttpSecurity類是Spring Security核心配置類之一,用來配置Spring Security的行為,包括認(rèn)證方式、權(quán)限控制、CSRF保護等。我們先來看一下HttpSecurity類的源碼結(jié)構(gòu):

        
        @Configuration
        public final class HttpSecurity extends
                AbstractConfiguredSecurityBuilder
                implements SecurityBuilder, SecurityConfigurerAware, HttpSecurity>, HttpSecurityBuilder {
        
            // 構(gòu)造方法
            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類,并實現(xiàn)了SecurityBuilder和SecurityConfigurerAware接口。其中,requestMatcher方法用于匹配請求的URL,根據(jù)匹配結(jié)果來對請求進行處理。

        二、HttpSecurity的用法

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

        1、創(chuàng)建一個SecurityConfigurer,并在其中重寫configure方法,實現(xiàn)對HttpSecurity的配置;

        2、通過調(diào)用HttpSecurity的apply方法,并傳入第一步創(chuàng)建的SecurityConfigurer,來將SecurityConfigurer中的配置應(yīng)用到HttpSecurity中;

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

        三、HttpSecurity配置

        3.1 HttpSecurity默認(rèn)配置

        當(dāng)我們沒有對HttpSecurity進行額外的配置時,Spring Security會使用默認(rèn)配置。默認(rèn)配置包括以下內(nèi)容:

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

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

        3.2 HttpSecurity權(quán)限配置詳解

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

        
        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"兩個角色方可訪問;而其他請求只需要在通過身份驗證后即可訪問。同時,我們還可以設(shè)置登錄頁和退出頁。

        3.3 HttpSecurity解密失敗怎么回事

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

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

        以上代碼為設(shè)置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
        聲明:本站稿件版權(quán)均屬千鋒教育所有,未經(jīng)許可不得擅自轉(zhuǎn)載。
        10年以上業(yè)內(nèi)強師集結(jié),手把手帶你蛻變精英
        請您保持通訊暢通,專屬學(xué)習(xí)老師24小時內(nèi)將與您1V1溝通
        免費領(lǐng)取
        今日已有369人領(lǐng)取成功
        劉同學(xué) 138****2860 剛剛成功領(lǐng)取
        王同學(xué) 131****2015 剛剛成功領(lǐng)取
        張同學(xué) 133****4652 剛剛成功領(lǐng)取
        李同學(xué) 135****8607 剛剛成功領(lǐng)取
        楊同學(xué) 132****5667 剛剛成功領(lǐng)取
        岳同學(xué) 134****6652 剛剛成功領(lǐng)取
        梁同學(xué) 157****2950 剛剛成功領(lǐng)取
        劉同學(xué) 189****1015 剛剛成功領(lǐng)取
        張同學(xué) 155****4678 剛剛成功領(lǐng)取
        鄒同學(xué) 139****2907 剛剛成功領(lǐng)取
        董同學(xué) 138****2867 剛剛成功領(lǐng)取
        周同學(xué) 136****3602 剛剛成功領(lǐng)取
        相關(guān)推薦HOT
        株洲县| 安陆市| 东阳市| 修文县| 白城市| 建平县| 谷城县| 民权县| 和林格尔县| 土默特左旗| 禄丰县| 晋城| 宁津县| 上虞市| 克拉玛依市| 江山市| 德州市| 台中市| 马关县| 武隆县| 邓州市| 吴旗县| 清镇市| 五常市| 布拖县| 镇巴县| 四川省| 大荔县| 莎车县| 江山市| 秦安县| 黄大仙区| 莱芜市| 邵武市| 牟定县| 泰兴市| 泗洪县| 栾城县| 渝中区| 陇南市| 宁南县|