带有oauth2的春季安全

发布于 2025-02-11 00:30:57 字数 3713 浏览 0 评论 0 原文

我的旧安全配置代码基于Spring Boot 2.6工作正常:

@Configuration @EnableWebSecurity
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {

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

我现在正在升级不推荐使用的 websecurityConfigurerAdapter 类,而有利于使用 @bean@bean return return return 的方法SecurityFilterChain authenticationEventPublisher :

@Configuration @EnableWebSecurity
public class ResourceServerConfig {

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

我的问题的原因是:对象 bearerTokenAuthenticationFilter use providermanager as autheation> authetication> authenticationManager (甚至在使用 WebSecurityConfigurerAdapter 之前)。 ,如默认 providerManager 声明其 authenticationEdticationEventPublisher 以这种方式:

public class ProviderManager implements AuthenticationManager, . . . {

    . . .

    private AuthenticationEventPublisher eventPublisher = new NullEventPublisher();

存在问题:NulleventPublisher是一个不发布事件的无效实现。

但是 在使用 WebsecurityConfigurerAdapter - providerManager 的EventPublisher对象被分配给 defaultauthatectionEventPublisher 对象,

在一些测试之后,我能够“解决问题”,我能够“解决问题”。以下代码:

@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;
    }
}

还调整我的安全配置:

@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()来自 ProvidManager 而无需强迫配置 oauth2resourceserver()。配置其 SecurityFilterChain

My old security config code based on Spring Boot 2.6 worked fine:

@Configuration @EnableWebSecurity
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {

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

I am now upgrading the usage of deprecated WebSecurityConfigurerAdapter class in favour of a method with @Bean to return SecurityFilterChain as recommended and my applications have no valid AuthenticationEventPublisher anymore:

@Configuration @EnableWebSecurity
public class ResourceServerConfig {

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

The reason for my problem is: the object BearerTokenAuthenticationFilter uses ProviderManager as AuthenticationManager (even before using WebSecurityConfigurerAdapter).
But as default ProviderManager declares its AuthenticationEventPublisher this way:

public class ProviderManager implements AuthenticationManager, . . . {

    . . .

    private AuthenticationEventPublisher eventPublisher = new NullEventPublisher();

There it is the problem: NullEventPublisher is a null implementation which doesn't publish events.

And before when using WebSecurityConfigurerAdapter - the eventPublisher object of ProviderManager was assigned with DefaultAuthenticationEventPublisher object

After some tests I was able to "fix the problem" with the following code:

@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;
    }
}

And also adjusting my security configuration:

@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();
    }
}

But I have two concerns:

  1. The purpose of my module/application is to be used by other applications of my company in order to publish specific logs. And this solution will force dozens of applications to add the lines with comments 1 and 2
  2. I am not aware of the risks of "forcing" a pre-built ProviderManager for those applications

So finally my question here is: Is there a way to bypass eventPublisher = new NullEventPublisher() from ProviderManager without forcing to configure oauth2ResourceServer().authenticationManager(manager) in all applications configuring its SecurityFilterChain?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

你对谁都笑 2025-02-18 00:30:57

看起来您对默认 authenticationEventPublisher 的假设不正确。
In fact, the default one (DefaultAuthenticationEventPublisher) is provided via the Spring Security autoconfiguration: org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration#authenticationEventPublisher().< br>
然后,它是由 authenticationConfiguration 创建 authenticationManagerBuilder 的,该 在请求中创建了提到的 providermanager ,并使用填充的事件发布者创建。

It doesn't look like your assumption about the default AuthenticationEventPublisher is correct.
In fact, the default one (DefaultAuthenticationEventPublisher) is provided via the Spring Security autoconfiguration: org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration#authenticationEventPublisher().
Then it's picked up by AuthenticationConfiguration which creates an AuthenticationManagerBuilder which upon request creates the mentioned ProviderManager with the populated event publisher.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文