千古难遇,跨域CORSConfig不生效的问题

我在其他模块配置的跨域生效,到了这里就不生效了,弄来弄去,原来是文件名称的问题,估计是springboot底层配置了相应的bean有名称为CORSConfig

1.我的跨域配置

@Configuration
public class CORSConfig {
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");                // 设置访问源请求头
        corsConfiguration.addAllowedMethod("GET");              // 允许GET方法访问
        corsConfiguration.addAllowedMethod("POST");             // 允许POST方法访问
        corsConfiguration.addAllowedMethod("OPTIONS");          // 允许OPTIONS方法访问
        corsConfiguration.setAllowCredentials(Boolean.TRUE);
        // 2.4以后这样设置 替换 addAllowedOrigin("*)
        corsConfiguration.setAllowedOriginPatterns(Arrays.asList(CorsConfiguration.ALL));
        corsConfiguration.setMaxAge(3600L);
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig()); // 4 对接口配置跨域设置
        return new CorsFilter(source);
    }

}

2.异常原因

把文件名CORSConfig改成其他名字就好了,比如 PASSCORSConfig,
ACORSConfig,

3.总结

名字叫CORSConfig / GlobalCORSConfig 跨域文件有时候会出问题

4.正常的config

package cn.wisegraph.purchasems.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;

import java.util.Arrays;

@Configuration
public class PassCorsConfig {
    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedHeader("*");                // 设置访问源请求头
        corsConfiguration.addAllowedMethod("GET");              // 允许GET方法访问
        corsConfiguration.addAllowedMethod("POST");             // 允许POST方法访问
        corsConfiguration.addAllowedMethod("OPTIONS");          // 允许OPTIONS方法访问
        corsConfiguration.setAllowCredentials(Boolean.TRUE);
        // 2.4以后这样设置 替换 addAllowedOrigin("*)
        corsConfiguration.setAllowedOriginPatterns(Arrays.asList(CorsConfiguration.ALL));
        corsConfiguration.setMaxAge(3600L);
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig()); // 4 对接口配置跨域设置
        return new CorsFilter(source);
    }

}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
跨域配置通常需要在Web服务器上进行配置,以允许来自不同域的请求访问您的应用程序。在Java Spring框架中,可以使用CorsConfig来配置跨域解决方案。 CorsConfig是Spring-Framework中的一个Java类,它包含一组配置选项来控制跨域请求的处理方式。以下是一个示例CorsConfig的代码: ```java @Configuration public class CorsConfig { @Bean public WebMvcConfigurer corsConfigurer() { return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { registry.addMapping("/api/**") .allowedOrigins("http://localhost:9000") .allowedMethods("GET", "POST", "PUT", "DELETE", "HEAD") .allowedHeaders("*") .allowCredentials(true) .maxAge(3600); } }; } } ``` 这个示例配置将允许来自http://localhost:9000的请求访问/api/路径下的所有资源。它还指定了允许使用的HTTP方法,允许的标头以及是否允许带凭据(如cookie)的请求。最后,maxAge属性指定了CORS预检请求的缓存时间(以秒为单位)。 要使用CorsConfig,您需要将它作为一个Spring Bean注册到应用程序上下文中。可以在您的Spring应用程序的配置类中添加以下代码: ```java @Configuration @EnableWebMvc public class AppConfig { @Autowired private CorsConfig corsConfig; @Bean public WebMvcConfigurer webMvcConfigurer() { return new WebMvcConfigurerAdapter() { @Override public void addCorsMappings(CorsRegistry registry) { corsConfig.corsConfigurer().addCorsMappings(registry); } }; } } ``` 在上面的代码中,我们使用@Autowired注解将我们的CorsConfig实例注入到AppConfig中,然后再通过addCorsMappings方法调用CorsConfig中的配置选项。最后,我们需要使用@EnableWebMvc注解来启用Spring的Web MVC功能,以便跨域配置生效。 总之,CorsConfig提供了一种灵活的方式来控制跨域请求的处理方式,并且可以轻松地集成到您的Spring应用程序中。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

军大君

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值