构建后如何将用户添加到 inMemoryAuthentication 构建器? [英] How can I add users to the inMemoryAuthentication builder after it has been built?
问题描述
在应用程序的初始加载期间,我设法将所有用户加载到 AuthenticationManagerBuilder 中,但我需要在启动后添加用户.
I have managed to load all the users into the AuthenticationManagerBuilder during the initial load of the application, but I have a requirement to add users post-startup.
启动:
public class WebSecurityConfig extends WebSecurityConfigurerAdapter
...
auth.inMemoryAuthentication().withUser(email).password(password).roles(roles.toArray(new String[roles.size()])).and().passwordEncoder(encoder());
这在某个时间点非常有效,但我有一个用例,可以在应用程序运行时添加用户.
This works great for a point in time, but I have a use case where users can be added while the application is running.
我可以通过哪种方法进行启动后(通过控制器/服务)?我认为它可能是 InMemoryUserDetailsManager
(它包含 createUser()
方法),但我不确定如何引用或配置它.
By which method can I do this post-startup (via controller/service)? I'm thinking it might be the InMemoryUserDetailsManager
(it contains the createUser()
method) but I'm not sure how to reference or configure it.
推荐答案
以下代码将满足您的要求:
The following code will do what you are asking:
@Configuration
@EnableWebMvcSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//whatever here
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(inMemoryUserDetailsManager());
}
@Bean
public InMemoryUserDetailsManager inMemoryUserDetailsManager() {
final Properties users = new Properties();
users.put("user","pass,ROLE_USER,enabled"); //add whatever other user you need
return new InMemoryUserDetailsManager(users);
}
}
使用您在上面配置的 InMemoryUserDetailsManager
一个超级简单的控制器,该控制器只添加并检查用户是否存在,如下所示:
Using the InMemoryUserDetailsManager
you configured above a super simple controller that just adds and checks for the existence of a user would look like:
@RestController
@RequestMapping("user")
public class SimpleSecurityController {
private final InMemoryUserDetailsManager inMemoryUserDetailsManager;
@Autowired
public SimpleSecurityController(InMemoryUserDetailsManager inMemoryUserDetailsManager) {
this.inMemoryUserDetailsManager = inMemoryUserDetailsManager;
}
@RequestMapping("exists/{username}")
public boolean userExists(@PathVariable("username") String username ) {
return inMemoryUserDetailsManager.userExists(username);
}
@RequestMapping("add/{username}/{password}")
public String add(@PathVariable("username") String username, @PathVariable("password") String password) {
inMemoryUserDetailsManager.createUser(new User(username, password, new ArrayList<GrantedAuthority>()));
return "added";
}
}
另请注意,如果您使用的是 Spring Boot 的自动配置,则需要添加
Also note that if you are using Spring Boot's autoconfiguration you will need to add
@EnableAutoConfiguration(exclude = SecurityAutoConfiguration.class)
防止 Spring Boot 尝试自动配置安全性
to prevent Spring Boot from trying to autoconfigure security
更新正如@AdamMichalik 所指出的,@EnableWebMvcSecurity
已被弃用,应替换为 @EnableWebSecurity
Update
As noted by @AdamMichalik, @EnableWebMvcSecurity
is deprecated and should be replaced by @EnableWebSecurity
这篇关于构建后如何将用户添加到 inMemoryAuthentication 构建器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!