SpringBoot - 运行时更新CorsFilter

时间:2018-06-10 15:55:56

标签: java spring-mvc spring-boot cors javabeans

我的corsFilter实现如下:

@Configuration
public class CorsConfig {

@Autowired
AppConfigurationRepository appConfigurationRepository;

@Bean
public FilterRegistrationBean corsFilter() {
    UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
    CorsConfiguration config = new CorsConfiguration();
    config.setAllowCredentials(true);
    config.setAllowedOrigins(getOrigin());
    config.addAllowedHeader("*");
    config.addAllowedMethod("*");
    source.registerCorsConfiguration("/**", config);
    FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
    bean.setOrder(0);
    return bean;
}

public List<String> getOrigin() {
    return Arrays.asList(appConfigurationRepository.findByKey(AppConfigurationKeys.CORS_ORIGINS).getValue().split(","));
}}

getOrigin从db中获取允许访问的域。我有一个端点,用新域更新数据库。但是当我再次构建应用程序时,来自新域的请求才会获得访问权限。我想要一些东西,当在db上更新域时,它在运行时获得访问权限,而不需要重新启动应用程序

0 个答案:

没有答案