首页 > 其他分享 >SpringBoot 2.3 升级到 SpringBoot 2.7 爬坑-- Cors 跨域

SpringBoot 2.3 升级到 SpringBoot 2.7 爬坑-- Cors 跨域

时间:2024-10-21 14:51:18浏览次数:1  
标签:web SpringBoot -- org springframework Cors import config annotation

When allowCredentials is true, allowedOrigins cannot contain the special value "*" since that cannot be set on the "Access-Control-Allow-Origin" response header. To allow credentials to a set of origins, list them explicitly or consider using "allowedOriginPatterns" instead.
image

原配置文件 -- 不生效

package com.vipsoft.admin.config;

import com.vipsoft.admin.interceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebConfig implements WebMvcConfigurer { 

    /**
     * 跨域支持
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(true)
                .allowedMethods("GET", "POST", "DELETE", "PUT", "PATCH", "OPTIONS", "HEAD")
                .maxAge(3600 * 24);
    }

}

修改后

package com.vipsoft.admin.config;

import com.vipsoft.admin.interceptor.AuthInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class WebConfig implements WebMvcConfigurer { 

 /**
     * 跨域配置
     */
    @Bean
    public CorsFilter corsFilter()
    {
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);
        // 设置访问源地址
        config.addAllowedOriginPattern("*");
        // 设置访问源请求头
        config.addAllowedHeader("*");
        // 设置访问源请求方法
        config.addAllowedMethod("*");
        // 有效期 1800秒
        config.setMaxAge(1800L);
        // 添加映射路径,拦截一切请求
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config);
        // 返回新的CorsFilter
        return new CorsFilter(source);
    } 
}

标签:web,SpringBoot,--,org,springframework,Cors,import,config,annotation
From: https://www.cnblogs.com/vipsoft/p/18489481

相关文章

  • Springboot接入Mqtt
    MQTT(MessageQueuingTelemetryTransport,消息队列遥测传输协议),是一种基于发布/订阅(publish/subscribe)模式的"轻量级"通讯协议,该协议构建于TCP/IP协议上,由IBM在1999年发布。MQTT最大优点在于,可以以极少的代码和有限的带宽,为远程连接设备提过实时可靠的消息服务,作为一种低开......
  • 不用PLC和板卡,一台电脑就可以控制伺服
    1、前言大家好!我是付工。EtherCAT是运动控制领域使用最广泛的总线通信协议之一。如果我们只有一台电脑,能不能直接控制EtherCAT总线伺服呢?这个是完全可以的。我们可以在电脑上安装实时运行环境,从而实现对伺服电机的总线控制。如果大家手上也有一台EtherCAT总线伺服,就可以直接......
  • 双系统Linux使用windows硬盘导致git报错问题解决
    一.问题产生的背景双系统下ubuntu为了节省空间挂载使用了windows硬盘,在使用最新的gitclone代码后提示“gitfataldetecteddubiousownershipinrepository”,这是git为了安全原因限制登陆用户和仓库文件用户必须一致,否则提示上述错误信息二.问题的解决办法办法1:挂载磁盘时......
  • 揭秘PostgreSQL的隐藏奥秘:物理、内存与进程模型的深度解析与高效优化策略
    引言PostgreSQL作为一款强大的开源关系型数据库管理系统,以其灵活性、高性能和丰富的功能特性在全球范围内受到广泛欢迎。其底层架构的精心设计,使其在处理复杂查询、支持多种数据类型和高并发用户访问时表现出色。理解PostgreSQL的底层架构不仅有助于提升系统性能,还能帮助开......
  • 在 Git 中,获取提交的哈希值(commit hash)
    在Git中,获取提交的哈希值(commithash)的方法有多种。以下是一些常用的方法:1.使用gitlog命令你可以使用gitlog命令查看提交历史,其中包括每个提交的哈希值。gitlog这将输出类似以下的内容:commit8927698069e9c719f452d7a71faac23ef25d27ab(HEAD->main)Auth......
  • 金山文档误删后如何找回?文件恢复的常用方法
    在我们日常办公和学习中,金山文档因其便捷性和功能性而广受欢迎。然而,在使用过程中,误删文件的情况时有发生。今天将介绍4种方法,可快速恢复误删的金山文档文件。恢复误删金山文档的四种方法方法一:回收站恢复当文件被误删时,首先应检查金山文档的回收站。回收站是金山文档提供......
  • 广州盈致WMS系统:优化仓储管理的智能化解决方案
    WMS系统是一种基于信息技术的智能化仓储管理解决方案,通过数字化、自动化和智能化的方式,优化企业的仓储管理流程,提高效率、准确性和可控性。以下是WMS系统优化仓储管理的智能化解决方案:自动化操作:WMS系统可以自动化执行仓库操作,包括入库、出库、移库等任务,通过系统自动分配任务、指......
  • Vue3动态填充Echars5数据(柱形图为例)
    1.Echars的安装Vue3项目终端输入命令npminstallecharts2.在vue项目中引入3.echars三步走96行获取实例97~125行样式设置及数据(那么需要动态的获取api接口请求来的数据而不是写死的数据,这里再116行进行设置)126行设置数据注:该方法需要在onMounted()方法中挂载5......
  • 鸿蒙Flutter实战:01-搭建开发环境
    鸿蒙Flutter实战:01-搭建开发环境准备工作1.安装DevEcoStudioNEXTIDE,注意版本应该是Next,当前最新的是Beta32.安装Git,如果要同时适配安卓,需要安装AndroidStudio;如果要适配ios,需要安装XcodeMac安装(推荐)环境变量配置#FlutterMirrorexportPUB_HOSTED_URL=h......
  • LeetCode刷题日记之贪心算法(五)
    目录前言无重叠区间划分字母区间合并区间单调递增的数字监控二叉树总结前言随着对贪心算法的不断深入,本篇文章将继续挑战一些经典的题目,进一步巩固这一算法的应用技巧。希望博主记录的内容能够帮助大家更好地掌握贪心算法的解题思路✍✍✍无重叠区间LeetCode题目......