网站icp备案证书,wordpress page.php,校园引流推广方法,仙居建设局网站跨域
当一台服务器资源从另一台服务器#xff08;不同的域名或者端口#xff09;请求一个资源或者接口#xff0c;就会发起一个跨域HTTP请求。
同源#xff1a;协议、域名、端口都相同 只要一个不同#xff0c;就是跨域。
例子
请求方响应方是否跨域原因http://www.ba…跨域
当一台服务器资源从另一台服务器不同的域名或者端口请求一个资源或者接口就会发起一个跨域HTTP请求。
同源协议、域名、端口都相同 只要一个不同就是跨域。
例子
请求方响应方是否跨域原因http://www.baidu.comhttp://www.baidu.com/test.html否协议/域名/端口相同http://www.baidu.comhttps://www.baidu.com/test.html是协议不同http://www.baidu.comhttp://www.hhhh.com/test.html是主域名不同http://www.baidu.comhttp://haha.baidu.com/test.html是主域名相同、子域名不同http://www.baidu.com:8080http://www.baidu.com/8090/test.html是端口不同
跨域访问实例 跨域处理 任意一种方式都可。 1.添加跨域配置类
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;Configuration
public class GlobalCorsConfig {Beanpublic CorsFilter corsFilter(){// 1.添加cors配置信息CorsConfiguration config new CorsConfiguration();// 放行哪些原始域名config.addAllowedOriginPattern(*);//2.4.0后的写法// config.addAllowedOrigin(*);// 是否发送Cookieconfig.setAllowCredentials(true);// 放行哪些请求方式config.addAllowedMethod(*);// 放行哪些原始请求头部信息config.addAllowedHeader(*);// 暴露哪些头部信息config.addExposedHeader(*);// 2.添加映射路径UrlBasedCorsConfigurationSource corsConfigurationSource new UrlBasedCorsConfigurationSource();corsConfigurationSource.registerCorsConfiguration(/**, config);// 3.返回新的CorsFilterreturn new CorsFilter(corsConfigurationSource);}
}
2.重写WebMvcConfigurer
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 {Overridepublic void addCorsMappings(CorsRegistry registry) {registry.addMapping(/**)// 是否发送Cookie.allowCredentials(true)// 放行哪些原始域//.allowedOrigins(*).allowedOriginPatterns(*) // 2.4.0后的写法.allowedMethods(new String[] {GET, POST, PUT, DELETE}).allowedHeaders(*).exposedHeaders(*);}
}
3.注解CrossOrigin 类上注解 RestController
CrossOrigin(*)
public class CorsController {GetMapping(/cors)public String hello(){return hello cors;}
}方法上注解 方法可以单独跨域没有CrossOrigin(“*”)注解的方法则不行 RestController
public class CorsController {GetMapping(/cors)CrossOrigin(*)public String hello(){return hello cors;}GetMapping(/cors2)public String hello2(){return hello cors2;}
}4.自定义过滤器
import org.springframework.stereotype.Component;import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;Component
public class MyCorsFilter implements Filter {Overridepublic void doFilter(ServletRequest req, ServletResponse res,FilterChain chain) throws IOException, ServletException {HttpServletResponse response (HttpServletResponse) res;HttpServletRequest httpServletRequest (HttpServletRequest) req;response.setHeader(Access-Control-Allow-Origin, httpServletRequest.getHeader(origin));response.setHeader(Access-Control-Allow-Methods, POST, GET, OPTIONS, DELETE, HEAD);response.setHeader(Access-Control-Max-Age, 3600);response.setHeader(Access-Control-Allow-Headers, access-control-allow-origin, authority, content-type, version-info, X-Requested-With);response.setHeader(Access-Control-Allow-Credentials, true);chain.doFilter(req, res);}Overridepublic void init(FilterConfig filterConfig) throws ServletException {}Overridepublic void destroy() {}
}