spring - 通过 spring LDAP 进行身份验证,并在数据库中进行额外的安全检查

标签 spring authentication jdbc spring-security ldap

LDAP 身份验证后,我想检查数据库是否在用户表中列出了用户 ID。

我怎样才能做到这一点?我在 Google 上看到的只是通过 LDAP 进行身份验证以及数据库中的用户角色检索。

最佳答案

您必须改变拥有用户​​ Prancipal 的方式:

您将保留 ldapProvider :

<beans:bean id="ldapProvider" class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">   

....

<beans:bean id="customUserDetailsMapper" class="xxxxx.CustomUserDetailsMapper">
    <beans:constructor-arg ref="customUserDetailService" />
</beans:bean>

<beans:bean id="customUserDetailService" class="xxxxxx.CustomUserDetailService">
</beans:bean>

定义自定义 UserDatailsMapper :

public class CustomUserDetailsMapper extends LdapUserDetailsMapper {

private UserDetailsService userDetailService;

public CustomUserDetailsMapper (UserDetailsService userDetailService) {
    this.userDetailService = userDetailService;
}

@Override
public UserDetails mapUserFromContext(DirContextOperations ctx,
        String username, Collection<? extends GrantedAuthority> authorities) {

    return (UserDetails) this.userDetailService.loadUserByUsername(username);
}

}

并定义一个自定义 UserDetailsS​​ervice :

public class CustomUserDetailService implements UserDetailsService {


@Autowired
protected UserRepository userRepository;

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    UserDb user = userRepository.findByUserName(username);

    if (UserDb  == null) {
        throw new UsernameNotFoundException(username);
    }           

    // Construct customUserDetails

    return (UserDetails)customUserDetails;
}

关于spring - 通过 spring LDAP 进行身份验证,并在数据库中进行额外的安全检查,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18737969/

相关文章:

java - 运行子查询决定是否跳过外部查询,如果跳过不发送参数?

java - java中如何处理表列中的空值?

Spring从另一个变量设置@value

xml - 无法将名称 'repository:repository' 解析为 (n) 'type definition' 组件。

java - Spring Security 不接受正确的凭据

python - 使用 json 渲染器将记住 header 添加到 json 响应

java - Spring - 无法获取资源(405错误)

javascript - Facebook 登录按钮回调函数

c++ - 哪个 Windows API 用于 Windows 安全代理身份验证对话框?

java - 为什么 select 语句总是返回最后插入的值?