贵州建网站,网站怎么优化自己免费,优化seo搜索,厅门户网站建设第一种跨域解决方式
第一种就是我们平常使用的解决跨域问题的方法#xff0c;但是要实现WebMvcConfigurer 接口#xff0c;还需要导入web依赖#xff0c;如果我们不引入web依赖#xff0c;如何解决跨域呢#xff1f; 答#xff1a;看第二种方式
pom.xml
dependenc…第一种跨域解决方式
第一种就是我们平常使用的解决跨域问题的方法但是要实现WebMvcConfigurer 接口还需要导入web依赖如果我们不引入web依赖如何解决跨域呢 答看第二种方式
pom.xml
dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactId
/dependencyCorsConfig
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;/*** author xjz_2002* version 1.0*/
Configuration
public class CorsConfig implements WebMvcConfigurer {//解决跨域请求问题Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping(/**).allowedOrigins(*).allowCredentials(true).allowedMethods(GET, POST, PUT, DELETE, OPTIONS).maxAge(3600);}
}第二种跨域解决方式
如果我们这个微服务模块不引入web依赖只是用来做一个网关遇到跨域问题可以中下面代码解决
package com.xjz.xjzliving.gateway.config;import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;/*** 在 gateway 统一解决跨域问题* 不需要引入web依赖 实现去 WebMvcConfigurer接口 就可以解决跨域问题* author xjz_2002* version 1.0*/
Configuration
public class XjzlivingGatewayCorsConfiguration {Beanpublic CorsWebFilter corsWebFilter() {System.out.println(enter....);UrlBasedCorsConfigurationSource source new UrlBasedCorsConfigurationSource();CorsConfiguration corsConfiguration new CorsConfiguration();//1、配置跨域corsConfiguration.addAllowedHeader(*);corsConfiguration.addAllowedMethod(*);corsConfiguration.addAllowedOrigin(*);corsConfiguration.setAllowCredentials(true);source.registerCorsConfiguration(/**, corsConfiguration);return new CorsWebFilter(source);}}