SecurityConfig.java 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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;
  18. import org.apache.dolphinscheduler.api.security.impl.ldap.LdapAuthenticator;
  19. import org.apache.dolphinscheduler.api.security.impl.pwd.PasswordAuthenticator;
  20. import org.apache.commons.lang.StringUtils;
  21. import org.slf4j.Logger;
  22. import org.slf4j.LoggerFactory;
  23. import org.springframework.beans.factory.annotation.Autowired;
  24. import org.springframework.beans.factory.annotation.Value;
  25. import org.springframework.beans.factory.config.AutowireCapableBeanFactory;
  26. import org.springframework.context.annotation.Bean;
  27. import org.springframework.context.annotation.Configuration;
  28. @Configuration
  29. public class SecurityConfig {
  30. private static final Logger logger = LoggerFactory.getLogger(SecurityConfig.class);
  31. @Value("${security.authentication.type:PASSWORD}")
  32. private String type;
  33. private AutowireCapableBeanFactory beanFactory;
  34. private AuthenticationType authenticationType;
  35. @Autowired
  36. public SecurityConfig(AutowireCapableBeanFactory beanFactory) {
  37. this.beanFactory = beanFactory;
  38. }
  39. private void setAuthenticationType(String type) {
  40. if (StringUtils.isBlank(type)) {
  41. logger.info("security.authentication.type configuration is empty, the default value 'PASSWORD'");
  42. this.authenticationType = AuthenticationType.PASSWORD;
  43. return;
  44. }
  45. this.authenticationType = AuthenticationType.valueOf(type);
  46. }
  47. @Bean(name = "authenticator")
  48. public Authenticator authenticator() {
  49. setAuthenticationType(type);
  50. Authenticator authenticator;
  51. switch (authenticationType) {
  52. case PASSWORD:
  53. authenticator = new PasswordAuthenticator();
  54. break;
  55. case LDAP:
  56. authenticator = new LdapAuthenticator();
  57. break;
  58. default:
  59. throw new IllegalStateException("Unexpected value: " + authenticationType);
  60. }
  61. beanFactory.autowireBean(authenticator);
  62. return authenticator;
  63. }
  64. }