8 changed files with 172 additions and 18 deletions
-
5pom.xml
-
66src/main/java/com/spring/modules/sys/controller/SysLoginController.java
-
9src/main/java/com/spring/modules/sys/dao/SysUserDao.java
-
8src/main/java/com/spring/modules/sys/service/SysUserService.java
-
10src/main/java/com/spring/modules/sys/service/impl/SysUserServiceImpl.java
-
74src/main/java/com/spring/modules/sys/utils/CustomerLdapUtils.java
-
13src/main/resources/application-dev.yml
-
5src/main/resources/mapper/sys/SysUserDao.xml
@ -0,0 +1,74 @@ |
|||
package com.spring.modules.sys.utils; |
|||
|
|||
import java.util.HashMap; |
|||
import java.util.Hashtable; |
|||
import java.util.List; |
|||
import java.util.Map; |
|||
|
|||
import javax.naming.NamingEnumeration; |
|||
import javax.naming.NamingException; |
|||
import javax.naming.directory.Attribute; |
|||
import javax.naming.directory.Attributes; |
|||
|
|||
import org.springframework.beans.factory.annotation.Autowired; |
|||
import org.springframework.ldap.core.AttributesMapper; |
|||
import org.springframework.ldap.core.LdapTemplate; |
|||
import org.springframework.ldap.query.LdapQueryBuilder; |
|||
import org.springframework.stereotype.Component; |
|||
|
|||
@Component |
|||
public class CustomerLdapUtils { |
|||
|
|||
@Autowired |
|||
private LdapTemplate ldapTemplate; |
|||
|
|||
/** |
|||
* |
|||
* @param ldapUrls |
|||
* @param username |
|||
* @param password |
|||
* @return |
|||
*/ |
|||
public Map<String, String> CheckLdapAccountAndPassword(String username, String password) { |
|||
//设置映射 |
|||
AttributesMapper<Map<String, String>> mapper = new AttributesMapper<Map<String, String>>() { |
|||
|
|||
@Override |
|||
public Map<String, String> mapFromAttributes(Attributes attributes) throws NamingException { |
|||
Map<String, String> resultMap = new HashMap<>(); |
|||
String username = attributes.get("sAMAccountName").get().toString(); |
|||
resultMap.put("username", username); |
|||
// 打印所有属性及其值 |
|||
NamingEnumeration<? extends Attribute> attrs = attributes.getAll(); |
|||
while (attrs.hasMore()) { |
|||
Attribute attr = attrs.next(); |
|||
String attrName = attr.getID(); |
|||
String attrValue = attr.get().toString(); |
|||
resultMap.put(attrName, attrValue); |
|||
} |
|||
// 添加其他你需要映射的属性 |
|||
return resultMap; |
|||
} |
|||
}; |
|||
|
|||
//查询用户信息 |
|||
List<Map<String, String>> resultList = ldapTemplate.search( |
|||
LdapQueryBuilder.query().where("objectClass").isPresent() |
|||
.and("sAMAccountName").is(username), |
|||
mapper); |
|||
//判断用户是否能找到 |
|||
if(resultList.size() > 0) { |
|||
try { |
|||
ldapTemplate.authenticate(LdapQueryBuilder.query().where("objectClass").isPresent() |
|||
.and("sAMAccountName").is(username), password); |
|||
} catch (Exception e) { |
|||
e.printStackTrace(); |
|||
throw new RuntimeException("账号或者密码有误!"); |
|||
} |
|||
}else { |
|||
throw new RuntimeException("查无此账号!"); |
|||
} |
|||
return resultList.get(0); |
|||
} |
|||
|
|||
} |
|||
Write
Preview
Loading…
Cancel
Save
Reference in new issue