简体   繁体   English

Spring Security中的Ldap配置-自己的类

[英]Ldap configuration in Spring Security - own class

I want to write my own LDAP authentication provider. 我想编写自己的LDAP身份验证提供程序。 I am extending AbstractUserDetailsAuthenticationProvider , which has a method retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) . 我正在扩展AbstractUserDetailsAuthenticationProvider ,它具有一个方法retrieveUser(String username, UsernamePasswordAuthenticationToken authentication)

I want to override this method and write my own data retrieving method. 我想重写此方法并编写自己的数据检索方法。 How to do that in Java? 如何用Java做到这一点? How to make an LDAP query and how connect to the LDAP server? 如何进行LDAP查询以及如何连接到LDAP服务器? I was searching in Internet but I didn't find anything that helped. 我在Internet上搜索,但没有找到任何帮助。

EDIT: 22.01.2013 编辑:22.01.2013

@Override
protected UserDetails retrieveUser(String username,
        UsernamePasswordAuthenticationToken authentication)
        throws AuthenticationException {

    LdapUser userDetail = null;

    log.entry("retrieveUser", authentication.getPrincipal());

    UsernamePasswordAuthenticationToken userToken = authentication;
    String userName = userToken.getName();
    userName = userName != null ? userName.toLowerCase() : userName;
    String password = userToken.getCredentials().toString();

    try {
        if (password == null || "".equals(password)) {
            log.debug("retrieveUser", "no password provided");
            throw new AuthenticationCredentialsNotFoundException(
                    "Invalid login or password");
        }
    }

    catch (AuthenticationCredentialsNotFoundException e) {
        log.debug("retrieveUser", "no password provided");
    }

    // connection with ldap and check retrieved username and password
    connect = connection(userName, password);

    if (connect) {
        log.debug("retrieve user", "correct connection with ldap");
        userDetail = new LdapUser();
        setUserDetails(userDetail, ctx, username);

    } else {
        log.error("retrieve user", "Failed connection");
    }

    log.exit("retrieveUser", "user logged: " + userDetail);
    return userDetail;
}

My security.xml file 我的security.xml文件

<http auto-config='true'>
    <intercept-url pattern="/**/*.ico" filters="none" />
    <intercept-url pattern="/**/*.gif" filters="none" />
    <intercept-url pattern="/**/*.jpg" filters="none" />
    <intercept-url pattern="/**/*.css" filters="none" />
    <intercept-url pattern="/**/*.js" filters="none" />
    <intercept-url pattern="/**/*.png" filters="none" />
    <intercept-url pattern="/logout.jsp*" filters="none" />
    <intercept-url pattern="/index.jsp*" filters="none" />
    <intercept-url pattern="/**" access="ROLE_USER,ROLE_ADMIN" />
    <logout logout-success-url="/index.jsp"/>


    <form-login login-page="/index.jsp"
    authentication-failure-url="/error_ldap.jsp" 
    default-target-url="/main_ldap.jsp" always-use-default-target="true" />                 
    </http>

<authentication-manager>
    <authentication-provider ref="ldapAuthenticationProvider">  
        <password-encoder hash="sha" /> 
    </authentication-provider>
</authentication-manager> 

When login is suceed I got redirect to main_ldap.jsp, but if authentication fail, I got this error. 登录成功后,我重定向到main_ldap.jsp,但是如果身份验证失败,则会出现此错误。 I tried to throw exception UsernameNotFoundException instead returning null in retrieveUser method (which is not allowed) but anything happend (only i got this exception). 我试图抛出异常UsernameNotFoundException,而是在retrieveUser方法中返回null(这是不允许的),但是任何事情都发生了(只有我得到了此异常)。

You can connect to LDAP from java: 您可以从java连接到LDAP:

http://docs.oracle.com/javase/jndi/tutorial/ldap/security/ldap.html http://docs.oracle.com/javase/jndi/tutorial/ldap/security/ldap.html

but spring security already has ldap integration, you can use of the methods described here: 但是spring security已具有ldap集成,您可以使用此处描述的方法:

http://static.springsource.org/spring-security/site/docs/3.0.x/reference/ldap.html http://static.springsource.org/spring-security/site/docs/3.0.x/reference/ldap.html

... ...

xml config for using your own UserDetails service is: 使用您自己的UserDetails服务的xml配置为:

<b:bean id="userDetailsService" class="your.class.here">
</b:bean>
<authentication-provider user-service-ref="userDetailsService">
</authentication-provider>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM