Skip to content

Commit 05022f8

Browse files
authored
Merge pull request #442 from Huyueeer/devLookUserName
LDAP忽略大小写 & 认证携带必要元信息(姓名、部门、邮箱)
2 parents 10a27bc + 542e5d3 commit 05022f8

File tree

9 files changed

+142
-21
lines changed

9 files changed

+142
-21
lines changed

distribution/conf/create_mysql_table.sql

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ CREATE TABLE `account` (
1616
`status` int(16) NOT NULL DEFAULT '0' COMMENT '0标识使用中,-1标识已废弃',
1717
`gmt_create` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
1818
`gmt_modify` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '修改时间',
19+
`department` varchar(128) DEFAULT '' COMMENT '部门名',
20+
`display_name` varchar(128) DEFAULT '' COMMENT '用户姓名',
21+
`mail` varchar(128) DEFAULT '' COMMENT '邮箱',
1922
PRIMARY KEY (`id`),
2023
UNIQUE KEY `uniq_username` (`username`)
2124
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT='账号表';

kafka-manager-common/src/main/java/com/xiaojukeji/kafka/manager/common/entity/dto/rd/AccountDTO.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,15 @@ public class AccountDTO {
2121
@ApiModelProperty(value = "角色")
2222
private Integer role;
2323

24+
@ApiModelProperty(value = "用户姓名")
25+
private String displayName;
26+
27+
@ApiModelProperty(value = "部门")
28+
private String department;
29+
30+
@ApiModelProperty(value = "邮箱")
31+
private String mail;
32+
2433
public String getUsername() {
2534
return username;
2635
}
@@ -45,12 +54,39 @@ public void setRole(Integer role) {
4554
this.role = role;
4655
}
4756

57+
public String getDisplayName() {
58+
return displayName;
59+
}
60+
61+
public void setDisplayName(String displayName) {
62+
this.displayName = displayName;
63+
}
64+
65+
public String getDepartment() {
66+
return department;
67+
}
68+
69+
public void setDepartment(String department) {
70+
this.department = department;
71+
}
72+
73+
public String getMail() {
74+
return mail;
75+
}
76+
77+
public void setMail(String mail) {
78+
this.mail = mail;
79+
}
80+
4881
@Override
4982
public String toString() {
5083
return "AccountDTO{" +
5184
"username='" + username + '\'' +
5285
", password='" + password + '\'' +
5386
", role=" + role +
87+
", displayName='" + displayName + '\'' +
88+
", department='" + department + '\'' +
89+
", mail='" + mail + '\'' +
5490
'}';
5591
}
5692

kafka-manager-common/src/main/java/com/xiaojukeji/kafka/manager/common/entity/pojo/AccountDO.java

Lines changed: 37 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ public class AccountDO {
2121

2222
private Integer role;
2323

24+
private String displayName;
25+
26+
private String department;
27+
28+
private String mail;
29+
2430
public String getUsername() {
2531
return username;
2632
}
@@ -45,16 +51,43 @@ public void setRole(Integer role) {
4551
this.role = role;
4652
}
4753

54+
public String getDisplayName() {
55+
return displayName;
56+
}
57+
58+
public void setDisplayName(String displayName) {
59+
this.displayName = displayName;
60+
}
61+
62+
public String getDepartment() {
63+
return department;
64+
}
65+
66+
public void setDepartment(String department) {
67+
this.department = department;
68+
}
69+
70+
public String getMail() {
71+
return mail;
72+
}
73+
74+
public void setMail(String mail) {
75+
this.mail = mail;
76+
}
77+
4878
@Override
4979
public String toString() {
5080
return "AccountDO{" +
51-
"username='" + username + '\'' +
52-
", password='" + password + '\'' +
53-
", role=" + role +
54-
", id=" + id +
81+
"id=" + id +
5582
", status=" + status +
5683
", gmtCreate=" + gmtCreate +
5784
", gmtModify=" + gmtModify +
85+
", username='" + username + '\'' +
86+
", password='" + password + '\'' +
87+
", role=" + role +
88+
", displayName='" + displayName + '\'' +
89+
", department='" + department + '\'' +
90+
", mail='" + mail + '\'' +
5891
'}';
5992
}
6093
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.xiaojukeji.kafka.manager.common.utils;
2+
3+
/**
4+
* @className: SplitUtils
5+
* @description: Split string of type keyValue
6+
* @author: Hu.Yue
7+
* @date: 2021/8/4
8+
**/
9+
public class SplitUtils {
10+
11+
public static String keyValueSplit(String keyValue){
12+
return keyValue.split(":\\s+")[1];
13+
}
14+
}

kafka-manager-dao/src/main/resources/mapper/AccountDao.xml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,17 @@
1010
<result property="role" column="role" />
1111
<result property="gmtCreate" column="gmt_create" />
1212
<result property="gmtModify" column="gmt_modify" />
13+
<result property="displayName" column="display_name" />
14+
<result property="department" column="department" />
15+
<result property="mail" column="mail" />
1316
</resultMap>
1417

1518
<insert id="insert" parameterType="com.xiaojukeji.kafka.manager.common.entity.pojo.AccountDO">
1619
<![CDATA[
1720
REPLACE account
18-
(username, password, role)
21+
(username, password, role, display_name, department, mail)
1922
VALUES
20-
(#{username}, #{password}, #{role})
23+
(#{username}, #{password}, #{role}, #{displayName}, #{department}, #{mail})
2124
]]>
2225
</insert>
2326

kafka-manager-extends/kafka-manager-account/src/main/java/com/xiaojukeji/kafka/manager/account/component/account/BaseEnterpriseStaffService.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,8 @@ public List<EnterpriseStaff> searchEnterpriseStaffByKeyWord(String keyWord) {
5454
}
5555
List<EnterpriseStaff> staffList = new ArrayList<>();
5656
for (AccountDO accountDO: doList) {
57-
staffList.add(new EnterpriseStaff(accountDO.getUsername(), accountDO.getUsername(), ""));
57+
//这里对chineseName填充共识的displayName,Department则获取Department信息
58+
staffList.add(new EnterpriseStaff(accountDO.getUsername(), accountDO.getDisplayName(), accountDO.getDepartment()));
5859
}
5960
return staffList;
6061
} catch (Exception e) {

kafka-manager-extends/kafka-manager-account/src/main/java/com/xiaojukeji/kafka/manager/account/component/ldap/LdapAuthentication.java

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.xiaojukeji.kafka.manager.account.component.ldap;
22

3+
import com.xiaojukeji.kafka.manager.common.utils.SplitUtils;
34
import com.xiaojukeji.kafka.manager.common.utils.ValidateUtils;
45
import org.slf4j.Logger;
56
import org.slf4j.LoggerFactory;
@@ -14,7 +15,9 @@
1415
import javax.naming.directory.SearchResult;
1516
import javax.naming.ldap.InitialLdapContext;
1617
import javax.naming.ldap.LdapContext;
18+
import java.util.HashMap;
1719
import java.util.Hashtable;
20+
import java.util.Map;
1821

1922
@Component
2023
public class LdapAuthentication {
@@ -60,16 +63,19 @@ private LdapContext getLdapContext() {
6063
return null;
6164
}
6265

63-
private String getUserDN(String account, LdapContext ctx) {
66+
private Map<String, Object> getLdapAttrsInfo(String account, LdapContext ctx) {
67+
//存储更多的LDAP元信息
68+
Map<String, Object> ldapAttrsInfo = new HashMap<>();
6469
String userDN = "";
70+
ldapAttrsInfo.clear();
6571
try {
6672
SearchControls constraints = new SearchControls();
6773
constraints.setSearchScope(SearchControls.SUBTREE_SCOPE);
6874
String filter = "(&(objectClass=*)("+ldapFilter+"=" + account + "))";
6975

7076
NamingEnumeration<SearchResult> en = ctx.search("", filter, constraints);
7177
if (en == null || !en.hasMoreElements()) {
72-
return "";
78+
return null;
7379
}
7480
// maybe more than one element
7581
while (en.hasMoreElements()) {
@@ -78,13 +84,25 @@ private String getUserDN(String account, LdapContext ctx) {
7884
SearchResult si = (SearchResult) obj;
7985
userDN += si.getName();
8086
userDN += "," + ldapBasedn;
87+
//携带LDAP更多元信息以填充用户元信息
88+
ldapAttrsInfo.put("userDN", userDN);
89+
ldapAttrsInfo.put("sAMAccountName",
90+
SplitUtils.keyValueSplit(si.getAttributes().get("samaccountname").toString()));
91+
ldapAttrsInfo.put("department",
92+
SplitUtils.keyValueSplit(si.getAttributes().get("department").toString()));
93+
ldapAttrsInfo.put("company",
94+
SplitUtils.keyValueSplit(si.getAttributes().get("company").toString()));
95+
ldapAttrsInfo.put("displayName",
96+
SplitUtils.keyValueSplit(si.getAttributes().get("displayname").toString()));
97+
ldapAttrsInfo.put("mail",
98+
SplitUtils.keyValueSplit(si.getAttributes().get("mail").toString()));
8199
break;
82100
}
83101
}
84102
} catch (Exception e) {
85103
LOGGER.error("class=LdapAuthentication||method=getUserDN||account={}||errMsg={}", account, e);
86104
}
87-
return userDN;
105+
return ldapAttrsInfo;
88106
}
89107

90108
/**
@@ -93,23 +111,23 @@ private String getUserDN(String account, LdapContext ctx) {
93111
* @param password
94112
* @return
95113
*/
96-
public boolean authenticate(String account, String password) {
114+
public Map<String, Object> authenticate(String account, String password) {
97115
LdapContext ctx = getLdapContext();
98116
if (ValidateUtils.isNull(ctx)) {
99-
return false;
117+
return null;
100118
}
101119

102120
try {
103-
String userDN = getUserDN(account, ctx);
104-
if(ValidateUtils.isBlank(userDN)){
105-
return false;
121+
Map<String, Object> ldapAttrsInfo = getLdapAttrsInfo(account, ctx);
122+
if(ValidateUtils.isNull(ldapAttrsInfo)){
123+
return null;
106124
}
107125

108-
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, userDN);
126+
ctx.addToEnvironment(Context.SECURITY_PRINCIPAL, ldapAttrsInfo.get("userDN").toString());
109127
ctx.addToEnvironment(Context.SECURITY_CREDENTIALS, password);
110128
ctx.reconnect(null);
111129

112-
return true;
130+
return ldapAttrsInfo;
113131
} catch (AuthenticationException e) {
114132
LOGGER.warn("class=LdapAuthentication||method=authenticate||account={}||errMsg={}", account, e);
115133
} catch (NamingException e) {
@@ -125,6 +143,6 @@ public boolean authenticate(String account, String password) {
125143
}
126144
}
127145
}
128-
return false;
146+
return null;
129147
}
130148
}

kafka-manager-extends/kafka-manager-account/src/main/java/com/xiaojukeji/kafka/manager/account/component/sso/BaseSessionSignOn.java

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import javax.servlet.http.HttpServletRequest;
1919
import javax.servlet.http.HttpServletResponse;
20+
import java.util.Map;
2021

2122
/**
2223
* @author zengqiao
@@ -47,27 +48,36 @@ public Result<String> loginAndGetLdap(HttpServletRequest request, HttpServletRes
4748
if (ValidateUtils.isBlank(dto.getUsername()) || ValidateUtils.isNull(dto.getPassword())) {
4849
return Result.buildFailure("Missing parameters");
4950
}
50-
51-
Result<AccountDO> accountResult = accountService.getAccountDO(dto.getUsername());
51+
//先创建空对象,看是在LDAP去做填充,还是直接查表填充
52+
Result<AccountDO> accountResult;
5253

5354
//判断是否激活了LDAP验证, 若激活则也可使用ldap进行认证
5455
if(!ValidateUtils.isNull(accountLdapEnabled) && accountLdapEnabled){
5556
//去LDAP验证账密
56-
if(!ldapAuthentication.authenticate(dto.getUsername(),dto.getPassword())){
57+
Map<String, Object> ldapAttrsInfo = ldapAuthentication.authenticate(dto.getUsername(),dto.getPassword());;
58+
if(ValidateUtils.isNull(ldapAttrsInfo)){
5759
return Result.buildFrom(ResultStatus.LDAP_AUTHENTICATION_FAILED);
5860
}
61+
//LDAP验证通过,拿LDAP的sAMAccountName替换dto对象的值,便于第一次自动注册采用LDAP值,并且第二次也避免REPLACE
62+
dto.setUsername(ldapAttrsInfo.get("sAMAccountName").toString());
63+
accountResult = accountService.getAccountDO(dto.getUsername());
5964

6065
if((ValidateUtils.isNull(accountResult) || ValidateUtils.isNull(accountResult.getData())) && authUserRegistration){
6166
//自动注册
6267
AccountDO accountDO = new AccountDO();
6368
accountDO.setUsername(dto.getUsername());
6469
accountDO.setRole(AccountRoleEnum.getUserRoleEnum(authUserRegistrationRole).getRole());
6570
accountDO.setPassword(dto.getPassword());
71+
accountDO.setDisplayName(ldapAttrsInfo.get("displayName").toString());
72+
accountDO.setDepartment(ldapAttrsInfo.get("department").toString());
73+
accountDO.setMail(ldapAttrsInfo.get("mail").toString());
6674
accountService.createAccount(accountDO);
6775
}
6876

6977
return Result.buildSuc(dto.getUsername());
7078
}
79+
//不走LDAP认证直接查表填充
80+
accountResult = accountService.getAccountDO(dto.getUsername());
7181

7282
if (ValidateUtils.isNull(accountResult) || accountResult.failed()) {
7383
return new Result<>(accountResult.getCode(), accountResult.getMessage());

kafka-manager-web/src/main/java/com/xiaojukeji/kafka/manager/web/converters/AccountConverter.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ public static AccountDO convert2AccountDO(AccountDTO dto) {
1818
accountDO.setUsername(dto.getUsername());
1919
accountDO.setPassword(dto.getPassword());
2020
accountDO.setRole(dto.getRole());
21+
accountDO.setDepartment(dto.getDepartment());
22+
accountDO.setMail(dto.getMail());
23+
accountDO.setDisplayName(dto.getDisplayName());
2124
return accountDO;
2225
}
2326

0 commit comments

Comments
 (0)