1.ad域介绍:

windos server 2008R2服务器下的ad域,见下图(我是在虚拟机安装到windos server)

2.连接ad域代码:(里面代码自行修改)

public ResultMsg<User> loginAd(User user) throws Exception {
ResultMsg<User> msg;
//通过ad域登录
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
//ad域地址:windos server上输入ipconfig查看,369是固定端口,dc=contoso,dc=com是域的范围
env.put(Context.PROVIDER_URL, "ldap://192.168.153.160:389/dc=contoso,dc=com");
//ad域里面的用户
env.put(Context.SECURITY_PRINCIPAL, "admin@contoso.com");
//ad域里面的密码
env.put(Context.SECURITY_CREDENTIALS, "Ai123456");
DirContext ctx = null;
NamingEnumeration results = null;
User u1 = null;
String st="";
try {
//登录验证
ctx = new InitialDirContext(env);
SearchControls controls = new SearchControls();
controls.setSearchScope(SearchControls.SUBTREE_SCOPE);
//根据用户名查看ad域中是否存在当前用户
results = ctx.search("", "(&(objectclass=person)(userprincipalname=" + user.getUsername()+domainName + "))", controls);
SearchResult searchResult = (SearchResult) results.next();
Attributes attributes = searchResult.getAttributes();
if (attributes != null) {
//查询数据库用户
User userByName = userService.getUserByName(user.getUsername());
if(userByName==null)
{
//设置唯一id
try {
for (NamingEnumeration ne=attributes.getAll();ne.hasMore();)
{
Attribute Attr = (Attribute) ne.next();
if ("objectGUID".equals(Attr.getID()))
{
st = DeptServiceImpl.getGUID(Attr.get().toString().getBytes());
}
}
}catch (Exception e)
{
e.printStackTrace();
}
//查询员工是否存在,若存在返回id编号不存在就插入
EmpBasic empBasicByUserPrincipalName = empMapper.getObjectGuid(st);
Integer integer;
if(empBasicByUserPrincipalName==null)
{
//添加员工
EmpBasic empBasic=new EmpBasic();
empBasic.setLastName(attributes.get("sn")==null?"":attributes.get("sn").get().toString());
empBasic.setFirstName(attributes.get("givenName")==null?"":attributes.get("givenName").get().toString());
empBasic.setNickName(attributes.get("displayname")==null?"":attributes.get("displayname").get().toString());
empBasic.setUserName(attributes.get("userprincipalname")==null?"":attributes.get("userprincipalname").get().toString().split("@")[0]);
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
empBasic.setHireDate(df.format(new Date()));
empBasic.setEmail(attributes.get("mail")==null?"":attributes.get("mail").get().toString());
empBasic.setCellphone(attributes.get("mobile")==null?"":attributes.get("mobile").get().toString()); empBasic.setObjectGuid(st);
String dateRq= attributes.get("whenCreated").get().toString().substring(0,8);
SimpleDateFormat df1 = new SimpleDateFormat("yyyyMMdd");
Date date=df1.parse(dateRq);
empBasic.setHireDate(df.format(date)); //查询部门编号
String str = attributes.get("distinguishedName").get().toString().substring(
attributes.get("distinguishedName").get().toString().indexOf("O"));
Department department = deptMapper.getDistinguiName(str);
empBasic.setDeptNo(department.getDeptNo());
//返回插入的id
integer = empService.addAdEmp(empBasic);
}else
{
integer=empBasicByUserPrincipalName.getEmpNo();
}
//添加用户
User u = new User();
u.setUsername(attributes.get("userprincipalname").get().toString().split("@")[0]);
u.setLastName(attributes.get("sn")==null?"":attributes.get("sn").get().toString());
u.setFirstName(attributes.get("givenName")==null?"":attributes.get("givenName").get().toString());
u.setNickName(attributes.get("displayname")==null?"":attributes.get("displayname").get().toString());
u.setUserNo(integer.toString());
//添加用户时,用户类型默认为1,
u.setUserType(1);//?
//添加用户时,用户默认启用
u.setEnabled(1);
u.setIsAd(1);
userService.addAdUser(u); u1= userService.getUserByName(u.getUsername());
}else
{
u1=userByName;
//查询部门
if (u1.getUserType().equals(1)) { //用户类型为员工时才查询其部门
u1.setDepartment(userService.getDeptInfoByUsername(user.getUsername()));
};
}
}
} catch (AuthenticationException e) {
String erroMsg= e.getMessage();
if (erroMsg.contains("701"))
{
msg = new ResultMsg<User>(false, "该账户已过期");
}else if (erroMsg.contains("52e"))
{
msg = new ResultMsg<User>(false, "用户或密码错误");
}else if (erroMsg.contains("525"))
{
msg = new ResultMsg<User>(false, "用户或密码错误");
}else if (erroMsg.contains("773"))
{
msg = new ResultMsg<User>(false, "用户必须重置密码");
} else if (erroMsg.contains("533"))
{
msg = new ResultMsg<User>(false, "用户账户禁用");
}else
{
msg = new ResultMsg<User>(false, "用户登录失败");
}
return msg;
} catch (NameNotFoundException e)//沒有对象
{
e.printStackTrace();
msg = new ResultMsg<User>(false, "登录发生异常");
return msg;
} catch (NamingException e) {
e.printStackTrace();
msg = new ResultMsg<User>(false, "登录发生异常");
return msg;
} finally {
if (results != null) {
try {
results.close();
} catch (Exception e) {
}
}
if (ctx != null) {
try {
ctx.close();
} catch (Exception e) {
}
}
}
return msg = new ResultMsg<User>(true, "登录验证成功", "", u1);
}

java集成微软的ad域,实现单点登录的更多相关文章

  1. 可跨域的单点登录(SSO)实现方案

    可跨域的单点登录(SSO)实现方案 SSO简介 定义: 传统的单站点登录访问授权机制是:登录成功后将用户信息保存在session中,sessionId保存在cookie中,每次访问需要登录访问的资源( ...

  2. 跨域分布式系统单点登录的实现(CAS单点登录)

    1. 概述 上一次我们聊了一下<使用Redis实现分布式会话>,原理就是使用 客户端Cookie + Redis 的方式来验证用户是否登录. 如果分布式系统中,只是对Tomcat做了负载均 ...

  3. 深入理解跨域SSO(单点登录)原理与技术

    一:SSO体系结构 SSO ​ SSO英文全称Single Sign On,单点登录.SSO是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.它包括可以将这次主要的登录映射到其他 ...

  4. 深入理解跨域SSO单点登录原理与技术

    [本文版权归微信公众号"代码艺术"(ID:onblog)所有,若是转载请务必保留本段原创声明,违者必究.若是文章有不足之处,欢迎关注微信公众号私信与我进行交流!] 一:SSO体系结 ...

  5. JAVA 通过LDAP获取AD域用户及组织信息

    因为工作需求近期做过一个从客户AD域获取数据实现单点登录的功能,在此整理分享. 前提:用户可能有很多系统的情况下,为了方便账号的统一管理使用AD域验证登录,所以不需要我们的系统登录,就需要获取用户的A ...

  6. springboot整合微软的ad域,采用ldap的api来整合,实现用户登录验证、

    流程: 1.用户调登录接口,传用户名和密码2.用户名和密码在ad验证,验证通过后,返回当前用户的相关信息.(注:ldap为java自带的api不需要maven引入其他的)3.根据返回的用户信息,实现自 ...

  7. JAVA使用Ldap操作AD域

    项目上遇到的需要在集成 操作域用户的信息的功能,第一次接触ad域,因为不了解而且网上其他介绍不明确,比较费时,这里记录下. 说明: (1). 特别注意:Java操作查询域用户信息获取到的数据和域管理员 ...

  8. Java使用LdAP获取AD域用户

    随着我们的习大大上台后,国家在网络信息安全方面就有了非常明显的改变!所以如今好多做网络信息安全产品的公司和须要网络信息安全的公司都会提到用AD域server来验证,这里就简单的研究了一下! 先简单的讲 ...

  9. 可跨域的单点登录(SSO)实现方案【附.net代码】

    SSO简介 定义: 传统的单站点登录访问授权机制是:登录成功后将用户信息保存在session中,sessionId保存在cookie中,每次访问需要登录访问的资源(url)时判断当前session是否 ...

随机推荐

  1. (三)Java工程化--Git起步

    GIT学习参考:https://git-scm.com/book/zh/v2 版本控制 版本控制记录了一个或若干文件的历史变化,便于今后查阅,恢复. 三类版本控制系统 本地版本控制系统 RCS : 本 ...

  2. monkeyrunner环境配置

    1.安装JDK.android  SDK.Python (安装完成后,配置环境变量:%JAVA_HOME%\bin;%JAVA_HOME%\jre\bin;%ANDROID_HOME%\platfor ...

  3. 在c:forEach与s:iterator里面使用if标签判断当前位置是否为2的倍数

    在c:forEach与s:iterator里面使用if标签判断当前位置是否为2的倍数 c:forEach: <c:forEach var="workflow" items=& ...

  4. 使用 ThreeSixty 创建可拖动的 360 度全景图片预览效果

    ThreeSixty 是生成可拖动的360度预览图像序列的 jQuery 插件.只需要在你的 HTML 页面包引入最新的 jQuery 和 threesixty.js 文件就可以使用了,支持键盘上的箭 ...

  5. asp.net core 通过ajax调用后台方法(非api)

    1.    在Startup.cs文件中添加:        services.AddMvc();            services.AddAntiforgery(o => o.Heade ...

  6. Scapy

    1.UDP scanning with Scapy Scapy is a tool that can be used  to craft and inject custom packets into  ...

  7. python小猪蹄儿

    夜的第七章,打字机继续向前推向,微亮! 请写一个栈 class Stack: #初始化栈(列表) def __init__(self): self.items=[] #栈的大小 def size(se ...

  8. beta冲刺2/7

    目录 摘要 团队部分 个人部分 摘要 队名:小白吃 组长博客:hjj 作业博客:beta冲刺(2/7) 团队部分 后敬甲(组长) 过去两天完成了哪些任务 整理博客 做了点商家数据表格 接下来的计划 做 ...

  9. nginx+vsftp图片下载java代码上传

    系统环境:阿里云centos7.3 安装nginx 查看nginx进程 ps aux|grep nginx 在/usr/local/nginx/sbin/目录下 nginx启动 ./nginx 快速停 ...

  10. F#周报2019年第11期

    新闻 Bolero:WebAssembly中的F# 尝试WebAssembly里的F# JetBrains的fsharp-support 2019.1 ML.NET 0.11发布 Outreachy内 ...