春季启动-无法读取Yaml属性文件application.yml

时间:2018-08-20 13:37:24

标签: java spring spring-boot ldap

我尝试为此配置ldap服务器,并使用spring-boot,如果我尝试读取{,则使用application.yml文件从代码中读取ldap url,base,userdn,password等。 {1}}属性,它们总是返回null。

以下是我的文件,请帮助我解决问题。

// application.yml // ----------------

application.yml

// LdapConfig.java // ----------------

spring:
      profiles:
        active: TEST 

     ---

    spring:
     profiles: PROD
    logging:
      config: classpath:PROD/log4j2.yml
    ldap:
      url: ldap://pod-url
      base: XXX
      userDn: yyy
      password: password

    ---

    spring:
       profiles: TEST
    logging:
      config: classpath:UAT/log4j2.yml
    ldap:
       url: ldap://ldap.forumsys.com:389
       base: dc=example,dc=com
       userDn: cn=read-only-admin,dc=example,dc=com
       password: password

//从下面的文件中,我将尝试使用ldapconfig属性

// AuthenticationServiceimpl.java // ------------------------------

@Configuration
@ComponentScan(basePackages = {"com.base.package.*"})
public class LdapConfig {

    @Autowired
    Environment env;

    @Bean
    public LdapContextSource contextSource() {
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setUrl(env.getProperty("ldap.url"));
        contextSource.setBase(env.getProperty("ldap.base"));
        contextSource.setUserDn(env.getProperty("ldap.userDn"));
        contextSource.setPassword(env.getProperty("ldap.password"));
        contextSource.afterPropertiesSet();
        return contextSource;
    }

    @Bean
    public LdapTemplate ldapTemplate() {
        return new LdapTemplate(contextSource());
    }


}

//并且我的主要应用是

    public class AuthenticationServiceImpl implements AuthenticationService {
    Logger logger = LogManager.getLogger(AuthenticationServiceImpl.class);


    private LdapTemplate ldapTemplate;
    private LdapContextSource ldapContextSource;

    public boolean authenticateUser(String username, String password) {

        ApplicationContext context = new AnnotationConfigApplicationContext(LdapConfig.class);
        ldapTemplate = (LdapTemplate) context.getBean(LdapTemplate.class);
        ldapContextSource = (LdapContextSource) context.getBean(LdapContextSource.class);
        DirContext ctx = null;
        try {

            return ldapTemplate.authenticate(ldapContextSource.getBaseLdapPathAsString(), "(uid=" + username + ")",
                    password);

        } catch (Exception e) {
            logger.error("user " + username + " failed to authenticated " + e);
            return false;
        } finally {
            if (ctx != null) {
                org.springframework.security.ldap.LdapUtils.closeContext(ctx);
            }
        }
    }


}

1 个答案:

答案 0 :(得分:1)

使用@Value:

@Configuration
@ComponentScan(basePackages = {"com.base.package.*"})
public class LdapConfig {

    @Autowired
    Environment env;

    @Value("${ldap.url}")
    private String ldapUrl;

    @Bean
    public LdapContextSource contextSource() {
        LdapContextSource contextSource = new LdapContextSource();
        contextSource.setUrl(ldapUrl);
        // ...
    }

    @Bean
    public LdapTemplate ldapTemplate() {
        return new LdapTemplate(contextSource());
    }

}