From bd51abfe1fbd03f036e66fd0e29cccf98f209eca Mon Sep 17 00:00:00 2001 From: CaptainB Date: Tue, 25 Oct 2022 18:42:05 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BF=AE=E5=A4=8Dldap=E7=99=BB=E5=BD=95?= =?UTF-8?q?=E6=97=B6=E5=AD=98=E5=9C=A8=E6=9C=AC=E5=9C=B0=E5=90=8C=E5=90=8D?= =?UTF-8?q?=E7=94=A8=E6=88=B7=E7=9A=84=E6=8A=A5=E9=94=99=E6=8F=90=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../io/metersphere/gateway/service/LdapService.java | 13 ++++++++----- .../io/metersphere/gateway/service/SSOService.java | 2 -- .../gateway/service/UserLoginService.java | 10 +++++++--- 3 files changed, 15 insertions(+), 10 deletions(-) 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) {