繁体   English   中英

SpringBoot微服务如何使用java配置在应用上下文中设置属性

[英]SpringBoot microservice How to set properties in application context using java configuration

我有一个基于 Java 的配置的 spring-boot 微服务。 现在有一个身份验证令牌容器,我需要调用它来获取访问令牌。 该身份验证令牌库有一个这样的类。 请注意,类AuthServletContextListener来自我无法修改的第三方jar文件。

public class AuthServletContextListener implements ServletContextListener {
    public void contextInitialized(ServletContextEvent arg0) {
        try {
            ServletContext e = arg0.getServletContext();
            Properties config = new Properties();
            this.addProp(config, e, "auth.token.url", "Token Service URL");
            this.addProp(config, e, "auth.system.username", "System Username");
            this.addProp(config, e, "cauth.system.password", "System Password");
            TokenContainer.init(config);
        } catch (IOException arg3) {
            arg3.printStackTrace();
        }

    }


    private void addProp(Properties config, ServletContext context, String propName, String descrip) {
        String propVal = (String) context.getAttribute(propName);
        if (StringUtils.isEmpty(propVal)) {
            propVal = context.getInitParameter(propName);
        }

        if (StringUtils.isNotEmpty(propVal)) {
            config.put(propName, propVal);
        } else {
            throw new RuntimeException("error: ");
        }
    }
}

AuthContextListener 有一个自动查找应用程序启动事件的注解。 如果包含上述上下文参数,这将自动启动容器正确设置。 然后我可以通过该容器获取令牌,如下所示:

TokenContainer.getSystemToken() 

如果上述上下文参数包含在 web.xml 中,这将成功初始化,如下所示:

<context-param>
   <param-name>auth.system.username</param-name>
   <param-value>UserName</param-value>
</context-param> 

可以通过创建具有以下信息的 Spring bean 来执行与上述相同的应用程序上下文配置:

<bean>
    <property name="attributes">
        <map>
            <entry key="auth.token.url" value="${auth.token.url}"/>
            <entry key="auth.system.username" value="${auth.system.username}"/>
            <entry key="auth.system.password" value="${auth.system.password}"/>
        </map>
    </property>
</bean>

我的问题是如何在最新的 Spring Boot 应用程序中使用基于 Java 的配置来实现相同的目标。 我所拥有的只是带有身份验证端点、用户名和密码值的application.yml文件。 我尝试使用@Configuration bean 但没有运气。 如何在应用程序上下文中设置这三个道具并使该侦听器为我自动启动。

在您的 application.yml 或 application.properties 中为您的 auth url 添加一组:

#Example for application.properties
auth.token.url = http:\\...

然后在您的配置类中配置您的占位符,以便您可以读取您的属性值:

@Configuration
public class Config {
        @Bean
        public PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer() {
            PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
            propertySourcesPlaceholderConfigurer.setLocations(new ClassPathResource("application.properties"));//or application.yml
            return propertySourcesPlaceholderConfigurer;
        }

    }

然后在你的 AuthServletContextListener 类中:添加这个

public class AuthServletContextListener implements ServletContextListener {

  @Value("${auth.token.url}")
  private String authUrl ;

    public void contextInitialized(ServletContextEvent arg0) {
        try {
            ServletContext e = arg0.getServletContext();
            Properties config = new Properties();
            this.addProp(config, e, "auth.token.url", authUrl);
            this.addProp(config, e, "auth.system.username", "System Username");
            this.addProp(config, e, "cauth.system.password", "System Password");
            TokenContainer.init(config);
        } catch (IOException arg3) {
            arg3.printStackTrace();
        }

    }


    private void addProp(Properties config, ServletContext context, String propName, String descrip) {
        String propVal = (String) context.getAttribute(propName);
        if (StringUtils.isEmpty(propVal)) {
            propVal = context.getInitParameter(propName);
        }

        if (StringUtils.isNotEmpty(propVal)) {
            config.put(propName, propVal);
        } else {
            throw new RuntimeException("error: ");
        }
    }
}

将以下内容添加到您的application.properties

server.servlet.context-parameters.auth.token.url=<your-value>
server.servlet.context-parameters.auth.system.username=<your-value>
server.servlet.context-parameters.auth.system.password=<your-value>

这会将值公开为上下文参数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM