0


springboot 跨域配置

方案一

@ConfigurationpublicclassGlobalCorsConfig{@BeanpublicCorsFiltercorsFilter(){//1. 添加 CORS配置信息CorsConfiguration config =newCorsConfiguration();//放行哪些原始域
        config.addAllowedOrigin("*");//是否发送 Cookie
        config.setAllowCredentials(true);//放行哪些请求方式
        config.addAllowedMethod("*");//放行哪些原始请求头部信息
        config.addAllowedHeader("*");//暴露哪些头部信息
        config.addExposedHeader("*");//2. 添加映射路径UrlBasedCorsConfigurationSource corsConfigurationSource =newUrlBasedCorsConfigurationSource();
        corsConfigurationSource.registerCorsConfiguration("/**",config);//3. 返回新的CorsFilterreturnnewCorsFilter(corsConfigurationSource);}}

方案二

// 案例 一@ConfigurationpublicclassCorsConfigimplementsWebMvcConfigurer{@OverridepublicvoidaddCorsMappings(CorsRegistry registry){
        registry.addMapping("/**")//是否发送Cookie.allowCredentials(true)//放行哪些原始域.allowedOrigins("*").allowedMethods(newString[]{"GET","POST","PUT","DELETE"}).allowedHeaders("*").exposedHeaders("*");}}

方案三

在控制器(类上)上使用注解 @CrossOrigin:,表示该类的所有方法允许跨域

在方法上使用注解 @CrossOrigin:

方案四

packagecn.wideth.aop;importjava.io.IOException;importjavax.servlet.Filter;importjavax.servlet.FilterChain;importjavax.servlet.FilterConfig;importjavax.servlet.ServletException;importjavax.servlet.ServletRequest;importjavax.servlet.ServletResponse;importjavax.servlet.http.HttpServletResponse;importorg.springframework.stereotype.Component;@ComponentpublicclassMyCorsFilterimplementsFilter{publicvoiddoFilter(ServletRequest req,ServletResponse res,FilterChain chain)throwsIOException,ServletException{HttpServletResponse response =(HttpServletResponse) res;
    response.setHeader("Access-Control-Allow-Origin","*");
    response.setHeader("Access-Control-Allow-Methods","POST, GET, OPTIONS, DELETE");
    response.setHeader("Access-Control-Max-Age","3600");
    response.setHeader("Access-Control-Allow-Headers","x-requested-with,content-type");
    chain.doFilter(req, res);}publicvoidinit(FilterConfig filterConfig){}publicvoiddestroy(){}}

方案五

packagecom.yiyi.yiyigateway.config.cors;importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.cors.CorsConfiguration;importorg.springframework.web.cors.reactive.CorsWebFilter;importorg.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;importorg.springframework.web.reactive.config.CorsRegistry;importorg.springframework.web.reactive.config.WebFluxConfigurer;importorg.springframework.web.util.pattern.PathPatternParser;/**
 * @author zrf
 * @date 2024-50-28 11:50:32
 * @description 全局跨域配置
 */@ConfigurationpublicclassGlobalCorsConfigimplementsWebFluxConfigurer{/**
     * 方案一
     * @return
     */@BeanpublicCorsWebFiltercorsFilter(){CorsConfiguration config =newCorsConfiguration();

        config.setAllowCredentials(true);// 允许cookies跨域
        config.addAllowedOrigin("*");// #允许向该服务器提交请求的URI,*表示全部允许,在SpringMVC中,如果设成*,会自动转成当前请求头中的Origin
        config.addAllowedHeader("*");// #允许访问的头信息,*表示全部
        config.addAllowedMethod("*");// 允许提交请求的方法类型,*表示全部允许
        config.setMaxAge(18000L);// 预检请求的缓存时间(秒),即在这个时间段里,对于相同的跨域请求不会再预检了UrlBasedCorsConfigurationSource source =newUrlBasedCorsConfigurationSource(newPathPatternParser());
        source.registerCorsConfiguration("/**", config);returnnewCorsWebFilter(source);}/**
     * 方案二 实现WebFluxConfigurer重写addCorsMappings方法
     * @param registry
     *//* @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")//允许路径
                .allowedHeaders("*")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowCredentials(true)
                .maxAge(36000);
    }*/}
标签: spring boot 后端 java

本文转载自: https://blog.csdn.net/weixin_42903592/article/details/141640496
版权归原作者 魏 无羡 所有, 如有侵权,请联系我们删除。

“springboot 跨域配置”的评论:

还没有评论