SpringCloud跨域请求

SpringCloud跨域请求配置

SpringCloud跨域请求

前后分离下,webstorm开发前端页面,调用接口时,跨域问题:

No 'Access-Control-Allow-Origin' header is present on the requested resource.

项目中配置跨域
项目结构图
CorsConfig代码:

package com.forezp.serviceribbon.config;

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;

/**
 * ********************************************
 * 类名称:CorsConfig
 * 类描述:设置跨域请求
 *
 * @Author: 
 * @Date: 2019/7/9 14:39
 * ********************************************
 */
@Configuration
public class CorsConfig {
    @Bean
    public CorsFilter corsFilter() {
        final UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        final CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true); // 允许cookies跨域
        config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
        config.addAllowedHeader("*");// #允许访问的头信息,*表示全部
        config.setMaxAge(7200L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了
        config.addAllowedMethod("*");// 允许提交请求的方法,*表示全部允许
        source.registerCorsConfiguration("/**", config);
        CorsFilter corsFilter = new CorsFilter(source);
        return corsFilter;
    }
}
### Spring Cloud 配置方法 在 Spring Cloud 中,问题(CORS, Cross-Origin Resource Sharing)可以通过多种方式进行配置。一种常见的方式是在网关层面上通过 `CorsGlobalFilter` 配置全局的支持[^1]。 #### 使用 CorsGlobalFilter 进行全局配置 下面展示了一个利用 `CorsGlobalFilter` 实现访问控制的例子: ```java import org.springframework.context.annotation.Bean; import org.springframework.web.cors.CorsConfiguration; import org.springframework.cloud.gateway.route.RouteLocator; import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder; import org.springframework.cloud.gateway.filter.CorsGlobalProperties; @Bean public CorsGlobalProperties corsGlobalProperties() { CorsGlobalProperties properties = new CorsGlobalProperties(); CorsConfiguration configuration = new CorsConfiguration(); configuration.addAllowedHeader("*"); configuration.addAllowedMethod("*"); configuration.setAllowCredentials(true); configuration.addAllowedOriginPattern("*"); properties.setAddToSimpleRequests(false); // 可选,默认false properties.setCorsConfigurations(Collections.singletonMap("/**", configuration)); return properties; } ``` 此段代码创建了允许任何源、头部和HTTP 方法发起请求并携带凭证的 CORS 设置,并将其应用到所有的路径上。 另一种方式则是直接定义一个 `WebMvcConfigurer` Bean 来添加映射规则,这种方式适用于更细粒度地管理哪些 URL 模式应该启用 CORS 政策[^4]: ```java @Bean public WebMvcConfigurer webMvcConfigurer(){ return new WebMvcConfigurer() { @Override public void addCorsMappings(CorsRegistry registry){ registry.addMapping("/**") .allowedOriginPatterns("*") .allowedHeaders("*") .allowedMethods("*") .allowCredentials(true) .maxAge(TimeUnit.DAYS.toSeconds(1)); } }; } ``` 上述两种方案都可以有效地解决 Spring Cloud 应用中的资源共享问题。具体采用哪种取决于项目需求和个人偏好[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值