Authored by qinchao

对接ldap

@@ -5,6 +5,7 @@ import com.ui.User.MD5Util; @@ -5,6 +5,7 @@ import com.ui.User.MD5Util;
5 import com.ui.User.UserAuthLocal; 5 import com.ui.User.UserAuthLocal;
6 import com.ui.contants.HttpUriContants; 6 import com.ui.contants.HttpUriContants;
7 import com.ui.http.HttpRestClient; 7 import com.ui.http.HttpRestClient;
  8 +import com.ui.ldaputil.LdapAuthUtil;
8 import com.ui.model.BaseResponse; 9 import com.ui.model.BaseResponse;
9 import com.ui.model.Directional; 10 import com.ui.model.Directional;
10 import com.ui.model.req.AuthModule; 11 import com.ui.model.req.AuthModule;
@@ -45,6 +46,9 @@ public class UserCtrl { @@ -45,6 +46,9 @@ public class UserCtrl {
45 @Autowired 46 @Autowired
46 UserAuthLocal userAuthLocal; 47 UserAuthLocal userAuthLocal;
47 48
  49 + @Autowired
  50 + private LdapAuthUtil ldapAuthenticate;
  51 +
48 @RequestMapping("/toLogin") 52 @RequestMapping("/toLogin")
49 public ModelAndView toLogin() { 53 public ModelAndView toLogin() {
50 return new ModelAndView("user/login"); 54 return new ModelAndView("user/login");
@@ -66,13 +70,50 @@ public class UserCtrl { @@ -66,13 +70,50 @@ public class UserCtrl {
66 70
67 } 71 }
68 72
69 - User u = userAuthLocal.getUserByname(user.getName()); 73 + boolean ldapFlag=false;
  74 + // 给admin留一个后门,防止ldap不能验证
  75 + if(!"admin".equals(user.getName())){
  76 + ldapFlag=true;
  77 + }
  78 +
  79 + User u =null;
  80 + if(ldapFlag){
  81 + //走ldap统一认证接口
  82 + //认证
  83 + //认证不通过,提示
  84 + if(!ldapAuthenticate.login(user.getName(),user.getPwd())){
  85 + model.addAttribute("message", "请使用你的OA账户登陆,登录名或者密码错误");
  86 + return new ModelAndView("user/login");
  87 + }
  88 +
  89 + //认证通过
  90 + u = userAuthLocal.getUserByLdapName(user.getName());
  91 +
70 if (u == null) { 92 if (u == null) {
71 - model.addAttribute("message", "用户名不存在"); 93 + //创建默认的用户
  94 + model.addAttribute("message", "用户名不存在,如有需要请联系管理员添加用户");
72 return new ModelAndView("user/login"); 95 return new ModelAndView("user/login");
73 } 96 }
74 - if (u.getPwd().equals(MD5Util.encryption(user.getPwd()))) { 97 +
  98 +
  99 + }else{
  100 + u = userAuthLocal.getUserByname(user.getName());
  101 +
  102 + if (u == null) {
  103 + model.addAttribute("message", "用户名不存在,如有需要请联系管理员添加用户");
  104 + return new ModelAndView("user/login");
  105 + }
  106 +
  107 + if (!u.getPwd().equals(MD5Util.encryption(user.getPwd()))) {
  108 + model.addAttribute("message", "密码错误");
  109 + return new ModelAndView("user/login");
  110 + }
  111 +
  112 + }
  113 +
  114 + //用户放到session
75 session.setAttribute("user", u); 115 session.setAttribute("user", u);
  116 +
76 if(StringUtils.isBlank(Directional.getInstance().getLoginTargetUrl())){ 117 if(StringUtils.isBlank(Directional.getInstance().getLoginTargetUrl())){
77 return new ModelAndView("dashBoard/dashBoard"); 118 return new ModelAndView("dashBoard/dashBoard");
78 }else{ 119 }else{
@@ -84,10 +125,6 @@ public class UserCtrl { @@ -84,10 +125,6 @@ public class UserCtrl {
84 } 125 }
85 return null; 126 return null;
86 } 127 }
87 - } else {  
88 - model.addAttribute("message", "密码错误");  
89 - return new ModelAndView("user/login");  
90 - }  
91 128
92 } 129 }
93 130
  1 +<?xml version="1.0" encoding="UTF-8" standalone="no"?>
  2 +<beans xmlns="http://www.springframework.org/schema/beans"
  3 + xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4 + xsi:schemaLocation="http://www.springframework.org/schema/beans
  5 + http://www.springframework.org/schema/beans/spring-beans.xsd">
  6 +
  7 +
  8 + <bean id="ldapContextSource" class="com.ui.ldaputil.YhLdapContextSource">
  9 + <!--test ldaps://yoho01.local:636 -->
  10 + <!--formal ldaps://ldap.yoho.cn:2018 -->
  11 + <property name="url" value="ldaps://ldap.yoho.cn:2018"/>
  12 + <property name="base" value="dc=yoho01,dc=local"/>
  13 + <property name="userDn" value="tech@yoho01.local"/>
  14 + <property name="password" value="3nIiz2Ov38NU"/>
  15 + <property name="referral" value="follow"></property>
  16 + </bean>
  17 +
  18 + <bean id="ldapTemplate" class="org.springframework.ldap.core.LdapTemplate">
  19 + <property name="contextSource" ref="ldapContextSource"/>
  20 + <property name="ignorePartialResultException" value="true"/>
  21 + </bean>
  22 +
  23 +</beans>
@@ -28,10 +28,10 @@ @@ -28,10 +28,10 @@
28 <img src="<%=basePath %>img/yoho.jpg" alt="Yoho Ops" > 28 <img src="<%=basePath %>img/yoho.jpg" alt="Yoho Ops" >
29 </div> 29 </div>
30 <br /> 30 <br />
31 - <h4 class="text-center mb5">Still not a user?</h4> 31 + <h4 class="text-center mb5" style="color:red">请使用OA账户登录</h4>
32 <p class="text-center">Please connect with the admin.</p> 32 <p class="text-center">Please connect with the admin.</p>
33 33
34 - <div class="mb30"></div> 34 + <div class="mb30" style="color:red">${message}</div>
35 35
36 <form id="loginform" action="<%=basePath %>user/login" method="post"> 36 <form id="loginform" action="<%=basePath %>user/login" method="post">
37 <div class="input-group mb15"> 37 <div class="input-group mb15">