从yml配置文件中读取配置信息:

从yml配置文件中读取配置信息:

yml

ly:                           #自定义leyou配置
  cors:
    addAllowedOrigin:
      - http://manage.leyou.com
      - http://www.leyou.com
    setAllowCredentials: true
    addAllowedMethod:
      - OPTIONS
      - HEAD
      - GET
      - PUT
      - POST
      - DELETE
    addAllowedHeader:
      - "*"
    setMaxAge: 3600000
    filterPath: "/**"

自定义属性类

package com.leyou.gateway.config;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Configuration;

import java.util.List;

/**
 * @Author: swq
 * @Date: 2019/8/29  14:09
 */
@Data   //自动生成setter/getter方法
@ConfigurationProperties(prefix = "ly.cors")   //配置公共前缀
public class CoresProperties {
    private List<String> addAllowedOrigin;
    private Boolean setAllowCredentials;
    private List<String> addAllowedMethod;
    private List<String> addAllowedHeader;
    private Long setMaxAge;
    private String filterPath;

}

从配置类中读取自定义的属性类

第一种方法:在方法中传入CoresProperties对象(CoresProperties prop)
package com.leyou.gateway.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Author: swq
 * @Date: 2019/8/29  13:46
 * 解决跨域问题
 */
@Configuration
@EnableConfigurationProperties(CoresProperties.class)  //读取自定义属性类
public class CorsConfig {

    //配置一个
    @Bean
    public CorsFilter corsFilter(CoresProperties prop) {
        //创建config对象,添加Cors的配置信息
        CorsConfiguration config = new CorsConfiguration();
        //允许的域
        prop.getAddAllowedOrigin().forEach(config::addAllowedOrigin);
        //是否发送cookies信息
        config.setAllowCredentials(prop.getSetAllowCredentials());
        //允许的请求方式
        prop.getAddAllowedMethod().forEach(config::addAllowedMethod);
        //允许的头信息
        prop.getAddAllowedHeader().forEach(config::addAllowedHeader);
        //有效期
        config.setMaxAge(prop.getSetMaxAge());
        //添加拦截的映射路径  拦截一切请求
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration(prop.getFilterPath(), config);

        //返回CorsFilter
        return new CorsFilter(configSource);
    }
}

第二种方法:在类中中注入CoresProperties
package com.leyou.gateway.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Author: swq
 * @Date: 2019/8/29  13:46
 * 解决跨域问题
 */
@Configuration
public class CorsConfig {
	@Autowired
    private CoresProperties prop;
    //配置一个
    @Bean
    public CorsFilter corsFilter() {
        //创建config对象,添加Cors的配置信息
        CorsConfiguration config = new CorsConfiguration();
        //允许的域
        prop.getAddAllowedOrigin().forEach(config::addAllowedOrigin);
        //是否发送cookies信息
        config.setAllowCredentials(prop.getSetAllowCredentials());
        //允许的请求方式
        prop.getAddAllowedMethod().forEach(config::addAllowedMethod);
        //允许的头信息
        prop.getAddAllowedHeader().forEach(config::addAllowedHeader);
        //有效期
        config.setMaxAge(prop.getSetMaxAge());
        //添加拦截的映射路径  拦截一切请求
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration(prop.getFilterPath(), config);

        //返回CorsFilter
        return new CorsFilter(configSource);
    }
}

第三种方法:使用@Component
package com.leyou.gateway.config;

import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.CorsConfigurationSource;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * @Author: swq
 * @Date: 2019/8/29  13:46
 * 解决跨域问题
 */
@Configuration
@Component
public class CorsConfig {
    //配置一个
    @Bean
    public CorsFilter corsFilter() {
        //创建config对象,添加Cors的配置信息
        CorsConfiguration config = new CorsConfiguration();
        //允许的域
        prop.getAddAllowedOrigin().forEach(config::addAllowedOrigin);
        //是否发送cookies信息
        config.setAllowCredentials(prop.getSetAllowCredentials());
        //允许的请求方式
        prop.getAddAllowedMethod().forEach(config::addAllowedMethod);
        //允许的头信息
        prop.getAddAllowedHeader().forEach(config::addAllowedHeader);
        //有效期
        config.setMaxAge(prop.getSetMaxAge());
        //添加拦截的映射路径  拦截一切请求
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        configSource.registerCorsConfiguration(prop.getFilterPath(), config);

        //返回CorsFilter
        return new CorsFilter(configSource);
    }
}

你可能感兴趣的:(springboot)