关于spring security使用ldap的配置疑问

pasband 2010-04-14
一个web系统需要用公司的域账号登录,
初次使用spring security遇到一些问题,
文档示例中是这么写的:
<ldap-server url="ldap://10.10.10.10:389/" />
<authentication-manager>   
<ldap-authentication-provider
user-dn-pattern="uid={0}"/>
</authentication-manager>

我把"uid={0}"改成"{0}"后报错:

Your login attempt was not successful, try again.
Reason: Failed to parse DN; nested exception is org.springframework.ldap.core.ParseException: Encountered "" at line 1, column 9. Was expecting: ...

貌似这个user-dn-pattern只能是 xxx={0} 这种形式,那怎么验证过呢?
xyz20003 2010-04-14
根据ldap的查询语法写就可以了啊。
pasband 2010-04-14
谢谢大侠的关注,我对ldap也是刚接触,使用jdk带的那个javax.naming包可以成功验证,验证部分代码如下:

public boolean authenricate(String ID, String password) {
boolean valid = false;
String userDN = ID;
userDN = "china\\" + userDN;
env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
env.put(Context.PROVIDER_URL, URL);// LDAP server
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, userDN);
env.put(Context.SECURITY_CREDENTIALS, password);
// 此处若不指定用户名和密码,则自动转换为匿名登录

try {
ctx = new InitialLdapContext(env, null);
valid = true;
} catch (javax.naming.AuthenticationException e) {
System.out.println("Authentication faild: " + e.toString());
valid = false;
} catch (Exception e) {
System.out.println("Something wrong while authenticating: "
+ e.toString());
valid = false;
}
return valid;
}

使用pasband登录,ethereal抓包是:
DN: china\pasband
Password: 12345678

而用spring security验证时,不能通过,
ethereal抓包是:
DN: uid=pasband
Password: 12345678

所以我试图把
<ldap-authentication-provider
user-dn-pattern="uid={0}"/>
改成
<ldap-authentication-provider
user-dn-pattern="china\\{0}"/>

结果就报错:
Your login attempt was not successful, try again.
Reason: Failed to parse DN; nested exception is org.springframework.ldap.core.TokenMgrError: Lexical error at line 1, column 6. Encountered: "\\" (92), after : ""


估计是解析user-dn-pattern的时候,只允许xxx=yyy这种样式,可这满足不了场景啊,这个解析代码是在spring-ldap,而不是security包中,而spring发布的源码,
未包含DnParserImpl.java这个文件(发布包是有DnParserImpl.class的)

现在不知道怎么处理了,可能是我使用的方式不对?请给点意见~谢谢!

xyz20003 写道
根据ldap的查询语法写就可以了啊。

Global site tag (gtag.js) - Google Analytics