Showing
11 changed files
with
282 additions
and
20 deletions
@@ -139,6 +139,25 @@ public class UserAuthLocal { | @@ -139,6 +139,25 @@ public class UserAuthLocal { | ||
139 | return usermap.get(name); | 139 | return usermap.get(name); |
140 | } | 140 | } |
141 | 141 | ||
142 | + /** | ||
143 | + * 获取用户 | ||
144 | + * | ||
145 | + * @param name | ||
146 | + * @return | ||
147 | + */ | ||
148 | + public User getUserByLdapName(String name) { | ||
149 | + if (usermap.isEmpty()) {//改用延时加载 | ||
150 | + init(); | ||
151 | + } | ||
152 | + for(String key:usermap.keySet()){ | ||
153 | + User u=usermap.get(key); | ||
154 | + if(name.equals(u.getLadpName())){ | ||
155 | + return u; | ||
156 | + } | ||
157 | + } | ||
158 | + return null; | ||
159 | + } | ||
160 | + | ||
142 | public AuthModule getAuthModuleByname(String name) { | 161 | public AuthModule getAuthModuleByname(String name) { |
143 | if (modulemap.isEmpty()) {//改用延时加载 | 162 | if (modulemap.isEmpty()) {//改用延时加载 |
144 | init(); | 163 | init(); |
@@ -102,6 +102,7 @@ public class HttpUriContants { | @@ -102,6 +102,7 @@ public class HttpUriContants { | ||
102 | public static final String USER_UPDATE = "/user/update"; | 102 | public static final String USER_UPDATE = "/user/update"; |
103 | public static final String USER_DELETE_NAME = "/user/deleteByName"; | 103 | public static final String USER_DELETE_NAME = "/user/deleteByName"; |
104 | public static final String USER_OPERATE = "/user/operate"; | 104 | public static final String USER_OPERATE = "/user/operate"; |
105 | + public static final String USER_LDAP_FLAG = "/user/getLdapFlag"; | ||
105 | 106 | ||
106 | public static final String APP_ADD_SEESION = "/user/appAddSession"; | 107 | public static final String APP_ADD_SEESION = "/user/appAddSession"; |
107 | public static final String APP_GET_SEESION = "/user/appGetSession"; | 108 | public static final String APP_GET_SEESION = "/user/appGetSession"; |
@@ -32,5 +32,11 @@ | @@ -32,5 +32,11 @@ | ||
32 | <version>1.0-rc2</version> | 32 | <version>1.0-rc2</version> |
33 | </dependency> | 33 | </dependency> |
34 | 34 | ||
35 | + <dependency> | ||
36 | + <groupId>org.springframework.ldap</groupId> | ||
37 | + <artifactId>spring-ldap-core</artifactId> | ||
38 | + <version>2.0.2.RELEASE</version> | ||
39 | + </dependency> | ||
40 | + | ||
35 | </dependencies> | 41 | </dependencies> |
36 | </project> | 42 | </project> |
@@ -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.req.AuthModule; | 10 | import com.ui.model.req.AuthModule; |
10 | import com.ui.model.req.User; | 11 | import com.ui.model.req.User; |
@@ -39,6 +40,9 @@ public class UserCtrl { | @@ -39,6 +40,9 @@ public class UserCtrl { | ||
39 | @Autowired | 40 | @Autowired |
40 | UserAuthLocal userAuthLocal; | 41 | UserAuthLocal userAuthLocal; |
41 | 42 | ||
43 | + @Autowired | ||
44 | + private LdapAuthUtil ldapAuthenticate; | ||
45 | + | ||
42 | @RequestMapping("/toLogin") | 46 | @RequestMapping("/toLogin") |
43 | public ModelAndView toLogin(String loginTargetUrl, Model model) { | 47 | public ModelAndView toLogin(String loginTargetUrl, Model model) { |
44 | if(StringUtils.isNotBlank(loginTargetUrl)){ | 48 | if(StringUtils.isNotBlank(loginTargetUrl)){ |
@@ -63,27 +67,60 @@ public class UserCtrl { | @@ -63,27 +67,60 @@ public class UserCtrl { | ||
63 | 67 | ||
64 | } | 68 | } |
65 | 69 | ||
66 | - User u = userAuthLocal.getUserByname(user.getName()); | ||
67 | - if (u == null) { | ||
68 | - model.addAttribute("message", "用户名不存在"); | ||
69 | - return new ModelAndView("user/login"); | 70 | + boolean ldapFlag=false; |
71 | + BaseResponse ldapFlagResp=httpRestClient.defaultGet(HttpUriContants.USER_LDAP_FLAG,BaseResponse.class); | ||
72 | + if(ldapFlagResp!=null&&ldapFlagResp.getCode()==200&&ldapFlagResp.getData()!=null&&"1".equals(String.valueOf(ldapFlagResp.getData()))){ | ||
73 | + ldapFlag=true; | ||
70 | } | 74 | } |
71 | - if (u.getPwd().equals(MD5Util.encryption(user.getPwd()))) { | ||
72 | - session.setAttribute("user", u); | ||
73 | - if(StringUtils.isBlank(user.getLoginTargetUrl())){ | ||
74 | - return new ModelAndView("dashBoard/dashBoard"); | ||
75 | - }else{ | ||
76 | - try { | ||
77 | - //直接跳转到初始的请求页面 | ||
78 | - response.sendRedirect(user.getLoginTargetUrl()); | ||
79 | - } catch (IOException e) { | ||
80 | - return new ModelAndView("dashBoard/dashBoard"); | ||
81 | - } | ||
82 | - return null; | 75 | + |
76 | + User u =null; | ||
77 | + if(ldapFlag){ | ||
78 | + //走ldap统一认证接口 | ||
79 | + //认证 | ||
80 | + //认证不通过,提示 | ||
81 | + if(!ldapAuthenticate.login(user.getName(),user.getPwd())){ | ||
82 | + model.addAttribute("message", "请使用你的OA账户登陆,登录名或者密码错误"); | ||
83 | + return new ModelAndView("user/login"); | ||
83 | } | 84 | } |
84 | - } else { | ||
85 | - model.addAttribute("message", "密码错误"); | ||
86 | - return new ModelAndView("user/login"); | 85 | + |
86 | + //认证通过 | ||
87 | + u = userAuthLocal.getUserByLdapName(user.getName()); | ||
88 | + | ||
89 | + if (u == null) { | ||
90 | + //创建默认的用户 | ||
91 | + model.addAttribute("message", "用户名不存在,如有需要请联系管理员添加用户"); | ||
92 | + return new ModelAndView("user/login"); | ||
93 | + } | ||
94 | + | ||
95 | + | ||
96 | + }else{ | ||
97 | + u = userAuthLocal.getUserByname(user.getName()); | ||
98 | + | ||
99 | + if (u == null) { | ||
100 | + model.addAttribute("message", "用户名不存在,如有需要请联系管理员添加用户"); | ||
101 | + return new ModelAndView("user/login"); | ||
102 | + } | ||
103 | + | ||
104 | + if (!u.getPwd().equals(MD5Util.encryption(user.getPwd()))) { | ||
105 | + model.addAttribute("message", "密码错误"); | ||
106 | + return new ModelAndView("user/login"); | ||
107 | + } | ||
108 | + | ||
109 | + } | ||
110 | + | ||
111 | + //用户放到session | ||
112 | + session.setAttribute("user", u); | ||
113 | + | ||
114 | + if(StringUtils.isBlank(user.getLoginTargetUrl())){ | ||
115 | + return new ModelAndView("dashBoard/dashBoard"); | ||
116 | + }else{ | ||
117 | + try { | ||
118 | + //直接跳转到初始的请求页面 | ||
119 | + response.sendRedirect(user.getLoginTargetUrl()); | ||
120 | + } catch (IOException e) { | ||
121 | + return new ModelAndView("dashBoard/dashBoard"); | ||
122 | + } | ||
123 | + return null; | ||
87 | } | 124 | } |
88 | 125 | ||
89 | } | 126 | } |
1 | +package com.ui.ldaputil; | ||
2 | + | ||
3 | +import org.slf4j.Logger; | ||
4 | +import org.slf4j.LoggerFactory; | ||
5 | +import org.springframework.beans.factory.annotation.Autowired; | ||
6 | +import org.springframework.ldap.core.AttributesMapper; | ||
7 | +import org.springframework.ldap.core.LdapTemplate; | ||
8 | +import org.springframework.ldap.filter.AndFilter; | ||
9 | +import org.springframework.ldap.filter.EqualsFilter; | ||
10 | +import org.springframework.stereotype.Component; | ||
11 | + | ||
12 | +import javax.naming.directory.SearchControls; | ||
13 | +import java.util.List; | ||
14 | + | ||
15 | +/** | ||
16 | + * Created by jimi on 2017/12/26. | ||
17 | + */ | ||
18 | +@Component | ||
19 | +public class LdapAuthUtil { | ||
20 | + | ||
21 | + private static final Logger logger= LoggerFactory.getLogger(LdapAuthUtil.class); | ||
22 | + | ||
23 | + @Autowired | ||
24 | + LdapTemplate ldapTemplate; | ||
25 | + | ||
26 | + public boolean login(String userName, String passWord){ | ||
27 | + AndFilter filter = new AndFilter(); | ||
28 | + filter.and(new EqualsFilter("objectclass", "person")) | ||
29 | + .and(new EqualsFilter("sAMAccountName", userName)); | ||
30 | + String[] urls = ((YhLdapContextSource) ldapTemplate.getContextSource()).getUrls(); | ||
31 | + logger.info("ldap urls :{}",urls); | ||
32 | + return ldapTemplate.authenticate("", filter.toString(), passWord); | ||
33 | + } | ||
34 | + | ||
35 | + public LdapUser getUser(String userName){ | ||
36 | + AndFilter filter = new AndFilter(); | ||
37 | + filter.and(new EqualsFilter("objectclass", "person")) | ||
38 | + .and(new EqualsFilter("sAMAccountName", userName)); | ||
39 | + LdapUser user = null; | ||
40 | + int SearchScope = SearchControls.SUBTREE_SCOPE; | ||
41 | + AttributesMapper<LdapUser> attr = new LdapMapUser(); | ||
42 | + List<LdapUser> users=ldapTemplate.search("",filter.toString(), SearchScope, attr); | ||
43 | + if (users.size() !=0){ | ||
44 | + user=users.get(0); | ||
45 | + } | ||
46 | + return user; | ||
47 | + } | ||
48 | +} | ||
49 | + | ||
50 | + | ||
51 | + |
1 | +package com.ui.ldaputil; | ||
2 | + | ||
3 | +import org.springframework.ldap.core.AttributesMapper; | ||
4 | + | ||
5 | +import javax.naming.NamingException; | ||
6 | +import javax.naming.directory.Attribute; | ||
7 | +import javax.naming.directory.Attributes; | ||
8 | + | ||
9 | +/** | ||
10 | + * Created by jimi on 2017/12/26. | ||
11 | + */ | ||
12 | +public class LdapMapUser implements AttributesMapper<LdapUser> { | ||
13 | + @Override | ||
14 | + public LdapUser mapFromAttributes(Attributes attributes) throws NamingException { | ||
15 | + | ||
16 | + Attribute attr; | ||
17 | + LdapUser user = new LdapUser(); | ||
18 | + | ||
19 | + attr = attributes.get("name"); | ||
20 | + user.setName((String) attr.get()); | ||
21 | + | ||
22 | + attr = attributes.get("sAMAccountName"); | ||
23 | + user.setsAMAccountName((String) attr.get()); | ||
24 | +// | ||
25 | +// attr = attributes.get("department"); | ||
26 | +// user.setDepartment((String) attr.get()); | ||
27 | + | ||
28 | + attr = attributes.get("mail"); | ||
29 | + user.setMail((String) attr.get()); | ||
30 | + | ||
31 | + return user; | ||
32 | + } | ||
33 | +} |
1 | +package com.ui.ldaputil; | ||
2 | + | ||
3 | +/** | ||
4 | + * Created by jimi on 2017/12/26. | ||
5 | + */ | ||
6 | +public class LdapUser { | ||
7 | + | ||
8 | + private String name; | ||
9 | + private String sAMAccountName; | ||
10 | + private String department; | ||
11 | + private String mail; | ||
12 | + | ||
13 | + public String getName() { | ||
14 | + return name; | ||
15 | + } | ||
16 | + | ||
17 | + public void setName(String name) { | ||
18 | + this.name = name; | ||
19 | + } | ||
20 | + | ||
21 | + public String getsAMAccountName() { | ||
22 | + return sAMAccountName; | ||
23 | + } | ||
24 | + | ||
25 | + public void setsAMAccountName(String sAMAccountName) { | ||
26 | + this.sAMAccountName = sAMAccountName; | ||
27 | + } | ||
28 | + | ||
29 | + public String getDepartment() { | ||
30 | + return department; | ||
31 | + } | ||
32 | + | ||
33 | + public void setDepartment(String department) { | ||
34 | + this.department = department; | ||
35 | + } | ||
36 | + | ||
37 | + public String getMail() { | ||
38 | + return mail; | ||
39 | + } | ||
40 | + | ||
41 | + public void setMail(String mail) { | ||
42 | + this.mail = mail; | ||
43 | + } | ||
44 | +} |
1 | +package com.ui.ldaputil; | ||
2 | + | ||
3 | +import org.springframework.beans.factory.InitializingBean; | ||
4 | +import org.springframework.ldap.core.support.LdapContextSource; | ||
5 | + | ||
6 | +import javax.net.ssl.SSLContext; | ||
7 | +import javax.net.ssl.TrustManager; | ||
8 | +import javax.net.ssl.X509TrustManager; | ||
9 | +import java.security.cert.CertificateException; | ||
10 | +import java.security.cert.X509Certificate; | ||
11 | + | ||
12 | +/** | ||
13 | + * Created by jimi on 2018/1/8. | ||
14 | + */ | ||
15 | +public class YhLdapContextSource extends LdapContextSource implements InitializingBean { | ||
16 | + | ||
17 | + YhLdapContextSource() { | ||
18 | + super(); | ||
19 | + } | ||
20 | + | ||
21 | + | ||
22 | + public void afterPropertiesSet() { | ||
23 | + | ||
24 | + super.afterPropertiesSet(); | ||
25 | + try { | ||
26 | + SSLContext ctx = SSLContext.getInstance("TLS"); | ||
27 | + X509TrustManager tm = new X509TrustManager() { | ||
28 | + | ||
29 | + public void checkClientTrusted(X509Certificate[] xcs, String string) throws CertificateException { | ||
30 | + } | ||
31 | + | ||
32 | + public void checkServerTrusted(X509Certificate[] xcs, String string) throws CertificateException { | ||
33 | + } | ||
34 | + | ||
35 | + public X509Certificate[] getAcceptedIssuers() { | ||
36 | + return null; | ||
37 | + } | ||
38 | + }; | ||
39 | + ctx.init(null, new TrustManager[]{tm}, null); | ||
40 | + SSLContext.setDefault(ctx); | ||
41 | + } catch (Exception ex) { | ||
42 | + ex.printStackTrace(); | ||
43 | + } | ||
44 | + } | ||
45 | + | ||
46 | +} |
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 ldap://yoho01.local:389 --> | ||
11 | + <property name="url" value="ldap://yoho01.local:389"/> | ||
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> |
@@ -31,7 +31,7 @@ | @@ -31,7 +31,7 @@ | ||
31 | <h4 class="text-center mb5">Still not a user?</h4> | 31 | <h4 class="text-center mb5">Still not a user?</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"> |
-
Please register or login to post a comment