尝试使用Micronaut框架提供的IP模式过滤器。我想要的是在应用程序启动时注入配置。现在,yaml文件接受IP列表作为输入。但是,为了在运行时注入配置,我如何将IP列表传递给这个yaml属性。
当前场景
micronaut:
security:
enabled: true
ip-patterns:
- 127.0.0.1
- 192.168.1.*不确定下面的方法是否有效
期望的
micronaut:
security:
enabled: true
ip-patterns:${list_of_Ip's}发布于 2019-05-15 23:10:07
正如您所知道的,ip-patterns是一个列表,您不能将${}表示为列表。您可以创建自定义属性源,以将环境变量转换为配置。
例如:
public class Application {
public static void main(String[] args) {
String patternsEnv = System.getenv("SOME_ENV");
List<String> ipPatterns = Arrays.stream(patternsEnv.split(","))
.map(StringUtils::trimToNull)
.filter(Objects::nonNull)
.collect(Collectors.toList());
Map<String, Object> map = new HashMap<>(1);
map.put("micronaut.security.ip-patterns", ipPatterns);
PropertySource propertySource = new MapPropertySource("ip-patterns", map) {
@Override
public int getOrder() {
return 0;
}
};
Micronaut.build(null)
.mainClass(Application.class)
.propertySources(propertySource)
.start();
}
}https://stackoverflow.com/questions/56149783
复制相似问题