跨域请求

跨域

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

 

实现

复制代码
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   周公  阅读(31)  评论(0编辑  收藏  举报

相关博文:
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
点击右上角即可分享
微信分享提示