公告:魔扣目录网为广大站长提供免费收录网站服务,提交前请做好本站友链:【 网站目录:https://www.morko.net 】, 免友链快审服务(50元/站),

点击这里在线咨询客服
新站提交
  • 网站:51998
  • 待审:31
  • 小程序:12
  • 文章:1030137
  • 会员:747

本文介绍了懒惰运行时初始化Spring安全+重新加载Spring安全配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Spring通常会在启动应用程序时急切地加载Spring安全配置。我正在将OAuth与Spring Security一起使用

我维护了一个配置表,用于存储与SSO相关的值(如jwk-url、Client_id、Client_Secret)。管理员用户将通过同一Spring Boot应用程序中的CRUD填充此值。

那么只有jwk-url可以在Spring安全配置(refer below code - jwkSetUri(...))中进行配置。这在应用程序启动时不可用。

因此,我希望在将值加载到表中之后初始化Spring安全配置,就像运行时的延迟加载(@Lazy)一样。我知道如何延迟加载常规类/服务。

    但我仍然不确定如何在运行时调用configure(HttpSecurity http)方法以及如何
    为HttpSecurity参数赋值。当我像在运行时延迟加载一样尝试调用new ResourceServerConfiguration()时,我没有看到调用了configuration()方法。(或者)无论何时需要,这个类都需要维护为Bean和延迟加载。但仍不确定如何在代码中调用CONFigure()。

    另一个问题是,如果管理员更改了jwk url,如何在运行时刷新/重新加载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
    }
}

我已经引用了这些链接。但这对我的目的没有帮助。如有任何帮助,我们将不胜感激。

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

推荐答案

请选中此链接Customizing CORS Filtering at Runtime,该链接包含与您相关的类似用例,但对于他来说,他需要动态更改允许的来源。他们决定创建一个新的筛选器并简单地扩展OncePerRequestFilter。

考虑检查您的用例的OAuth2ResourceServerProperties。

更新:
在此方案中尝试使用此代码:

另一件事是,如果管理员更改了jwk url,如何在运行时刷新/重新加载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);
    }

希望此信息能帮助您。

这篇关于懒惰运行时初始化Spring安全+重新加载Spring安全配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,

分享到:
标签:Spring 初始化 加载 懒惰 运行 配置
用户无头像

网友整理

注册时间:

网站:5 个   小程序:0 个  文章:12 篇

  • 51998

    网站

  • 12

    小程序

  • 1030137

    文章

  • 747

    会员

赶快注册账号,推广您的网站吧!
最新入驻小程序

数独大挑战2018-06-03

数独一种数学游戏,玩家需要根据9

答题星2018-06-03

您可以通过答题星轻松地创建试卷

全阶人生考试2018-06-03

各种考试题,题库,初中,高中,大学四六

运动步数有氧达人2018-06-03

记录运动步数,积累氧气值。还可偷

每日养生app2018-06-03

每日养生,天天健康

体育训练成绩评定2018-06-03

通用课目体育训练成绩评定