spring - 如何在 Spring Boot 中为 preAuthenticationChecks 和 postAuthenticationChecks 配置 UserDetailsChecker
问题描述
我正在寻找UserDetailsChecker
在 Spring Boot 中为 preAuthenticationChecks 和/或 postAuthenticationChecks 配置“最佳实践”的解决方案(请参阅AbstractUserDetailsAuthenticationProvider
和DaoAuthenticationProvider
)。
是否绝对有必要创建自定义DaoAuthenticationProvider
?
无法通过WebSecurityConfigurerAdapter
or自定义它AuthenticationManagerBuilder
?
解决方案
回答我自己的问题:我搜索了 spring boot 源(v1.5.13.RELEASE 和 v2.0.2.RELEASE)grep
- 没有结果。
至少在这个版本中,DaoAuthenticationProvider
如果您想使用额外的认证前或认证后检查,您必须创建一个自定义。
编辑:“你必须创建一个自定义DaoAuthenticationProvider
”这句话有点误导。在最近的一个项目中,我通过以下方式完成了它:
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig {
@Autowired
public void configureGlobal(
AuthenticationManagerBuilder auth,
MyUserAccountService myUserAccountService,
PasswordEncoder passwordEncoder,
MessageSourceAccessor messageSourceAccessor
) throws Exception {
DaoAuthenticationProvider daoAuthenticationProvider = new DaoAuthenticationProvider();
daoAuthenticationProvider.setPasswordEncoder(passwordEncoder);
daoAuthenticationProvider.setUserDetailsService(myUserAccountService);
daoAuthenticationProvider.setPostAuthenticationChecks(new UserAccountChecker(messageSourceAccessor));
auth.authenticationProvider(daoAuthenticationProvider);
auth.userDetailsService(myUserAccountService).passwordEncoder(passwordEncoder);
}
[...]
}
这UserAccountChecker implements UserDetailsChecker
是一个发生(后)身份验证测试的类(另请参见 中的类DefaultPostAuthenticationChecks
)AbstractUserDetailsAuthenticationProvider
。
推荐阅读
- python - Python:循环索引问题
- discord.js - Discord js语音连接订阅未检测到结束事件
- c# - 在类库项目中使用类
- python-3.x - 电视节目如何获得“警报”消息?
- python - 为什么我可以将数组作为输入传递给使用 numpy 的 lambda 函数,但我不能将它传递给没有 numpy 的 lambda 函数?
- android - SwipeRefreshLayout 内的 RecyclerView 不可点击
- ios - 将 CoreData 上传到 CollectionViewCell 并配置
- android - 如何解决FAILURE:使用firebase配置后构建失败并出现异常
- angular - 将 blob 写入磁盘或索引数据库存储?
- python - isinstance(dataset, (list,)) 和 isinstance(dataset, list) 的区别