diff --git a/framework/gateway/src/main/java/io/metersphere/gateway/service/LdapService.java b/framework/gateway/src/main/java/io/metersphere/gateway/service/LdapService.java index 9332b9ab33..5a6370e0c1 100644 --- a/framework/gateway/src/main/java/io/metersphere/gateway/service/LdapService.java +++ b/framework/gateway/src/main/java/io/metersphere/gateway/service/LdapService.java @@ -3,6 +3,7 @@ package io.metersphere.gateway.service; import io.metersphere.base.domain.User; import io.metersphere.commons.constants.ParamConstants; +import io.metersphere.commons.constants.SessionConstants; import io.metersphere.commons.constants.UserSource; import io.metersphere.commons.exception.MSException; import io.metersphere.commons.user.SessionUser; @@ -102,7 +103,7 @@ public class LdapService { } user.setSource(UserSource.LDAP.name()); - u = userLoginService.addLdapUser(user); + userLoginService.addLdapUser(user); } else { // 更新 u.setName(name); @@ -111,14 +112,16 @@ public class LdapService { userLoginService.updateUser(u); } - session.getAttributes().put("authenticate", UserSource.LDAP.name()); - session.getAttributes().put("email", email); - // 执行 LocalRealm 中 LDAP 登录逻辑 LoginRequest loginRequest = new LoginRequest(); loginRequest.setUsername(userId); loginRequest.setAuthenticate(UserSource.LDAP.name()); - return userLoginService.login(loginRequest, session, locale); + + Optional sessionUser = userLoginService.login(loginRequest, session, locale); + session.getAttributes().put("authenticate", UserSource.LDAP.name()); + session.getAttributes().put("email", email); + + return sessionUser; } private boolean authenticate(String dn, String credentials, LdapTemplate ldapTemplate) throws AuthenticationException { diff --git a/framework/gateway/src/main/java/io/metersphere/gateway/service/SSOService.java b/framework/gateway/src/main/java/io/metersphere/gateway/service/SSOService.java index a4496d51fe..987ab2fad2 100644 --- a/framework/gateway/src/main/java/io/metersphere/gateway/service/SSOService.java +++ b/framework/gateway/src/main/java/io/metersphere/gateway/service/SSOService.java @@ -156,7 +156,6 @@ public class SSOService { Optional userOptional = userLoginService.login(loginRequest, session, locale); session.getAttributes().put("authenticate", authSource.getType()); session.getAttributes().put("authId", authSource.getId()); - session.getAttributes().put(SessionConstants.ATTR_USER, userOptional.get()); return userOptional; } @@ -196,7 +195,6 @@ public class SSOService { Optional userOptional = userLoginService.login(loginRequest, session, locale); session.getAttributes().put("authenticate", authSource.getType()); session.getAttributes().put("authId", authSource.getId()); - session.getAttributes().put(SessionConstants.ATTR_USER, userOptional.get()); session.getAttributes().put("casTicket", ticket); // 记录cas对应关系 Long timeout = env.getProperty("spring.session.timeout", Long.class); diff --git a/framework/gateway/src/main/java/io/metersphere/gateway/service/UserLoginService.java b/framework/gateway/src/main/java/io/metersphere/gateway/service/UserLoginService.java index 9c986cefeb..b81ce90df6 100644 --- a/framework/gateway/src/main/java/io/metersphere/gateway/service/UserLoginService.java +++ b/framework/gateway/src/main/java/io/metersphere/gateway/service/UserLoginService.java @@ -50,7 +50,7 @@ public class UserLoginService { userDTO = loginSsoMode(request.getUsername(), request.getAuthenticate()); break; case "LDAP": - userDTO = loginLdapMode(request.getUsername(), request.getAuthenticate()); + userDTO = loginLdapMode(request.getUsername()); break; default: userDTO = loginLocalMode(request.getUsername(), request.getPassword()); @@ -62,8 +62,12 @@ public class UserLoginService { return Optional.of(sessionUser); } - private UserDTO loginLdapMode(String userId, String authenticate) { - return getLoginUser(userId, Collections.singletonList(authenticate)); + private UserDTO loginLdapMode(String userId) { + UserDTO loginUser = getLoginUser(userId, Collections.singletonList(UserSource.LDAP.name())); + if (loginUser == null) { + MSException.throwException(Translator.get("user_not_found_or_not_unique")); + } + return loginUser; } private UserDTO loginSsoMode(String userId, String authType) {