本文介紹了懶惰運(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ì)大家有所幫助,