spring - 在基于证书的客户端的情况下调用 AuthenticationManager
问题描述
使用基于证书的客户端身份验证时,spring 会调用哪个 AuthenticationManager?目前我正在使用 PamAuthentication,我需要使用基于证书的身份验证。我看到 org.springframework.security.core.userdetails.UserDetailsService 不再被调用。在基于证书的身份验证的情况下调用哪个实现?
解决方案
当使用基于证书的身份验证时,AbstractPreAuthenticatedProcessingFilter
将负责对客户端进行身份验证。首先,过滤器通过X509AuthenticationFilter
. 之后,使用证书 CN 作为主体,使用证书 DN 作为凭证来创建身份验证令牌。将AuthenticationManager
执行该令牌身份验证的实现是PreAuthenticatedAuthenticationProvider
,它将使用您的实现UserDetailsService
来对客户端进行身份验证。
您可以通过将其添加到 HttpConfigurer 来启用证书身份验证,并配置您自己的UserDetailsService
.
@Override
protected void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.anyRequest()
.authenticated()
.and()
.x509()
.subjectPrincipalRegex("CN=(.*?)")
.userDetailsService(userDetailsService());
}
@Bean
public UserDetailsService userDetailsService() {
return new UserDetailsService() {
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
Optional<User> user = userRepository.findByUsername(username);
if(user.isPresent()){
return new User(username, userCredentials, new Role("ROLE_USER"));
}
throw new UsernameNotFoundException("User" + username + "does not exist");
}
};
}
这里有一个 x509 证书身份验证的示例。
推荐阅读
- python - 合并大小略有不同的栅格
- python - QtableWidgets 用python对一列求和
- android - 每隔几小时在特定的毫秒内执行任务 Android
- docker-compose - Yii 1.1 与 AWS Fargate 上的 JQuery 资产有关的问题 - Dockerized 应用程序
- python - Pandas:在两个不同时间序列中按日期顺序分组在同一 ID 下的列表中显示事件
- git - ADF:重新部署 ADF 后,自托管集成运行时不可用
- python-3.x - 按特定顺序存储 2 个 API 调用响应 [Python 3]
- database - 在 oracle 12c 上创建索引时出现不适当的错误
- windows - ASP.NET Core 应用程序和 Windows 服务通信
- python - 从熊猫数据框中以正确的格式提取和子集字典 - python