跨域请求

跨域

域名、协议、端口有一个不一样,就是跨域。 

 

实现

package com.jlpay.agent.query.framework.mvc.filter;

import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;

/**
 * 自定义拦截器配置
 *
 * @Author: heyaolei
 * @Date: 2021/11/5
 */
@Slf4j
@Configuration
public class FilterConfiguration {
    /**
     * 可选字段,布尔值类型,表示是否允许发送Cookie。默认情况下,Cookie不包括在CORS请求之中;如果设为true,即表示服务器允许在请求中包含Cookie,一起发给服务器。
     * 注意该值只能设为true,如果服务器不允许浏览器发送Cookie,删除该字段即可。
     */
    @Value("${jlpay.business.filter.allowCredentials:true}")
    private boolean allowCredentials;
    /**
     * 可选字段,CORS请求时默认支持6个基本字段,XMLHttpRequest.getResponseHeader()方法:
     * Cache-Control、Content-Language、Content-Type、Expires、Last-Modified、Pragma。
     * 如果需要支持其他Headers字段,必须在Access-Control-Allow-Headers里面指定。
     */
    @Value("${jlpay.business.filter.allowedHeader:*}")
    private String allowedHeader;
    /**
     * 必填字段,支持的跨域请求的方法
     */
    @Value("${jlpay.business.filter.allowedMethod:*}")
    private String allowedMethod;
    /**
     * 必填字段,接受任意域名的请求
     */
    @Value("${jlpay.business.filter.allowedOrigin:*}")
    private String allowedOrigin;
    @Value("${jlpay.business.filter.corsPath:/**}")
    private String corsPath;

    /**
     * 跨域请求配置
     *
     * @return
     */
    private CorsConfiguration buildCorsConfig() {
        CorsConfiguration corsConfig = new CorsConfiguration();
        corsConfig.setAllowCredentials(allowCredentials);
        corsConfig.addAllowedHeader(allowedHeader);
        corsConfig.addAllowedMethod(allowedMethod);
        //指定域名拦截配置
        if (!StringUtils.isEmpty(allowedOrigin) && !CorsConfiguration.ALL.equals(allowedOrigin)) {
            String[] originArr = allowedOrigin.split(",");
            for (String origin : originArr) {
                corsConfig.addAllowedOrigin(origin);
            }
        } else {
            corsConfig.addAllowedOrigin(CorsConfiguration.ALL);
        }

        return corsConfig;
    }

    /**
     * 跨域请求过滤器配置
     */
    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource configSource = new UrlBasedCorsConfigurationSource();
        //注:暂定所有接口均允许跨域,建议针对具体接口配置允许跨域
        configSource.registerCorsConfiguration(corsPath, buildCorsConfig());

        log.info("跨域请求过滤器配置:{}", JSONObject.toJSONString(configSource.getCorsConfigurations()));
        return new CorsFilter(configSource);
    }

    /**
     * 跨域请求配置注册至过滤器
     *
     * @return
     */
    @Bean
    public FilterRegistrationBean corsFilterRegist() {
        FilterRegistrationBean filterRegistration = new FilterRegistrationBean();
        filterRegistration.setFilter(corsFilter());
        filterRegistration.setName("corsFilter");
        return filterRegistration;
    }
}

 

posted on 2022-02-10 15:30  周公  阅读(26)  评论(0编辑  收藏  举报

导航