国产睡熟迷奷白丝护士系列精品,中文色字幕网站,免费h网站在线观看的,亚洲开心激情在线

      <sup id="hb9fh"></sup>
          1. 千鋒教育-做有情懷、有良心、有品質的職業(yè)教育機構

            手機站
            千鋒教育

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

            千鋒教育

            掃一掃進入千鋒手機站

            領取全套視頻
            千鋒教育

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

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

            HttpSecurity詳解

            來源:千鋒教育
            發(fā)布人: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類,并實現(xiàn)了SecurityBuilder和SecurityConfigurerAware接口。其中,requestMatcher方法用于匹配請求的URL,根據(jù)匹配結果來對請求進行處理。

            二、HttpSecurity的用法

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

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

            2、通過調用HttpSecurity的apply方法,并傳入第一步創(chuàng)建的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解密失敗怎么回事

            解密失敗可能是因為客戶端傳遞的數(shù)據(jù)被篡改或者密鑰不正確,當出現(xiàn)解密失敗的情況,可以使用以下代碼對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
            聲明:本站稿件版權均屬千鋒教育所有,未經(jīng)許可不得擅自轉載。
            10年以上業(yè)內強師集結,手把手帶你蛻變精英
            請您保持通訊暢通,專屬學習老師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