首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SpringBoot2.7中使用没有OAuth2的WebSecurityConfigurerAdapter但没有AuthenticationEventPublisher的Spring安全性

SpringBoot2.7中使用没有OAuth2的WebSecurityConfigurerAdapter但没有AuthenticationEventPublisher的Spring安全性
EN

Stack Overflow用户
提问于 2022-06-28 01:42:09
回答 1查看 487关注 0票数 0

我基于Spring 2.6的旧安全配置代码运行良好:

代码语言:javascript
复制
@Configuration @EnableWebSecurity
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                . . . 
            .and()
                .oauth2ResourceServer()
                    .jwt();
    }
}

现在,我正在升级被废弃的WebSecurityConfigurerAdapter类的用法,转而使用@Bean来返回SecurityFilterChain 按建议,并且我的应用程序不再具有有效的AuthenticationEventPublisher

代码语言:javascript
复制
@Configuration @EnableWebSecurity
public class ResourceServerConfig {

    @Bean
    public SecurityFilterChain configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                . . . 
            .and()
                .oauth2ResourceServer()
                    .jwt();
        return http.build();
    }
}

造成我的问题的原因是:对象BearerTokenAuthenticationFilter使用ProviderManager作为AuthenticationManager (甚至在使用WebSecurityConfigurerAdapter之前)。但是,当默认的ProviderManager以这种方式声明其AuthenticationEventPublisher时:

代码语言:javascript
复制
public class ProviderManager implements AuthenticationManager, . . . {

    . . .

    private AuthenticationEventPublisher eventPublisher = new NullEventPublisher();

--这就是问题所在: NullEventPublisher是一个不发布事件的空实现。

在使用WebSecurityConfigurerAdapter之前,ProviderManager的eventPublisher对象被指定为DefaultAuthenticationEventPublisher对象。

经过一些测试,我能够用以下代码“修复问题”:

代码语言:javascript
复制
@Configuration
@ConditionalOnClass({AuthenticationEventPublisher.class, JwtAuthenticationProvider.class})
public class SpringConfiguration { //global configuration for several others
    @Bean
    public ProviderManager providerManagerAvecDefaultAuthenticationPublisher(@Lazy JwtDecoder jwtDecoder, AuthenticationEventPublisher authenticationPublisher) {
        JwtAuthenticationProvider authenticationProvider = new JwtAuthenticationProvider(jwtDecoder);
        ProviderManager providerManager = new ProviderManager(Arrays.asList(authenticationProvider));
        providerManager.setAuthenticationEventPublisher(authenticationPublisher);
        return providerManager;
    }
}

还调整了我的安全配置:

代码语言:javascript
复制
@Configuration @EnableWebSecurity
public class ResourceServerConfig {

    @Autowired ProviderManager manager; //1

    @Bean
    public SecurityFilterChain configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                . . . 
            .and()
                .oauth2ResourceServer()
                    .jwt()
                    .authenticationManager(manager); //2
        return http.build();
    }
}

但我有两个顾虑:

  1. 我的模块/应用程序的目的是供我公司的其他应用程序使用,以便发布特定的日志。此解决方案将强制数十个应用程序添加注释1和2的行。
  2. 我不知道为这些应用程序“强制”一个预先构建的ProviderManager的风险。

最后,我的问题是:是否有一种方法可以绕过eventPublisher = new NullEventPublisher()ProviderManager的关系,而无需在所有配置SecurityFilterChain的应用程序中配置oauth2ResourceServer().authenticationManager(manager)

EN

回答 1

Stack Overflow用户

发布于 2022-06-28 08:57:19

您对默认AuthenticationEventPublisher的假设似乎不正确。

实际上,默认的一个(DefaultAuthenticationEventPublisher)是通过:org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration#authenticationEventPublisher()提供的。

然后由AuthenticationConfiguration获取,它创建一个AuthenticationManagerBuilder,该AuthenticationManagerBuilder根据请求使用填充的事件发布者创建所述的ProviderManager

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/72779864

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档