一、Dspace 登录主要用到的3个类
LDAPServlet.java (package org.dspace.app.webui.servlet)
AuthenticationManager.java (package org.dspace.authenticate)
LDAPAuthentication.java (package org.dspace.authenticate)
二、Dspace.cfg 配置
每项具体的含义,文件中的注释写的非常清楚。
ldap.enable = true
ldap.provider_url = ldap://127.0.0.1/dc=informationDepartment,dc=wti.ac.cn
ldap.id_field = uid
ldap.object_context = ou=people,dc=informationDepartment,dc=wti.ac.cn
ldap.search_context = ou=people
ldap.email_field = mail
ldap.surname_field = sn
ldap.givenname_field = givenName
ldap.phone_field = telephoneNumber
webui.ldap.autoregister = true
三、Ldap认证过程
1. 当 ldap.enable = true 设定后,需要验证时会弹出 chooser.jsp 页面。
2. 选择第二项通过LDAP验证登录,会显示 webapp/components/ldap-form.jsp 页面。
3. 输入代号(Ldap的用户名)如 “sspecial” 、密码为 “password”。点击“登入”按钮,会触发LdapServlet,核心代码如下。
1) 首次登录:
// 如果数据库中不存在此用户,就通过Ldap进行认证,认证通过后创建对应的EPerson
if (ldapAuthenticate(netid, password, context, ldapResult))
{
//自动注册配置属性为true时
if (ConfigurationManager.getBooleanProperty("webui.ldap.autoregister"))
{
// 自动注册新用户
......
}
else
{
// 跳转到注册失败页面
......
}
}
2) 登录验证:
protected boolean ldapAuthenticate(String netid, String password, Context context, LDAPResult ldapResult)
{
//--------- 开始Ldap认证-------------
if (!password.equals(""))
{
String ldap_provider_url = ConfigurationManager.getProperty("ldap.provider_url");
String ldap_id_field = ConfigurationManager.getProperty("ldap.id_field");
String ldap_search_context = ConfigurationManager.getProperty("ldap.search_context");
String ldap_object_context = ConfigurationManager.getProperty("ldap.object_context");
// Set up environment for creating initial context
Hashtable env = new Hashtable(11);
env.put(javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(javax.naming.Context.PROVIDER_URL, ldap_provider_url);
// Authenticate
env.put(javax.naming.Context.SECURITY_AUTHENTICATION, "simple");
env.put(javax.naming.Context.SECURITY_PRINCIPAL, ldap_id_field+"="+netid+","+ldap_object_context);
env.put(javax.naming.Context.SECURITY_CREDENTIALS, password);
try
{
// Create initial context
DirContext ctx = new InitialDirContext(env); //如果netid或password不对,此处会抛出异常。
......
3) 验证通过后获取相关属性:
Attributes matchAttrs = new BasicAttributes(true);
matchAttrs.put(new BasicAttribute(ldap_id_field, netid)); //查找项
String attlist[] = {ldap_email_field, ldap_givenname_field, ldap_surname_field, ldap_phone_field}; // 要获取的属性
// look up attributes
try
{
NamingEnumeration answer = ctx.search(ldap_search_context, matchAttrs, attlist);
while(answer.hasMore()) {...... //获取相关属性的处理}
}
4) 登录成功:
在系统的左上角可以看到登录成功状态的人员信息。