add multiple cross origin urls in spring boot

前端 未结 6 1227
太阳男子
太阳男子 2021-02-02 13:50

I found an example on how to set cors headers in spring-boot application. Since we have many origins, I need to add them. Is the following valid?

@Configuration
         


        
6条回答
  •  一个人的身影
    2021-02-02 14:13

    resources/application.yaml

    server:
      port: 8080
      servlet:
        contextPath: /your-service
      jetty:
        acceptors: 1
        maxHttpPostSize: 0
      cors:
        origins:
          - http://localhost:3001
          - https://app.mydomainnnn.com
          - https://app.yourrrrdooomain.com
    

    config/Config.java

    package com.service.config;
    
    import org.springframework.boot.context.properties.ConfigurationProperties;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.context.annotation.Configuration;
    
    import java.util.ArrayList;
    import java.util.List;
    
    @Configuration
    @EnableConfigurationProperties
    @ConfigurationProperties("server")
    public class Config {
    
      private int port;
        private Cors cors;
    
      public int getPort() {
        return this.port;
      }
    
      public void setPort(int port) {
        this.port = port;
      }
    
      public Cors getCors() {
        return this.cors;
      }
    
      public void setCors(Cors cors) {
        this.cors = cors;
      }
    
      public static class Cors {
        private List origins = new ArrayList<>();
    
        public List getOrigins() {
          return this.origins;
        }
    
        public void setOrigins(List origins) {
          this.origins = origins;
        }
      }
    }
    

    config/WebConfig.java

    package com.service.config;
    
    import java.util.Arrays; 
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.context.annotation.PropertySource;
    import org.springframework.context.annotation.ComponentScan;
    import org.springframework.core.env.Environment;
    import org.springframework.web.servlet.config.annotation.CorsRegistry;
    import org.springframework.web.servlet.config.annotation.EnableWebMvc;
    import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
    
    @Configuration
    @EnableWebMvc
    @ComponentScan(basePackages = "com.service.controller")
    @PropertySource("classpath:application.yaml")
    public class WebConfig extends WebMvcConfigurerAdapter {
    
        @Autowired
        private Environment environment;
    
        @Autowired
        private Config config;
    
        @Override
        public void addCorsMappings(CorsRegistry registry) {
          System.out.println("configuring cors");
          String[] origins = config.getCors().getOrigins().toArray(String[]::new);
          System.out.println("  - origins " + Arrays.toString(origins));
          registry.addMapping("/**")
                  .allowedOrigins(origins);
        }
    }
    

提交回复
热议问题