拒绝访问 - LDAP

时间:2014-05-21 07:01:43

标签: java spring-mvc spring-security ldap

我使用ldap并试图制作这样的方案: 我尝试登录" Web应用程序仪表板",使用数据库服务器中的现有用户名(在本例中使用sql server)

enter image description here

这是Spring-security.xml代码:

    <beans:beans>
    <beans:bean id="ldapAuthProvider" class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
        <beans:constructor-arg value="intra.anabatictech.com" />
        <beans:constructor-arg value="ldap://10.8.8.11:389/" />
        <beans:property name="authoritiesMapper" ref="userDetailsService" /> 
    </beans:bean>
    <authentication-manager alias="authenticationManager">
        <authentication-provider ref="ldapAuthProvider"></authentication-provider>
    </authentication-manager>
    <beans:bean id="userDetailsService" class="com.anabatic.alc.service.impl.ldap.LdapUserService" />
</beans:beans>

这是LdapUserService.ldap代码:

package com.anabatic.alc.service.impl.ldap;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List; 
import java.util.EnumSet;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.mapping.GrantedAuthoritiesMapper;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.security.core.authority.SimpleGrantedAuthority;

import com.anabatic.alc.bean.UserDetailAlc;
import com.anabatic.alc.dao.admin.auth.UserDao;
import com.anabatic.alc.entity.admin.auth.User;
 import com.anabatic.alc.entity.admin.auth.RoleMapping;
 import com.anabatic.alc.enums.RolesEnum;

 @Service
 @Transactional(readOnly=true)
 public class LdapUserService implements UserDetailsService, GrantedAuthoritiesMapper{

@Autowired
private UserDao userDao;
private User user;
public UserDetails loadUserByUsername(String username)
        throws UsernameNotFoundException {
    this.user = this.userDao.findByUsername(username).get(0);
    if(this.user == null) throw new UsernameNotFoundException("Error tidak ada user");

    List<GrantedAuthority> roles = new ArrayList<GrantedAuthority>();
    for (RoleMapping rm : user.getRoleMapping()) {
        roles.add(new SimpleGrantedAuthority(rm.getRole().getRole()));
    }

    UserDetailAlc userDetailAlc = new UserDetailAlc(username, 
            user.getPassword(), 
            true, 
            true, 
            true, 
            true, 
            roles);

    mapAuthorities(roles);

    return userDetailAlc;
}

public Collection<? extends GrantedAuthority> mapAuthorities(
        Collection<? extends GrantedAuthority> authorities) {

    Set<RolesEnum> enums = EnumSet.noneOf(RolesEnum.class);

    for(GrantedAuthority authority : authorities){
        if(RolesEnum.ROLE_ADMIN.equals(authority.getAuthority())){
            enums.add(RolesEnum.ROLE_ADMIN);    System.out.println("admin");
        }else if(RolesEnum.ROLE_USER.equals(authority.getAuthority())){
            enums.add(RolesEnum.ROLE_USER);     System.out.println("user");
        }else if(RolesEnum.ROLE_SUPERVISOR.equals(authority.getAuthority())){
            enums.add(RolesEnum.ROLE_SUPERVISOR);   System.out.println("agent");
        }
    }
    return enums;
}
}

我尝试使用LDAP进行身份验证但是我失败了, 这是我运行程序时来自控制台的信息

INFO : org.springframework.security.ldap.SpringSecurityLdapTemplate - Ignoring PartialResultException

结果:

enter image description here

我认为它的成功将身份验证与服务器连接起来,因为当我登录并插入与服务器相同的用户名和密码成功但可能无法从我使用的帐户中获取详细信息

你可以帮助我,我必须做什么才能成功登录? 谢谢:))

0 个答案:

没有答案