前端 ajax:
$.ajax({
url:"http://127.0.0.1:8080/blog/type/5",
dataType:"JSON",
type: "GET",
contentType:'application/json;charset=UTF-8',
crossDomain: true,
success: function (result) {}
})
后端 CROS 配置:
@Configuration
public class CorsConfig {
private CorsConfiguration buildConfig() {
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.addAllowedOrigin("*");
corsConfiguration.addAllowedHeader("X-Requested-With,content-type,token");
corsConfiguration.addAllowedMethod("GET, HEAD, POST, PUT, DELETE, TRACE, OPTIONS, PATCH");
corsConfiguration.setMaxAge(3600L); // 预检请求的有效期,单位为秒。
corsConfiguration.setAllowCredentials(true);// 是否支持安全证书(必需参数)
return corsConfiguration;
}
@Bean
public CorsFilter corsFilter() {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", buildConfig());
return new CorsFilter(source);
}
}
请求网址:http://127.0.0.1:8080/blog/type/5
请求方法:OPTIONS
远程地址:127.0.0.1:8080
状态码:
403
版本:HTTP/1.1
Referrer 政策:no-referrer-when-downgrade
请求头:
Host: 127.0.0.1:8080
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:71.0) Gecko/20100101 Firefox/71.0
Accept: */*
Accept-Language: zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2
Accept-Encoding: gzip, deflate
Access-Control-Request-Method: GET
Access-Control-Request-Headers: content-type
Referer: http://localhost:63343/MyBlog/index.html?_ijt=hbbpnj3pufuv05obggpj706936
Origin: http://localhost:63343
Connection: keep-alive
Cache-Control: max-age=0
响应头:
HTTP/1.1 403
Vary: Origin
Vary: Access-Control-Request-Method
Vary: Access-Control-Request-Headers
Transfer-Encoding: chunked
Date: Sat, 29 Feb 2020 02:18:33 GMT
Keep-Alive: timeout=60
Connection: keep-alive
http://127.0.0.1:8080/blog/type/5 网站可以正常访问
之前有个项目 相同配置 可以运行 求解
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.