<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Spring Security的配置機(jī)制早就變了

          共 4205字,需瀏覽 9分鐘

           ·

          2022-05-12 22:32


          源?/?? ? ? ??文/?

          以前胖哥說過SecurityConfigurerAdapter會在即將發(fā)布的5.7版本作廢,從Spring Security 5.4版本開始會提供一個原型范圍的HttpSecurity來幫助我們構(gòu)建過濾器鏈SecurityFilterChain

           @Bean(HTTPSECURITY_BEAN_NAME)
          ?@Scope("prototype")
          ?HttpSecurity?httpSecurity()?throws?Exception?{
          ??WebSecurityConfigurerAdapter.LazyPasswordEncoder?passwordEncoder?=?new?WebSecurityConfigurerAdapter.LazyPasswordEncoder(
          ????this.context);
          ??AuthenticationManagerBuilder?authenticationBuilder?=?new?WebSecurityConfigurerAdapter.DefaultPasswordEncoderAuthenticationManagerBuilder(
          ????this.objectPostProcessor,?passwordEncoder);
          ??authenticationBuilder.parentAuthenticationManager(authenticationManager());
          ??HttpSecurity?http?=?new?HttpSecurity(this.objectPostProcessor,?authenticationBuilder,?createSharedObjects());
          ??//?@formatter:off
          ??http
          ???.csrf(withDefaults())
          ???.addFilter(new?WebAsyncManagerIntegrationFilter())
          ???.exceptionHandling(withDefaults())
          ???.headers(withDefaults())
          ???.sessionManagement(withDefaults())
          ???.securityContext(withDefaults())
          ???.requestCache(withDefaults())
          ???.anonymous(withDefaults())
          ???.servletApi(withDefaults())
          ???.apply(new?DefaultLoginPageConfigurer<>());
          ??http.logout(withDefaults());
          ??//?@formatter:on
          ??return?http;
          ?}

          這里會構(gòu)建基于原型的HttpSecurityBean,并且初始化了一些默認(rèn)配置供我們來使用。涉及Spring Security的日常開發(fā)都是圍繞這個類進(jìn)行的,所以這個類是學(xué)習(xí)Spring Security的重中之重。

          ?

          基于原型(prototype)的Spring Bean的一個典型應(yīng)用場景,

          基本配置

          日常我們使用的一些配置項如下:

          方法說明
          requestMatchers()SecurityFilterChain提供URL攔截策略,具體還提供了antMatchermvcMathcer
          openidLogin()用于基于?OpenId?的驗證
          headers()將安全標(biāo)頭添加到響應(yīng),比如說簡單的 XSS 保護(hù)
          cors()配置跨域資源共享( CORS )
          sessionManagement()配置會話管理
          portMapper()配置一個PortMapper(HttpSecurity#(getSharedObject(class))),其他提供SecurityConfigurer的對象使用?PortMapper?從 HTTP 重定向到 HTTPS 或者從 HTTPS 重定向到 HTTP。默認(rèn)情況下,Spring Security使用一個PortMapperImpl映射 HTTP 端口8080到 HTTPS 端口8443,HTTP 端口80到 HTTPS 端口443
          jee()配置基于容器的預(yù)認(rèn)證。在這種情況下,認(rèn)證由Servlet容器管理
          x509()配置基于x509的預(yù)認(rèn)證
          rememberMe配置“記住我”的驗證
          authorizeRequests()基于使用HttpServletRequest限制訪問
          requestCache()配置請求緩存
          exceptionHandling()配置錯誤處理
          securityContext()HttpServletRequests之間的SecurityContextHolder上設(shè)置SecurityContext的管理。當(dāng)使用WebSecurityConfigurerAdapter時,這將自動應(yīng)用
          servletApi()HttpServletRequest方法與在其上找到的值集成到SecurityContext中。當(dāng)使用WebSecurityConfigurerAdapter時,這將自動應(yīng)用
          csrf()添加 CSRF 支持,使用WebSecurityConfigurerAdapter時,默認(rèn)啟用
          logout()添加退出登錄支持。當(dāng)使用WebSecurityConfigurerAdapter時,這將自動應(yīng)用。默認(rèn)情況是,訪問URL”/ logout”,使HTTP Session無效來清除用戶,清除已配置的任何#rememberMe()身份驗證,清除SecurityContextHolder,然后重定向到/login?success
          anonymous()配置匿名用戶的表示方法。當(dāng)與WebSecurityConfigurerAdapter結(jié)合使用時,這將自動應(yīng)用。默認(rèn)情況下,匿名用戶將使用org.springframework.security.authentication.AnonymousAuthenticationToken表示,并包含角色?ROLE_ANONYMOUS
          authenticationManager()配置AuthenticationManager
          authenticationProvider()添加AuthenticationProvider
          formLogin()指定支持基于表單的身份驗證。如果未指定FormLoginConfigurer#loginPage(String),則將生成默認(rèn)登錄頁面
          oauth2Login()根據(jù)外部OAuth 2.0或OpenID Connect 1.0提供程序配置身份驗證
          oauth2Client()OAuth2.0 客戶端相關(guān)的配置
          oauth2ResourceServer()OAuth2.0資源服務(wù)器相關(guān)的配置
          requiresChannel()配置通道安全。為了使該配置有用,必須提供至少一個到所需信道的映射
          httpBasic()配置 Http Basic 驗證
          addFilter()添加一個已經(jīng)在內(nèi)置過濾器注冊表注冊過的過濾器實例或者子類
          addFilterBefore()在指定的Filter類之前添加過濾器
          addFilterAt()在指定的Filter類的位置添加過濾器
          addFilterAfter()在指定的Filter類的之后添加過濾器
          and()連接以上策略的連接器,用來組合安全策略。實際上就是”而且”的意思

          高級玩法

          新手建議先把上面的基本玩法有選擇的弄明白,然后有精力的話去研究下HttpSecurity的高級玩法。

          apply

          這個方法用來把其它的一些配置合并到當(dāng)前的配置中去,形成插件化,支持SecurityConfigurerAdapter或者SecurityConfigurer的實現(xiàn)。其實內(nèi)置的一些配置都是以這種形式集成到HttpSecurity中去的。例如文章開頭的配置中有默認(rèn)登錄頁面相關(guān)的配置:

          ?httpSecurity.apply(new?DefaultLoginPageConfigurer<>());
          胖哥就利用這個搞了一個支持小程序登錄和驗證碼登錄的擴(kuò)展?spring-security-login-extension

          objectPostProcessor

          配置一個自定義ObjectPostProcessor。ObjectPostProcessor可以改變某些配置內(nèi)部的機(jī)制,這些配置往往不直接對外提供操作接口。

          獲取、移除配置類

          getConfigurer用來獲取已經(jīng)apply的配置類;getConfigurers用來獲取已經(jīng)apply某個類型的所有配置類。這個現(xiàn)在是我最喜歡的自定義的方式。

          配置、獲取SharedObject

          SharedObject是在配置中進(jìn)行共享的一些對象,HttpSecurity共享了一些非常有用的對象可以供各個配置之間共享,比如AuthenticationManager。相關(guān)的方法有setSharedObjectgetSharedObject、getSharedObjects。

          獲取SecurityFilterChain

          HttpSecurity也提供了構(gòu)建目標(biāo)對象SecurityFilterChain的實例的方法。你可以通過build()來對配置進(jìn)行初次構(gòu)建;也可以通過getObject()來獲取已經(jīng)構(gòu)建的實例;甚至你可以使用getOrBuild()來進(jìn)行直接獲取實例或者構(gòu)建實例。

          所以新的配置都是這樣的:

          @Bean
          SecurityFilterChain?securityFilterChain?(HttpSecurity?http)?{
          ????http.cors();
          ????return?http.build();
          }
          ?

          記住每一個HttpSecurity只能被構(gòu)建成功一次。

          這一篇非常重要

          本篇東西非常重要,不是馬上就能掌握的,需要有些耐心,需要在使用和學(xué)習(xí)中總結(jié)和發(fā)現(xiàn)。


          end





          頂級程序員:topcoding

          做最好的程序員社區(qū):Java后端開發(fā)、Python、大數(shù)據(jù)、AI


          一鍵三連「分享」、「點贊」和「在看」



          瀏覽 70
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機(jī)掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  麻豆成人三级 | 豆花视频AⅤ一区二区三区 | 中日韩操逼视频 | 超碰在线欧美 | 18 精品 爽 视频 |