refactor(LDAP): 用户登录优化

--story=1004504 --user=lyh
LDAP用户登录优化
https://www.tapd.cn/55049933/s/1090284
This commit is contained in:
shiziyuan9527 2022-01-11 10:30:00 +08:00 committed by fit2-zhao
parent 31067e9f41
commit ed921bdef0
2 changed files with 11 additions and 15 deletions

View File

@ -63,9 +63,6 @@ public class LdapService {
// 检查属性是否存在
getMappingAttr("name", dirContextOperations);
getMappingAttr("email", dirContextOperations);
return dirContextOperations;
}
@ -85,17 +82,12 @@ public class LdapService {
}
DirContextOperations dirContext = authenticate(request);
String email = getMappingAttr("email", dirContext);
String email = getNotRequiredMappingAttr("email", dirContext);
String userId = getMappingAttr("username", dirContext);
SecurityUtils.getSubject().getSession().setAttribute("authenticate", UserSource.LDAP.name());
SecurityUtils.getSubject().getSession().setAttribute("email", email);
if (StringUtils.isBlank(email)) {
MSException.throwException(Translator.get("login_fail_email_null"));
}
// userId email 有一个相同即为存在本地用户
User u = userService.selectUser(userId, email);
String name = getMappingAttr("name", dirContext);
@ -106,6 +98,9 @@ public class LdapService {
User user = new User();
user.setId(userId);
user.setName(name);
if (StringUtils.isBlank(email)) {
email = userId + "@localhost.localhost";
}
user.setEmail(email);
if (StringUtils.isNotBlank(phone)) {

View File

@ -164,15 +164,16 @@ public class UserService {
public User selectUser(String userId, String email) {
User user = userMapper.selectByPrimaryKey(userId);
if (user == null) {
UserExample example = new UserExample();
example.createCriteria().andEmailEqualTo(email);
List<User> users = userMapper.selectByExample(example);
if (!CollectionUtils.isEmpty(users)) {
return users.get(0);
if (StringUtils.isNotBlank(email)) {
UserExample example = new UserExample();
example.createCriteria().andEmailEqualTo(email);
List<User> users = userMapper.selectByExample(example);
if (!CollectionUtils.isEmpty(users)) {
return users.get(0);
}
}
}
return user;
}
private void checkUserParam(User user) {