【发布时间】:2018-05-21 15:14:10
【问题描述】:
我的目标是使用凭据进行两步身份验证。第一步是检查具有主体的用户是否在特殊的数据库表中具有角色。二是执行标准的ldap认证。
我需要同时执行 两个 检查,但身份验证提供程序的常见方法是在 any 身份验证提供程序第一次成功后声明身份验证成功。所以我决定创建一个自定义的 AuthenticationProvider 实现,它调用 LdapAuthenticationProvider 然后执行数据库检查逻辑,但它不起作用,因为没有什么可以与 AbstractLdapAuthenticationProvider 自动装配。
请告诉我是否
- 解决此类问题的方法是合理的
- 如果合理,我该如何注入 AbstractLdapAuthenticationProvider?
安全配置代码为
@Autowired
private DBRoleAuthenticationProvider dbRoleAuthenticationProvider;
@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
auth.ldapAuthentication()
.contextSource()
.url("...")
.managerDn("...")
.managerPassword("...")
.and()
.userSearchFilter("uid={0}");
auth.authenticationProvider(dbRoleAuthenticationProvider);
}
自定义身份验证提供程序是
@Component
public class DBRoleAuthenticationProvider implements AuthenticationProvider {
@Autowired
private UserHasRoleInDBService userHasRoleInDBService;
@Autowired
private AbstractLdapAuthenticationProvider ldapAuthenticationProvider;
@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
authentication = ldapAuthenticationProvider.authenticate(authentication);
if (!authentication.isAuthenticated()) {
return authentication;
}
try {
String loginToSearch = (String) authentication.getPrincipal();
if (!userHasRoleInDBService.userHasRole(loginToSearch)) {
authentication.setAuthenticated(false);
}
} catch (Exception e) {
authentication.setAuthenticated(false);
}
return authentication;
}
@Override
public boolean supports(Class<?> authentication) {
return authentication.equals(UsernamePasswordAuthenticationToken.class);
}
}
提前致谢!
【问题讨论】:
标签: spring-security spring-java-config