2

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

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

4

1 回答 1

3

您必须改变拥有用户​​ 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;
}
于 2013-09-11T11:09:37.230 回答