java - 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 的新实例关键词。
解决方案
最后,在我的第二个项目中,我以这种方式从第一个项目中扩展了 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,并且它正在停止应用程序并在启动时出现错误。非常简单的方法,但最后一次尝试。
推荐阅读
- javascript - 重复一个元素,直到它覆盖整个屏幕
- reactjs - 我正在寻找一种方法来修复 react material-ui 中表组件中的第一列
- cluster-computing - Ceph 时钟偏差
- java - 从子类为私有属性赋值
- ajax - Asp.net 网络表单,jquery
- python - 我在下面发布的数据,我想将其上传到 django 模型并希望将 image_link 转换为我们的本地主机链接
- ruby-on-rails - 如何使用 2 种不同类型的用户在 Rails 中创建通知系统
- python - ImportError:无法从“颜色”导入名称“颜色”
- web3 - 如何通过 web3 将 Number 类型和 Bytes 转换为 Bytes32
- android-studio - 更改android studio项目包名称和标题?