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
ba3db5e0
Commit
ba3db5e0
authored
Jun 17, 2022
by
fangxinjiang
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/develop' into develop
parents
997d98bb
9764881b
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
365 additions
and
1 deletion
+365
-1
LdapCheck.java
...m/yifu/cloud/plus/v1/yifu/common/ldap/util/LdapCheck.java
+121
-0
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
updateUserTask.java
...va/com/yifu/cloud/plus/v1/job/compont/updateUserTask.java
+53
-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
SysDeptMapper.xml
...yifu-upms-biz/src/main/resources/mapper/SysDeptMapper.xml
+41
-0
No files found.
yifu-common/yifu-common-ldap/src/main/java/com/yifu/cloud/plus/v1/yifu/common/ldap/util/LdapCheck.java
0 → 100644
View file @
ba3db5e0
package
com
.
yifu
.
cloud
.
plus
.
v1
.
yifu
.
common
.
ldap
.
util
;
import
javax.naming.AuthenticationException
;
import
javax.naming.Context
;
import
javax.naming.NamingEnumeration
;
import
javax.naming.NamingException
;
import
javax.naming.directory.SearchControls
;
import
javax.naming.directory.SearchResult
;
import
javax.naming.ldap.Control
;
import
javax.naming.ldap.InitialLdapContext
;
import
javax.naming.ldap.LdapContext
;
import
java.util.Hashtable
;
/**
* 用户登陆认证,LDAP跨域认证,通过LDAP对用户进行更新
*
*/
public
class
LdapCheck
{
private
static
LdapContext
ctx
=
null
;
private
static
Control
[]
connCtls
=
null
;
/**** 定义LDAP的基本连接信息 ******/
// LDAP的连接地址(ldap://ip:port/)port默认为389
private
static
String
URL
=
"192.168.1.65"
;
//port
private
static
String
port
=
"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!"
;
// 校验用户名密码的方法
public
static
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
)
{
System
.
out
.
println
(
userDN
+
" is not authenticated"
);
System
.
out
.
println
(
e
.
toString
());
valide
=
false
;
}
catch
(
NamingException
e
)
{
System
.
out
.
println
(
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
+
":"
+
port
+
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
(
AuthenticationException
e
)
{
System
.
out
.
println
(
"Authentication faild: "
+
e
.
toString
());
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"Something wrong while authenticating: "
+
e
.
toString
());
}
}
public
static
void
closeCtx
()
{
try
{
if
(
ctx
!=
null
)
ctx
.
close
();
}
catch
(
NamingException
ex
)
{
}
}
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
);
if
(
en
==
null
)
{
System
.
out
.
println
(
"Have no NamingEnumeration."
);
}
if
(!
en
.
hasMoreElements
())
{
System
.
out
.
println
(
"Have no element."
);
}
while
(
en
!=
null
&&
en
.
hasMoreElements
())
{
Object
obj
=
en
.
nextElement
();
if
(
obj
instanceof
SearchResult
)
{
SearchResult
si
=
(
SearchResult
)
obj
;
userDN
+=
si
.
getName
();
userDN
+=
","
+
BASEDN
;
}
else
{
System
.
out
.
println
(
obj
);
}
System
.
out
.
println
();
}
}
catch
(
Exception
e
)
{
System
.
out
.
println
(
"Exception in search():"
+
e
);
}
return
userDN
;
}
}
\ No newline at end of file
yifu-common/yifu-common-ldap/src/main/java/com/yifu/cloud/plus/v1/yifu/common/ldap/util/LdapUtil.java
View file @
ba3db5e0
...
...
@@ -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 @
ba3db5e0
...
...
@@ -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-job/yifu-job-api/src/main/java/com/yifu/cloud/plus/v1/job/compont/updateUserTask.java
0 → 100644
View file @
ba3db5e0
package
com
.
yifu
.
cloud
.
plus
.
v1
.
job
.
compont
;
import
com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDept
;
import
com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysUser
;
import
com.yifu.cloud.plus.v1.yifu.common.core.constant.SecurityConstants
;
import
com.yifu.cloud.plus.v1.yifu.common.dapr.config.DaprUpmsProperties
;
import
com.yifu.cloud.plus.v1.yifu.common.dapr.util.HttpDaprUtil
;
import
lombok.extern.slf4j.Slf4j
;
import
org.springframework.beans.factory.annotation.Autowired
;
import
org.springframework.stereotype.Component
;
/**
* @Author huyc
* @Description 定时任务具体执行类
* @Date 10:48 2022/6/16
* @Param
* @return
**/
@Component
(
value
=
"updateUserTask"
)
@Slf4j
public
class
updateUserTask
{
@Autowired
private
DaprUpmsProperties
daprUpmsProperties
;
/**
* @Author huyc
* @Description
* @Date 15:49 2022/6/16
* @Param
* @return
**/
public
void
updateUserByLdap
()
throws
Exception
{
log
.
info
(
"-------------更新用户-定时任务开始------------"
);
HttpDaprUtil
.
invokeMethodPost
(
daprUpmsProperties
.
getAppUrl
(),
daprUpmsProperties
.
getAppId
(),
"/user/inner/updateUser"
,
""
,
SysUser
.
class
,
SecurityConstants
.
FROM_IN
);
log
.
info
(
"------------更新用户-定时任务结束------------"
);
}
/**
* @Author huyc
* @Description
* @Date 16:32 2022/6/16
* @Param
* @return
**/
public
void
updateDeptByLdap
()
throws
Exception
{
log
.
info
(
"-------------更新部门-定时任务开始------------"
);
HttpDaprUtil
.
invokeMethodPost
(
daprUpmsProperties
.
getAppUrl
(),
daprUpmsProperties
.
getAppId
(),
"/dept/inner/updateDept"
,
""
,
SysDept
.
class
,
SecurityConstants
.
FROM_IN
);
log
.
info
(
"------------更新部门-定时任务结束------------"
);
}
}
yifu-upms/yifu-upms-biz/src/main/java/com/yifu.cloud.plus.v1/yifu/admin/controller/DeptController.java
View file @
ba3db5e0
...
...
@@ -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 @
ba3db5e0
...
...
@@ -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 @
ba3db5e0
...
...
@@ -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 @
ba3db5e0
...
...
@@ -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
(
"验证不通过"
);
}
}
yifu-upms/yifu-upms-biz/src/main/resources/mapper/SysDeptMapper.xml
0 → 100644
View file @
ba3db5e0
<?xml version="1.0" encoding="UTF-8"?>
<!--
~ Copyright (c) 2020 yifu4cloud Authors. All Rights Reserved.
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper
namespace=
"com.yifu.cloud.plus.v1.yifu.admin.mapper.SysDeptMapper"
>
<!-- 通用查询映射结果 -->
<resultMap
id=
"BaseResultMap"
type=
"com.yifu.cloud.plus.v1.yifu.admin.api.entity.SysDept"
>
<id
column=
"deptId"
property=
"deptId"
/>
<result
column=
"name"
property=
"name"
/>
<result
column=
"parent_id"
property=
"parentId"
/>
<result
column=
"dept_dn"
property=
"deptDn"
/>
<result
column=
"sort_order"
property=
"sortOrder"
/>
<result
column=
"create_by"
property=
"createBy"
/>
<result
column=
"create_time"
property=
"createTime"
/>
<result
column=
"update_by"
property=
"updateBy"
/>
<result
column=
"update_time"
property=
"updateTime"
/>
<result
column=
"del_flag"
property=
"delFlag"
/>
</resultMap>
<!--查询所有dn-->
<select
id=
"selectDnList"
resultType=
"String"
>
SELECT a.dept_dn
FROM sys_dept a
WHERE a.del_flag = 0
AND a.dept_dn IS NOT NULL
</select>
</mapper>
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