期翼嘻嘻即时通讯综合平台

 找回密码
 立即注册
查看: 8522|回复: 3
打印 上一主题 下一主题

企业应用考虑事项 [复制链接]

Rank: 3Rank: 3

跳转到指定楼层
楼主
发表于 2012-9-25 17:50:33 |只看该作者 |倒序浏览
本帖最后由 zhj265 于 2012-9-26 16:40 编辑

企业应用应考虑
与LADP 或AD 集成----〉单点登陆
转载:
JAVA代码操作MS AD实现单点登录


package it.service;
import javax.swing.*;
import java.awt.*;
import javax.naming.*;
import javax.naming.directory.*;
import javax.naming.ldap.*;
import java.util.*;
import java.security.*;
public class ADConnection {
  DirContext ldapContext;
  String baseName = ",cn=users,DC=activedirectory,DC=myorg,DC=ca";
  String serverIP = "activedirectory.myorg.ca";
  String modelUsername = "template";
  public ADConnection() {
    try {
      Hashtable ldapEnv = new Hashtable(11);
      ldapEnv.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
      ldapEnv.put(Context.PROVIDER_URL,  "ldap://" + serverIP + ":636");
      ldapEnv.put(Context.SECURITY_AUTHENTICATION, "simple");
      ldapEnv.put(Context.SECURITY_PRINCIPAL, "cn=ldapadmin" + baseName);
      ldapEnv.put(Context.SECURITY_CREDENTIALS, "xxxx");
      ldapEnv.put(Context.SECURITY_PROTOCOL, "ssl");
      ldapContext = new InitialDirContext(ldapEnv);
      }
      catch (Exception e) {
        System.out.println(" bind error: " + e);
        e.printStackTrace();
        System.exit(-1);
     }
  }
  public void createNew(String username, String surname, String givenName) {
    try {
      String distinguishedName = "cn=" + username + baseName;
      Attributes newAttributes = new BasicAttributes(true);
      Attribute oc = new BasicAttribute("objectclass");
      oc.add("top");
      oc.add("person");
      oc.add("organizationalperson");
      oc.add("user");
      newAttributes.put(oc);
      newAttributes.put(new BasicAttribute("sAMAccountName", username));
      newAttributes.put(new BasicAttribute("userPrincipalName", username + "@" + serverIP));
      newAttributes.put(new BasicAttribute("cn", username));
      newAttributes.put(new BasicAttribute("sn", surname));
      newAttributes.put(new BasicAttribute("givenName", givenName));
      newAttributes.put(new BasicAttribute("displayName", givenName + " " + surname));
      System.out.println("Name: " + name + " Attributes: " + a);
      ldapContext.createSubcontext(distinguishedName, newAttributes);
    }
    catch (Exception e) {
      System.out.println("create error: " + e);
      e.printStackTrace();
      System.exit(-1);
    }
  }
  public void createClone(String username, String surname, String givenName) {
    try {
      Attributes modelAttributes = fetch(modelUsername);
      String distinguishedName = "cn=" + username + baseName;
      Attributes newAttributes = new BasicAttributes(true);
      newAttributes.put(modelAttributes.get("objectclass"));
      newAttributes.put(modelAttributes.get("userAccountControl"));
      newAttributes.put(new BasicAttribute("sAMAccountName", username));
      newAttributes.put(new BasicAttribute("userPrincipalName", username + "@" + serverIP));
      newAttributes.put(new BasicAttribute("cn", username));
      newAttributes.put(new BasicAttribute("sn", surname));
      newAttributes.put(new BasicAttribute("givenName", givenName));
      newAttributes.put(new BasicAttribute("displayName", givenName + " " + surname));
      System.out.println("distinguishedName: " + distinguishedName + " Attributes: " + newAttributes);
      ldapContext.createSubcontext(distinguishedName, newAttributes);
    }
    catch (Exception e) {
      System.out.println("create clone error: " + e);
      e.printStackTrace();
      System.exit(-1);
    }
  }
  public void update(String username) {
    try {
      System.out.println("updating.../n");
      ModificationItem[] mods = new ModificationItem[1];
      mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
        new BasicAttribute("description", "java y"));
      ldapContext.modifyAttributes("cn=" + username + baseName, mods);
     }
      catch (Exception e) {
        System.out.println(" update error: " + e);
        System.exit(-1);
      }
  }

  public void updatePassword(String username, String password) {
    try {
      System.out.println("updating password.../n");
      String quotedPassword = "/"" + password + "/"";
      char unicodePwd[] = quotedPassword.toCharArray();
      byte pwdArray[] = new byte[unicodePwd.length * 2];
      for (int i=0; i<unicodePwd.length; i++) {
        pwdArray[i*2 + 1] = (byte) (unicodePwd >>> 8);
        pwdArray[i*2 + 0] = (byte) (unicodePwd & 0xff);
      }
      System.out.print("encoded password: ");
      for (int i=0; i<pwdArray.length; i++) {
        System.out.print(pwdArray + " ");
      }
      System.out.println();
      ModificationItem[] mods = new ModificationItem[1];
      mods[0] = new ModificationItem(DirContext.REPLACE_ATTRIBUTE,
        new BasicAttribute("UnicodePwd", pwdArray));
      ldapContext.modifyAttributes("cn=" + username + baseName, mods);
     }
      catch (Exception e) {
        System.out.println("update password error: " + e);
        System.exit(-1);
      }
  }
  public Attributes fetch(String username) {
    Attributes attributes = null;
    try {
      System.out.println("fetching: " + username);
      DirContext o = (DirContext)ldapContext.lookup("cn=" + username + baseName);
      System.out.println("search done/n");
      attributes = o.getAttributes("");
      for (NamingEnumeration ae = attributes.getAll(); ae.hasMoreElements();) {
        Attribute attr = (Attribute)ae.next();
        String attrId = attr.getID();
        for (NamingEnumeration vals = attr.getAll(); vals.hasMore();) {
          String thing = vals.next().toString();
          System.out.println(attrId + ": " + thing);
        }
      }
    }
    catch (Exception e) {
      System.out.println(" fetch error: " + e);
      System.exit(-1);
    }
    return attributes;
  }
  public static void main(String[] args) {
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    // the keystore that holds trusted root certificates
    System.setProperty("javax.net.ssl.trustStore", "e://ldap//keystore");
    System.setProperty("javax.net.debug", "all");
    ADConnection adc = new ADConnection();
    adc.createClone("clone1", "Clone", "Clarissa");
    adc.updatePassword("clone1", "xxxx");
    adc.createNew("user1, "User", "Joe");
    Attributes a = adc.fetch("clone1");
  }
}

使用道具 举报

Rank: 4

沙发
发表于 2012-9-25 22:22:20 |只看该作者
额 看不懂 先支持了

使用道具 举报

Rank: 3Rank: 3

板凳
发表于 2013-4-28 10:57:43 |只看该作者
看出来啥是高人啦,呵呵

使用道具 举报

Rank: 1

地板
发表于 2019-4-9 22:43:26 |只看该作者
路过,发几张美女图片


使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

蓝牙耳机无线高音质适用于苹果华强北pro2023年新款华为小米通用 【推荐理由】赠运费险 【券后价】89.00

Archiver|手机版|期翼嘻嘻论坛企业即时通讯综合平台 ( 京 ICP 备 10015350 )

GMT+8, 2024-11-26 15:50 , Processed in 0.103134 second(s), 9 queries .

Powered by Discuz! X2

© 2001-2011 Comsenz Inc.

回顶部