Java 放置动态字符串数组时,允许的原点不起作用

Java 放置动态字符串数组时,允许的原点不起作用,java,spring,spring-boot,spring-mvc,cors,Java,Spring,Spring Boot,Spring Mvc,Cors,我正在使用spring启动应用程序,并使用corsfilter启用CORS 所以,当我将静态值传递给allowedorigin时,它工作,但当我将数组放入其中时,它不工作 请在下面找到我的非工作代码: final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource(); final CorsConfiguration config = new CorsConfiguration(

我正在使用spring启动应用程序,并使用corsfilter启用CORS

所以,当我将静态值传递给allowedorigin时,它工作,但当我将数组放入其中时,它不工作

请在下面找到我的非工作代码:

final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.setAllowedOrigins(dbProperties.getAllowedDomains());
        config.setAllowedHeaders(Arrays.asList("Origin", "Content-Type", "Accept"));
        config.setAllowedMethods(Arrays.asList("GET", "POST"));
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
dbProperties.getAllowedDomains()返回列表

并查找正在运行的代码:

final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        config.setAllowedOrigins(Arrays.asList("http://192.168.1.14:8080"));
        config.setAllowedHeaders(Arrays.asList("Origin", "Content-Type", "Accept"));
        config.setAllowedMethods(Arrays.asList("GET", "POST"));
        source.registerCorsConfiguration("/**", config);
        return new CorsFilter(source);
物业类别如下:

@Component
@ConfigurationProperties
public class MyProperties {
@Value("${alloweddomains}")
private List<String> alloweddomains;

private boolean enableMobileNotification;

public List<String> getAllowedDomains() {
    return alloweddomains;
}

public void setAllowedDomains(List<String> alloweddomains) {
    this.alloweddomains = alloweddomains;
}

}

这里有一个小错误,您不需要
@值
,因为您正在使用
@ConfigurationProperties
,我还建议您在属性中添加前缀

编辑

使用camelCase作为变量名,而不是
alloweddomains
将其更改为
alloweddomains
,并在属性中使用
my app.alloweddomains
而不是
my app.alloweddomains

@组件
@ConfigurationProperties(“我的应用程序”)
公共类MyProperties{
私人名单;
私有布尔启用移动通知;
公共列表getAllowedDomains(){
返回异域;
}
public void setAllowedDomains(列出allowedDomains){
this.allowedDomains=allowedDomains;
}
}
这应该可以解决它


工作示例:

您是否介意显示
dbProperties
类以及如何设置属性?@MarcosBarbero sure@Value(“${allowedomains}”)私有列表allowedomains@MarcosBarbero我正在从dbProperties获取域值让我重新表述一下,我需要查看您的dbProperties类以及您的application.properties(或yaml)。查看我编辑的Post删除@value后,现在我在AllowedDomain上获取空值。您添加前缀了吗?谢谢,但我已经解决了我的问题,因为它是“”我正在应用程序上使用它。属性我很高兴你修复了它,但是如果你仍然在
@ConfigurationProperties
类中使用
@Value
,那就不是应该怎么做的。我的意思是,如果你想在其中使用
@Value
,你根本不需要
@ConfigurationProperties
。是的,我只使用@Value,它可以很好地处理这个问题
alloweddomains="http://192.168.1.14:8085","http://192.168.1.15:8084"
my-app.allowed-domains=http://192.168.1.14:8085, http://192.168.1.15:8084