亚洲视频二区_亚洲欧洲日本天天堂在线观看_日韩一区二区在线观看_中文字幕不卡一区

公告:魔扣目錄網(wǎng)為廣大站長提供免費(fèi)收錄網(wǎng)站服務(wù),提交前請(qǐng)做好本站友鏈:【 網(wǎng)站目錄:http://www.430618.com 】, 免友鏈快審服務(wù)(50元/站),

點(diǎn)擊這里在線咨詢客服
新站提交
  • 網(wǎng)站:51998
  • 待審:31
  • 小程序:12
  • 文章:1030137
  • 會(huì)員:747

本文介紹了懶惰運(yùn)行時(shí)初始化Spring安全+重新加載Spring安全配置的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

Spring通常會(huì)在啟動(dòng)應(yīng)用程序時(shí)急切地加載Spring安全配置。我正在將OAuth與Spring Security一起使用

我維護(hù)了一個(gè)配置表,用于存儲(chǔ)與SSO相關(guān)的值(如jwk-url、Client_id、Client_Secret)。管理員用戶將通過同一Spring Boot應(yīng)用程序中的CRUD填充此值。

那么只有jwk-url可以在Spring安全配置(refer below code - jwkSetUri(...))中進(jìn)行配置。這在應(yīng)用程序啟動(dòng)時(shí)不可用。

因此,我希望在將值加載到表中之后初始化Spring安全配置,就像運(yùn)行時(shí)的延遲加載(@Lazy)一樣。我知道如何延遲加載常規(guī)類/服務(wù)。

    但我仍然不確定如何在運(yùn)行時(shí)調(diào)用configure(HttpSecurity http)方法以及如何
    為HttpSecurity參數(shù)賦值。當(dāng)我像在運(yùn)行時(shí)延遲加載一樣嘗試調(diào)用new ResourceServerConfiguration()時(shí),我沒有看到調(diào)用了configuration()方法。(或者)無論何時(shí)需要,這個(gè)類都需要維護(hù)為Bean和延遲加載。但仍不確定如何在代碼中調(diào)用CONFigure()。

    另一個(gè)問題是,如果管理員更改了jwk url,如何在運(yùn)行時(shí)刷新/重新加載Spring安全配置。則只有Spring安全配置才能使更改生效。

@Configuration
@EnableWebSecurity
public class ResourceServerConfiguration extends WebSecurityConfigurerAdapter {
    
    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.cors()
                .and()
                .csrf().disable()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                .anyRequest().authenticated()
                .and()
                .oauth2ResourceServer()
                .authenticationEntryPoint(oAuth2AuthenticationEntryPoint)
                .accessDeniedHandler(oAuth2AccessDeniedHandler)
                .jwt()
                 // Some Auth server URL which would be fetch from table
                .jwkSetUri(ssoConfigService.getActiveSSOCertificateURL()); 
                 // Eg. http://localhost:8090/auth/realms/demo-app/protocol/openid-connect/certs
    }
}

我已經(jīng)引用了這些鏈接。但這對(duì)我的目的沒有幫助。如有任何幫助,我們將不勝感激。

How do I lazy load Spring Security?

How to reload the Configure method of WebSecurityConfigurerAdapter when the application is up and running

Modify Spring Security Config at Runtime

Configure Spring HTTP Security at Runtime

推薦答案

請(qǐng)選中此鏈接Customizing CORS Filtering at Runtime,該鏈接包含與您相關(guān)的類似用例,但對(duì)于他來說,他需要?jiǎng)討B(tài)更改允許的來源。他們決定創(chuàng)建一個(gè)新的篩選器并簡單地?cái)U(kuò)展OncePerRequestFilter。

考慮檢查您的用例的OAuth2ResourceServerProperties。

更新:
在此方案中嘗試使用此代碼:

另一件事是,如果管理員更改了jwk url,如何在運(yùn)行時(shí)刷新/重新加載Spring安全配置。則只有Spring安全配置才能使更改生效。

 @Override
    public void configure(HttpSecurity http) throws Exception {
        http.cors()
                .and()
                .csrf().disable()
                .sessionManagement()
                .sessionCreationPolicy(SessionCreationPolicy.STATELESS).and()
                .authorizeRequests()
                .anyRequest().authenticated()

                // TODO: test with and without this and check if work for you
                .and()
                .oauth2ResourceServer()
                .authenticationEntryPoint(oAuth2AuthenticationEntryPoint)
                .accessDeniedHandler(oAuth2AccessDeniedHandler)
                .jwt()
                // Some Auth server URL which would be fetch from table
                .jwkSetUri(ssoConfigService.getActiveSSOCertificateURL());
        // Eg. http://localhost:8090/auth/realms/demo-app/protocol/openid-connect/certs

        http.addFilterBefore(new OncePerRequestFilter() {
            // Every time a request occur, this method will be called.
            @Override
            protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {

                try {
                    http.oauth2ResourceServer()
                            .authenticationEntryPoint(oAuth2AuthenticationEntryPoint)
                            .accessDeniedHandler(oAuth2AccessDeniedHandler)
                            .jwt()
                            // Some Auth server URL which would be fetch from table
                            .jwkSetUri(ssoConfigService.getActiveSSOCertificateURL());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, BasicAuthenticationFilter.class);
    }

希望此信息能幫助您。

這篇關(guān)于懶惰運(yùn)行時(shí)初始化Spring安全+重新加載Spring安全配置的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,

分享到:
標(biāo)簽:Spring 初始化 加載 懶惰 運(yùn)行 配置
用戶無頭像

網(wǎng)友整理

注冊(cè)時(shí)間:

網(wǎng)站:5 個(gè)   小程序:0 個(gè)  文章:12 篇

  • 51998

    網(wǎng)站

  • 12

    小程序

  • 1030137

    文章

  • 747

    會(huì)員

趕快注冊(cè)賬號(hào),推廣您的網(wǎng)站吧!
最新入駐小程序

數(shù)獨(dú)大挑戰(zhàn)2018-06-03

數(shù)獨(dú)一種數(shù)學(xué)游戲,玩家需要根據(jù)9

答題星2018-06-03

您可以通過答題星輕松地創(chuàng)建試卷

全階人生考試2018-06-03

各種考試題,題庫,初中,高中,大學(xué)四六

運(yùn)動(dòng)步數(shù)有氧達(dá)人2018-06-03

記錄運(yùn)動(dòng)步數(shù),積累氧氣值。還可偷

每日養(yǎng)生app2018-06-03

每日養(yǎng)生,天天健康

體育訓(xùn)練成績?cè)u(píng)定2018-06-03

通用課目體育訓(xùn)練成績?cè)u(píng)定