首页 > 解决方案 > Spring Security 从其他 jar 扩展配置

问题描述

我有一个应用程序具有定义安全性的依赖项。典型配置:

@Configuration
@EnableWebSecurity
public class AuthConfiguration extends WebSecurityConfigurerAdapter {
    
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
            .antMatchers(DEFAULT_PERMIT_ALL_PATHS).permitAll()
            .and()
            .authorizeRequests().anyRequest().authenticated()
            .and()
            .exceptionHandling()
            .and()
            .csrf().disable().httpBasic();
    }        

    @Override
    protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
        User.UserBuilder users = User.withDefaultPasswordEncoder();
        authManagerBuilder.inMemoryAuthentication().withUser(users.username("FOO").password("BAR").roles("USER").build());
    }
}

以上是第一个项目。我有第二个项目,我对上面的这个有 POM 依赖项。现在我想添加一个额外的用户,但我必须在我的第二个项目中执行此操作,其中仅依赖于第一个具有安全性的项目。

我可以复制和粘贴整个 AuthConfiguration,然后我有 2 个安全过滤器链,但这是一个坏主意。应用程序开始处理请求,查看目标 URI 是什么。两个过滤器链具有相同的 antMatchers(我只想添加其他用户),并且只处理一个过滤器链。结果,它部分工作 - 第一个或第二个项目的用户工作(它取决于 AuthConfiguration 的@Order)。

也许以某种方式可以在我的第二个项目中注入例如 AuthenticationManagerBuilder 并扩展/添加其他用户?我试过了,但它不起作用。我在第二个项目中添加了这样的代码:

@Configuration
public class AdditionalUsersConfiguration() {

    public AdditionalUsersConfiguration(AuthenticationManagerBuilder builder) throws Exception {
        User.UserBuilder users = User.withDefaultPasswordEncoder();

        authManagerBuilder.inMemoryAuthentication()
        .withUser(users.username("FOO").password("BAR").roles("USER")
        .withUser(users.username("FOO2").password("BAR").roles("USER").build());
    }
}

只有第一个用户 FOO 有效,看起来它来自定义它的第一个项目。

编辑:我也尝试了这个问题的方法 How can I add users to the inMemoryAuthentication builder after it has been built? 我在第二个项目中添加了这段代码:

@Configuration
public class NextApproachConfiguration() {

    public NextApproachConfiguration(InMemoryUserDetailsManager manager) throws Exception {
        manager.createUser(new User("FOO2", "BAR", new ArrayList<GrantedAuthority>()));
        // in debugger here I see two users [FOO, FOO2]
    }
}

这看起来不错的样子。在调试器(代码中的注释)中,我看到来自第一个项目的用户和新用户。但是当我向邮递员请求端点时,我在调试器内部看到它如何调用 InMemoryUserDetailsManager::loadByUsername() 并且该管理器的实例与我添加用户二的位置不同。它只有一个来自第一个项目的用户。当我使用调试器启动应用程序时,我看到首先在我添加第二个用户的地方执行我上面的代码,然后从第一个项目(AuthConfiguration 类)执行代码,其中 AuthenticationManagerBuilder::inMemoryAuthentication 转到 InMemoryUserDetailsManagerConfigurer 在其中使用 new 创建 InMemoryUserDetailsManager 的新实例关键词。

标签: javaspringspring-security

解决方案


最后,在我的第二个项目中,我以这种方式从第一个项目中扩展了 AuthConfiguration 类:

@Configuration
@Order(90)
public class SecondProjectConfiguration extends AuthConfiguration {

    @Override
    protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception {
        super.configure(authManagerBuilder); // it creates FOO user

        User.UserBuilder users = User.withDefaultPasswordEncoder();
        authManagerBuilder.inMemoryAuthentication()
            .withUser(users.username("FOO2").password("BAR").roles("USER").build());
    }   
}

它工作正常。Order(90)还需要因为 AuthConfiguration 具有默认的 Order 100,并且它正在停止应用程序并在启动时出现错误。非常简单的方法,但最后一次尝试。


推荐阅读