From 28b40da1b134fb54296ef9d1a8b13268f289fbf0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=97=BA=E9=98=B3?= Date: Wed, 20 Jul 2022 18:48:16 +0800 Subject: [PATCH] [Improve] Hide Change Pwd Page When Login With LDAP (#11041) * hide-change-pwd-page-when-ldap-login * add unit test --- .../api/security/SecurityConfig.java | 4 ++++ .../api/security/impl/AbstractAuthenticator.java | 15 +++++++++++++-- .../api/controller/LoginControllerTest.java | 6 ++++++ .../apache/dolphinscheduler/common/Constants.java | 9 +++++++++ 4 files changed, 32 insertions(+), 2 deletions(-) diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/security/SecurityConfig.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/security/SecurityConfig.java index ddb6f853df..6a17068e22 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/security/SecurityConfig.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/security/SecurityConfig.java @@ -72,4 +72,8 @@ public class SecurityConfig { beanFactory.autowireBean(authenticator); return authenticator; } + + public String getType() { + return type; + } } diff --git a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/security/impl/AbstractAuthenticator.java b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/security/impl/AbstractAuthenticator.java index 837e1f1f79..9f3ff8afcf 100644 --- a/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/security/impl/AbstractAuthenticator.java +++ b/dolphinscheduler-api/src/main/java/org/apache/dolphinscheduler/api/security/impl/AbstractAuthenticator.java @@ -19,6 +19,7 @@ package org.apache.dolphinscheduler.api.security.impl; import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.security.Authenticator; +import org.apache.dolphinscheduler.api.security.SecurityConfig; import org.apache.dolphinscheduler.api.service.SessionService; import org.apache.dolphinscheduler.api.service.UsersService; import org.apache.dolphinscheduler.api.utils.Result; @@ -27,7 +28,7 @@ import org.apache.dolphinscheduler.common.enums.Flag; import org.apache.dolphinscheduler.dao.entity.Session; import org.apache.dolphinscheduler.dao.entity.User; -import java.util.Collections; +import java.util.HashMap; import java.util.Map; import javax.servlet.http.HttpServletRequest; @@ -41,9 +42,13 @@ public abstract class AbstractAuthenticator implements Authenticator { @Autowired private UsersService userService; + @Autowired private SessionService sessionService; + @Autowired + private SecurityConfig securityConfig; + /** * user login and return user in db * @@ -78,8 +83,14 @@ public abstract class AbstractAuthenticator implements Authenticator { result.setMsg(Status.LOGIN_SESSION_FAILED.getMsg()); return result; } + logger.info("sessionId : {}", sessionId); - result.setData(Collections.singletonMap(Constants.SESSION_ID, sessionId)); + + Map data = new HashMap<>(); + data.put(Constants.SESSION_ID, sessionId); + data.put(Constants.SECURITY_CONFIG_TYPE, securityConfig.getType()); + + result.setData(data); result.setCode(Status.SUCCESS.getCode()); result.setMsg(Status.LOGIN_SUCCESS.getMsg()); return result; diff --git a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/LoginControllerTest.java b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/LoginControllerTest.java index 8d82363662..0e4cad9c27 100644 --- a/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/LoginControllerTest.java +++ b/dolphinscheduler-api/src/test/java/org/apache/dolphinscheduler/api/controller/LoginControllerTest.java @@ -23,8 +23,11 @@ import static org.springframework.test.web.servlet.result.MockMvcResultMatchers. import org.apache.dolphinscheduler.api.enums.Status; import org.apache.dolphinscheduler.api.utils.Result; +import org.apache.dolphinscheduler.common.Constants; import org.apache.dolphinscheduler.common.utils.JSONUtils; +import java.util.Map; + import org.junit.Assert; import org.junit.Test; import org.slf4j.Logger; @@ -55,6 +58,9 @@ public class LoginControllerTest extends AbstractControllerTest { Result result = JSONUtils.parseObject(mvcResult.getResponse().getContentAsString(), Result.class); Assert.assertEquals(Status.SUCCESS.getCode(),result.getCode().intValue()); logger.info(mvcResult.getResponse().getContentAsString()); + Map data = (Map) result.getData(); + Assert.assertEquals(Constants.SECURITY_CONFIG_TYPE_PASSWORD,data.get(Constants.SECURITY_CONFIG_TYPE)); + Assert.assertNotEquals(Constants.SECURITY_CONFIG_TYPE_LDAP,data.get(Constants.SECURITY_CONFIG_TYPE)); } @Test diff --git a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java index 7a84e95a13..9b9a9fb9c2 100644 --- a/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java +++ b/dolphinscheduler-common/src/main/java/org/apache/dolphinscheduler/common/Constants.java @@ -842,4 +842,13 @@ public final class Constants { public static final String FUNCTION_START_WITH = "$"; public static final Integer DEFAULT_QUEUE_ID = 1; + + /** + * Security authentication types (supported types: PASSWORD,LDAP) + */ + public static final String SECURITY_CONFIG_TYPE = "securityConfigType"; + + public static final String SECURITY_CONFIG_TYPE_PASSWORD = "PASSWORD"; + + public static final String SECURITY_CONFIG_TYPE_LDAP = "LDAP"; }