Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in / Register
Toggle navigation
Y
yifu-mvp
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
fangxinjiang
yifu-mvp
Commits
cecf1e30
Commit
cecf1e30
authored
Jun 17, 2022
by
huyuchen
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
huyc ldap相关代码提交
parent
02c3f3d8
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
150 additions
and
1 deletion
+150
-1
LdapUtil.java
...om/yifu/cloud/plus/v1/yifu/common/ldap/util/LdapUtil.java
+94
-0
pom.xml
yifu-job/yifu-job-api/pom.xml
+6
-0
DeptController.java
...u.cloud.plus.v1/yifu/admin/controller/DeptController.java
+11
-0
UserController.java
...u.cloud.plus.v1/yifu/admin/controller/UserController.java
+21
-0
SysUserService.java
...yifu.cloud.plus.v1/yifu/admin/service/SysUserService.java
+6
-0
SysUserServiceImpl.java
...d.plus.v1/yifu/admin/service/impl/SysUserServiceImpl.java
+12
-1
No files found.
yifu-common/yifu-common-ldap/src/main/java/com/yifu/cloud/plus/v1/yifu/common/ldap/util/LdapUtil.java
View file @
cecf1e30
...
...
@@ -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
;
}
}
yifu-job/yifu-job-api/pom.xml
View file @
cecf1e30
...
...
@@ -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>
...
...
yifu-upms/yifu-upms-biz/src/main/java/com/yifu.cloud.plus.v1/yifu/admin/controller/DeptController.java
View file @
cecf1e30
...
...
@@ -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
());
}
}
yifu-upms/yifu-upms-biz/src/main/java/com/yifu.cloud.plus.v1/yifu/admin/controller/UserController.java
View file @
cecf1e30
...
...
@@ -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
);
}
}
yifu-upms/yifu-upms-biz/src/main/java/com/yifu.cloud.plus.v1/yifu/admin/service/SysUserService.java
View file @
cecf1e30
...
...
@@ -128,4 +128,10 @@ public interface SysUserService extends IService<SysUser> {
*/
R
updateByLdap
();
/**
* 从ldap中验证登录用户
* @return R
*/
R
<
Boolean
>
loginAuthentication
(
String
userName
,
String
password
);
}
yifu-upms/yifu-upms-biz/src/main/java/com/yifu.cloud.plus.v1/yifu/admin/service/impl/SysUserServiceImpl.java
View file @
cecf1e30
...
...
@@ -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
(
"验证不通过"
);
}
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment