在运行时延迟初始化spring安全性+重新加载spring安全性配置
Spring 通常在启动应用程序时急切地加载 Spring 安全配置。我在 Spring Security 中使用 OAuth
我正在维护一个用于存储 SSO 相关值(如 jwk-url、client_id、client_secret)的配置表。该值将由管理员用户通过 CRUD 在同一个 Spring Boot 应用程序中填充。
那么只有 jwk-url 可以在 Spring 安全配置中进行配置(refer below code - jwkSetUri(...))。这在应用程序启动时不可用。
所以我想在将值加载到表中后初始化 spring 安全配置,就像运行时的延迟加载(@Lazy)一样。我知道如何延迟加载常规类/服务。
-
但是我仍然不确定如何
configure(HttpSecurity http)在运行时调用该方法以及如何传递 HttpSecurity 参数。当我在运行时尝试像延迟加载一样调用new ResourceServerConfiguration()时,我没有看到 configure() 方法被调用。(或者)这个类需要在需要时作为 bean 和延迟加载来维护。但仍然不确定如何在代码中调用 configure()。 -
另一件事是如何在运行时刷新/重新加载 spring 安全配置,如果管理员更改了 JWK url。那么只有 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
}
}
我已经提到了这些链接。但这对我的目的没有帮助。任何帮助,将不胜感激。
如何延迟加载 Spring Security?
如何在应用程序启动并运行时重新加载 WebSecurityConfigurerAdapter 的 Configure 方法
在运行时修改 Spring Security Config
在运行时配置 Spring HTTP Security