Dspace1.5 之 Ldap(二)

 

一、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)  登录成功:

    在系统的左上角可以看到登录成功状态的人员信息。

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值