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

本文介绍如何通过YML配置文件设置CORS策略来解决跨域问题,包括配置允许的源、请求方法、头信息及有效期等,实现对特定路径的请求拦截。

从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);
    }
}

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

后端技术那点事

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值