Commit cecf1e30 authored by huyuchen's avatar huyuchen

huyc ldap相关代码提交

parent 02c3f3d8
......@@ -8,7 +8,16 @@ import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ldap.core.LdapTemplate;
import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.SearchControls;
import javax.naming.ldap.Control;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import static org.springframework.ldap.query.LdapQueryBuilder.query;
......@@ -19,6 +28,21 @@ public class LdapUtil {
@Autowired
private LdapTemplate ldapTemplate;
private static LdapContext ctx = null;
private static Control[] connCtls = null;
/**** 定义LDAP的基本连接信息 ******/
// LDAP的连接地址(ldap://ip:port/)port默认为389
private static String URL = "ldap://192.168.1.65:389/";
// LDAP的根DN
private static String BASEDN = "dc=worfu,dc=com";
// LDAP的连接账号(身份认证管理平台添加的应用账号,应用账号格式:uid=?,ou=?,dc=????)
private static String PRINCIPAL = "cn=admin,dc=worfu,dc=com";
// LDAP的连接账号的密码(身份认证管理平台添加的应用账号的密码)
private static String PASSWORD = "yifu123456!";
/**
* @return List<SearchResultEntry>
* @author huyc
......@@ -47,4 +71,74 @@ public class LdapUtil {
return list;
}
// 校验用户名密码的方法
public Boolean authenticate(String usr, String pwd) {
boolean valide = false;
if (pwd == null || pwd == "")
return false;
if (ctx == null) {
getCtx();
}
String userDN = getUserDN(usr);
if ("".equals(userDN) || userDN == null) {
return false;
}
try {
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, userDN);
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, pwd);
ctx.reconnect(connCtls);
valide = true;
closeCtx();
} catch (AuthenticationException e) {
log.error(userDN + " is not authenticated");
valide = false;
} catch (NamingException e) {
log.error(userDN + " is not authenticated");
valide = false;
}
return valide;
}
public static void getCtx() {
if (ctx != null) {
return;
}
Hashtable<String, String> env = new Hashtable<String, String>();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, URL + BASEDN);
env.put(Context.SECURITY_AUTHENTICATION, "simple");
env.put(Context.SECURITY_PRINCIPAL, PRINCIPAL);
env.put(Context.SECURITY_CREDENTIALS, PASSWORD);
try {
// 链接ldap
ctx = new InitialLdapContext(env, connCtls);
} catch (NamingException e) {
e.printStackTrace();
}
}
public static void closeCtx() throws NamingException {
if (ctx != null)
ctx.close();
}
public static String getUserDN(String uid) {
String userDN = "";
try {
SearchControls constraints = new SearchControls();
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
NamingEnumeration<?> en = ctx.search("", "uid=" + uid, constraints);
while (en != null && en.hasMoreElements()) {
Object obj = en.nextElement();
if (obj instanceof javax.naming.directory.SearchResult) {
javax.naming.directory.SearchResult si = (javax.naming.directory.SearchResult) obj;
userDN += si.getName();
userDN += "," + BASEDN;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return userDN;
}
}
......@@ -37,6 +37,12 @@
<groupId>com.yifu.cloud.plus.v1</groupId>
<artifactId>yifu-common-mybatis</artifactId>
</dependency>
<!--dapr 依赖-->
<dependency>
<groupId>com.yifu.cloud.plus.v1</groupId>
<artifactId>yifu-common-dapr</artifactId>
<version>1.0.0</version>
</dependency>
<!-- 定时任务依赖 -->
<dependency>
<groupId>org.quartz-scheduler</groupId>
......
......@@ -143,4 +143,15 @@ public class DeptController {
public R updateDeptByLdap() {
return R.ok(sysDeptService.updateDeptByLdap());
}
/**
* 从ldap中获取部门
* @return R
*/
@SysLog("从ldap中获取部门")
@PostMapping("/inner/updateDept")
@Inner
public R innerUpdateDeptByLdap() {
return R.ok(sysDeptService.updateDeptByLdap());
}
}
......@@ -235,4 +235,25 @@ public class UserController {
public R updateByLdap() {
return R.ok(userService.updateByLdap());
}
/**
* 从ldap中获取用户
* @return R
*/
@SysLog("从ldap中获取用户")
@PostMapping("inner/updateUser")
@Inner
public R innerUpdateByLdap() {
return R.ok(userService.updateByLdap());
}
/**
* 从ldap中验证登录用户
* @return R
*/
@SysLog("登录验证")
@PostMapping("/ldapLogin")
public R<Boolean> loginAuthentication(@RequestParam String userName,@RequestParam String password) {
return userService.loginAuthentication(userName,password);
}
}
......@@ -128,4 +128,10 @@ public interface SysUserService extends IService<SysUser> {
*/
R updateByLdap();
/**
* 从ldap中验证登录用户
* @return R
*/
R<Boolean> loginAuthentication(String userName, String password);
}
......@@ -24,7 +24,6 @@ import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pig4cloud.plugin.excel.vo.ErrorMessage;
import com.unboundid.ldap.sdk.SearchResultEntry;
import com.yifu.cloud.plus.v1.yifu.admin.api.dto.UserDTO;
import com.yifu.cloud.plus.v1.yifu.admin.api.dto.UserInfo;
import com.yifu.cloud.plus.v1.yifu.admin.api.entity.*;
......@@ -429,4 +428,16 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
}
return R.ok();
}
/**
* 从ldap中验证登录用户
* @return R
*/
@Override
public R<Boolean> loginAuthentication(String userName, String password) {
if (ldapUtil.authenticate(userName,password)) {
return R.ok();
}
return R.failed("验证不通过");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment