相关疑难解决方法(0)

在运行时延迟初始化 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)一样。我知道如何延迟加载常规类/服务。

  1. 但是我仍然不确定如何configure(HttpSecurity http)在运行时调用该方法以及如何传递 HttpSecurity 参数。当我在运行时尝试像延迟加载一样调用new ResourceServerConfiguration()时,我没有看到 configure() 方法被调用。(或者)这个类需要在需要时作为 bean 和延迟加载来维护。但仍然不确定如何在代码中调用 configure()。

  2. 另一件事是如何在运行时刷新/重新加载 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() …
Run Code Online (Sandbox Code Playgroud)

java spring spring-security spring-boot spring-security-oauth2

7
推荐指数
1
解决办法
160
查看次数