【后端开发】后端跨域

java 复制代码
package com.example.demo.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * 后端实现跨域请求
 */
@Configuration
public class CorsConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowCredentials(false)
                .allowedMethods("*")
                .allowedHeaders("*");

    }
}

//
//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 linhongcun
// *
// */
//@Configuration
//public class CorsConfig {
//    @Bean
//    public CorsFilter corsFilter() {
//        final UrlBasedCorsConfigurationSource urlBasedCorsConfigurationSource = new UrlBasedCorsConfigurationSource();
//        final CorsConfiguration corsConfiguration = new CorsConfiguration();
//        /*是否允许请求带有验证信息*/
//        corsConfiguration.setAllowCredentials(true);
//        /*允许访问的客户端域名*/
//        corsConfiguration.addAllowedOrigin("*");
//        /*允许服务端访问的客户端请求头*/
//        corsConfiguration.addAllowedHeader("*");
//        /*允许访问的方法名,GET POST等*/
//        corsConfiguration.addAllowedMethod("*");
//        urlBasedCorsConfigurationSource.registerCorsConfiguration("/**", corsConfiguration);
//        return new CorsFilter(urlBasedCorsConfigurationSource);
//    }
//}
相关推荐
问道飞鱼32 分钟前
【Rust编程语言】Rust数据类型全面解析
开发语言·后端·rust·数据类型
泉城老铁1 小时前
目前开源架构需要注意的安全问题
spring boot·后端
ZoeGranger1 小时前
【Spring】IoC 控制反转、DI 依赖注入、配置文件和bean的作用域
后端
马卡巴卡1 小时前
分库分表数据源ShardingSphereDataSource的Connection元数据误用问题分析
后端
superman超哥1 小时前
仓颉动态特性探索:反射API的原理、实战与性能权衡
开发语言·后端·仓颉编程语言·仓颉·仓颉语言·仓颉动态特性·反射api
骑着bug的coder1 小时前
第7讲:索引(下)——失效场景与优化实战
后端·mysql
superman超哥1 小时前
仓颉元编程之魂:宏系统的设计哲学与深度实践
开发语言·后端·仓颉编程语言·仓颉·仓颉语言·仓颉语言特性
一 乐1 小时前
健身房预约|基于springboot + vue健身房预约小程序系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端·学习·小程序
代码游侠2 小时前
学习笔记——IO多路复用技术
linux·运维·数据库·笔记·网络协议·学习
踏浪无痕2 小时前
JobFlow:时间轮与滑动窗口的实战优化
后端·架构·开源