public ResultMsg loginAd(User user) throwsException {

ResultMsgmsg;//通过ad域登录

Hashtable env = newHashtable();

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 = newInitialDirContext(env);

SearchControls controls= newSearchControls();

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=newEmpBasic();

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(newDate()));

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 = newUser();

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(false, "该账户已过期");

}else if (erroMsg.contains("52e"))

{

msg= new ResultMsg(false, "用户或密码错误");

}else if (erroMsg.contains("525"))

{

msg= new ResultMsg(false, "用户或密码错误");

}else if (erroMsg.contains("773"))

{

msg= new ResultMsg(false, "用户必须重置密码");

}else if (erroMsg.contains("533"))

{

msg= new ResultMsg(false, "用户账户禁用");

}else{

msg= new ResultMsg(false, "用户登录失败");

}returnmsg;

}catch (NameNotFoundException e)//沒有对象

{

e.printStackTrace();

msg= new ResultMsg(false, "登录发生异常");returnmsg;

}catch(NamingException e) {

e.printStackTrace();

msg= new ResultMsg(false, "登录发生异常");returnmsg;

}finally{if (results != null) {try{

results.close();

}catch(Exception e) {

}

}if (ctx != null) {try{

ctx.close();

}catch(Exception e) {

}

}

}return msg = new ResultMsg(true, "登录验证成功", "", u1);

}

AD域账户登录mysql_java集成微软的ad域,实现单点登录相关推荐

  1. 帆软单点登录_FineReport和泛微OA(Ecology)的单点登录集成方案

    最近出现了很多关于帆软报表和泛微OA的集成问题,均出现在"单点登录"上.直接也有相关的文章介绍一些FineReport和泛微集成的背景.价值等,以及FineReport和OA的深度 ...

  2. cas登录成功什么意思_你知道CAS单点登录系统吗

    单点登录(Single Sign On),简称为 SSO,是目前比较流行的企业业务整合的解决方案之一.SSO的定义是在多个应用系统中,用户只需要登录一次就可以访问所有相互信任的应用系统.我们目前的系统 ...

  3. jenkins集成微软Active Directory并验证组登录

    测试环境: 1. ubuntu-20.04.5-live-server-amd64.iso  +  jenkins Long Term Support release 2.375.1 2. Windo ...

  4. oauth2 单点登录_Spring Security Oauth2和Spring Boot实现单点登录

    最近在学习单点登录相关,调查了一下目前流行的单点登录解决方案:cas 和 oauth2,文章主要介绍oauth2 单点登录.希望这篇文章能帮助大家学习相关内容. 我们将使用两个单独的应用程序: 授权服 ...

  5. php java 单点登录_php实现多站点共用session实现单点登录的方法详解

    本文实例讲述了php实现多站点共用session实现单点登录的方法.分享给大家供大家参考,具体如下: 最近闲来无事,总结整理下单点登录的问题. 单点登录的基本原理为:客户端共享sesionid,服务器 ...

  6. redis 登录_Redis集群架构+Dubbo开发框架+SSO单点登录+Nginx+ZooKeeper

    Redis集群架构 [课程介绍] Redis是现在使用为广泛的NoSQL数据库技术,其自身不仅拥有着良好的操作性能,也被广泛的应用于各种集群架构的数据整合处理之中,而本课程将通过Redis的核心作用, ...

  7. 单点登录CAS学习(一):初识单点登录

    一.单点登录应用场景 不少业主单位随着自身的发展,建立不少业务支撑系统,往往会采用不同的开发商进行系统开发和建设,因此必然形成如下一种局面:工作人员需要登录多个业务系统才能将自己的工作全部完成,给工作 ...

  8. 单点登录3 手撕代码模拟CAS实现单点登录

    一  工程概况 1.1 介绍 该模拟工程实现了子系统登录后,其他系统免登陆,在系统中注销,全部注销的功能,目前还有点小问题未实现: 访问子系统A,进行登录. 访问认证中心1,免密登录. 重新打开一个新 ...

  9. Spring Boot Security 多种登录方式集成配置思路及方法 账号用户名登录+微信网页授权登录

    概述 实现账号用户名+微信网页授权登录集成在Spring Security的思路 前情提要 本思路完全抛弃Spring Security的配置式账号密码登录模式,采用完全独立的Filter.Provi ...

最新文章

  1. python3中的int类型占64位_在windows 10 64位计算机中,默认情况下,numpy数组数据类型将以int32形式出现...
  2. Linux软件源apt,仓库,包的概念
  3. vga显卡下载 linux,下载:NVIDIA显卡Linux驱动190.53正式版
  4. SAP CRM Fiori my task应用是如何在My Account应用里加载的
  5. ios开发 静音键设置_合肥包河区:连夜设置三道拦水坝只为按下中考“静音键”...
  6. figma设计_Figma与Adobe XD:我们如何选择下一个设计工具
  7. docker Gitlab14.5.0 安装、配置、部署、使用
  8. pt-heartbeat 监测RDS延迟
  9. C# 中用stopwatch测试代码运行时间
  10. Python - 装机系列24 消息工具 RabbitMQ详细了解
  11. 打印机服务器不支持1020,HP1020打印机驱动安装不上的解决办法
  12. jaxen-1.1-beta-6.jar的用处
  13. freeswitch添加G729编解码模块
  14. 用html代码在word中插入分页符,Word文档中插入分页符并对分页方式进行设置的方法...
  15. msg1500说明书_MSG1500刷机笔记
  16. 误Ghost后的分区恢复
  17. Mysql必知必会概要总结
  18. 极速办公如何在Excel中进行条件计数
  19. SS2H框架搭建,基础上篇
  20. Linux中存储设备的命名规则

热门文章

  1. 【Android系统】拨号app
  2. r语言 协整_《量化金融R语言初级教程》一1.3 协整
  3. thinkphp使用phpoffice读取Excel并写入数据库
  4. java文档怎么设置默认打开方式
  5. 使用python将excel表格通讯录导入手机通讯录
  6. 兰州城市学院计算机专业在哪个校区,兰州城市学院 代码
  7. node.js爬虫-校园网模拟登录
  8. 使用命令行进行文件操作
  9. 数字摄影测量特征点提取之SUSAN算子
  10. 一键修复服务器,云服务器漏洞一键修复命令