LdapService.java 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.dolphinscheduler.api.security.impl.ldap;
  18. import org.apache.dolphinscheduler.common.enums.UserType;
  19. import java.util.Properties;
  20. import javax.naming.Context;
  21. import javax.naming.NamingEnumeration;
  22. import javax.naming.NamingException;
  23. import javax.naming.directory.Attribute;
  24. import javax.naming.directory.InitialDirContext;
  25. import javax.naming.directory.SearchControls;
  26. import javax.naming.directory.SearchResult;
  27. import javax.naming.ldap.InitialLdapContext;
  28. import javax.naming.ldap.LdapContext;
  29. import org.slf4j.Logger;
  30. import org.slf4j.LoggerFactory;
  31. import org.springframework.beans.factory.annotation.Value;
  32. import org.springframework.context.annotation.Configuration;
  33. import org.springframework.stereotype.Component;
  34. @Component
  35. @Configuration
  36. public class LdapService {
  37. private static final Logger logger = LoggerFactory.getLogger(LdapService.class);
  38. @Value("${security.authentication.ldap.user.admin:null}")
  39. private String adminUserId;
  40. @Value("${ldap.urls:null}")
  41. private String ldapUrls;
  42. @Value("${ldap.base.dn:null}")
  43. private String ldapBaseDn;
  44. @Value("${ldap.username:null}")
  45. private String ldapSecurityPrincipal;
  46. @Value("${ldap.password:null}")
  47. private String ldapPrincipalPassword;
  48. @Value("${ldap.user.identity.attribute:null}")
  49. private String ldapUserIdentifyingAttribute;
  50. @Value("${ldap.user.email.attribute:null}")
  51. private String ldapEmailAttribute;
  52. /***
  53. * get user type by configured admin userId
  54. * @param userId login userId
  55. * @return user type
  56. */
  57. public UserType getUserType(String userId) {
  58. return adminUserId.equalsIgnoreCase(userId) ? UserType.ADMIN_USER : UserType.GENERAL_USER;
  59. }
  60. /**
  61. * login by userId and return user email
  62. *
  63. * @param userId user identity id
  64. * @param userPwd user login password
  65. * @return user email
  66. */
  67. public String ldapLogin(String userId, String userPwd) {
  68. Properties searchEnv = getManagerLdapEnv();
  69. try {
  70. //Connect to the LDAP server and Authenticate with a service user of whom we know the DN and credentials
  71. LdapContext ctx = new InitialLdapContext(searchEnv, null);
  72. SearchControls sc = new SearchControls();
  73. sc.setReturningAttributes(new String[]{ldapEmailAttribute});
  74. sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
  75. String searchFilter = String.format("(%s=%s)", ldapUserIdentifyingAttribute, userId);
  76. //Search for the user you want to authenticate, search him with some attribute
  77. NamingEnumeration<SearchResult> results = ctx.search(ldapBaseDn, searchFilter, sc);
  78. if (results.hasMore()) {
  79. // get the users DN (distinguishedName) from the result
  80. SearchResult result = results.next();
  81. NamingEnumeration attrs = result.getAttributes().getAll();
  82. while (attrs.hasMore()) {
  83. //Open another connection to the LDAP server with the found DN and the password
  84. searchEnv.put(Context.SECURITY_PRINCIPAL, result.getNameInNamespace());
  85. searchEnv.put(Context.SECURITY_CREDENTIALS, userPwd);
  86. try {
  87. new InitialDirContext(searchEnv);
  88. } catch (Exception e) {
  89. logger.warn("invalid ldap credentials or ldap search error", e);
  90. return null;
  91. }
  92. Attribute attr = (Attribute) attrs.next();
  93. if (attr.getID().equals(ldapEmailAttribute)) {
  94. return (String) attr.get();
  95. }
  96. }
  97. }
  98. } catch (NamingException e) {
  99. logger.error("ldap search error", e);
  100. return null;
  101. }
  102. return null;
  103. }
  104. /***
  105. * get ldap env fot ldap server search
  106. * @return Properties
  107. */
  108. Properties getManagerLdapEnv() {
  109. Properties env = new Properties();
  110. env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  111. env.put(Context.SECURITY_AUTHENTICATION, "simple");
  112. env.put(Context.SECURITY_PRINCIPAL, ldapSecurityPrincipal);
  113. env.put(Context.SECURITY_CREDENTIALS, ldapPrincipalPassword);
  114. env.put(Context.PROVIDER_URL, ldapUrls);
  115. return env;
  116. }
  117. }