【发布时间】:2021-06-18 19:57:42
【问题描述】:
spring boot启动完成后,Spring Security的配置无法更新。如何在不重启spring boot的情况下添加新用户或更新用户密码或用户角色?因为页面被重定向到 /login?error 页面,当我使用新密码登录时。
@EnableWebSecurity
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
MemberMapper memberMapper;
Logger logger = Logger.getLogger(this.getClass());
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable();
http.addFilterBefore(new loginFilter(), AnonymousAuthenticationFilter.class).
authorizeRequests().antMatchers("/","/register/**","/log/**").permitAll();
http.formLogin().loginPage("/log/toLogin")
.loginProcessingUrl("/log/login")
.usernameParameter("memacc")
.passwordParameter("mempwd")
.failureHandler(new AppsAuthenticationFailureHandler());;
http.logout().logoutSuccessUrl("/");
}
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
List<Members> allMembers = memberMapper.getAllMembers();
for (Members members : allMembers){
String[] roleList = members.getRoleList().split(",");
auth.inMemoryAuthentication().passwordEncoder(new BCryptPasswordEncoder())
.withUser(members.getMemacc()).password(new BCryptPasswordEncoder().encode(members.getMempwd())).roles(roleList);
}
}
}
【问题讨论】:
-
是否有使用 inMemoryAuthentication() 的特定需求?还是可以使用其他类型的身份验证?
-
InMemoryAuthentication 基本上用于提供一组静态用户(可能仅用于测试目的)。如果您需要提供动态用户创建支持,则需要提供适当的注册流程
-
不,只是想使用简单的方式来处理动态用户创建或更新用户详细信息
-
inMemoryAuthentication 适用于固定用户。我可以使用 UserDetailsService 动态身份验证吗?
标签: java spring spring-boot spring-security