使用axios被禁止使用403,但是邮递员和axios得到所有的工作
服务器: Spring本地主机:8080
网站: Vue.js + Axios本地主机:80
配置:
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedHeaders("*")
.allowedMethods("*")
.maxAge(1800)
.allowedOrigins("http://localhost:80/");
}
}Vue.js ProxyTable:
proxyTable: {
'/api':{
target: "http://localhost:8080/",
changeOrigin:true,
}},Axios func:
doLogin(){
axios({
method: 'post',
url: '/api/te',
data: {
userNumber: 'hi'
}
});弹簧启动控制器:
@PostMapping("/te")
public String Test(@RequestBody HashMap<String,String> map) {
return map.get("userNumber");
}然后,在MSEdge本地主机中:80/:
然而,在邮递员中,这种方法效果很好:
我试了三个小时了,现在我很累.
发布于 2020-07-13 18:30:05
好了,我现在解决了!
那是因为我配置了CORS
allowedOrigins("http://localhost:80/");
和
proxyTable: {
'/api':{
target: "http://localhost:8080/",
changeOrigin:true,
}},原因:
http://localhost:80,最后添加'/‘是一个错误。proxyTable.'/api'.changeOrigin:true只将主机头设置为目标,而不是将原产地标头设置为目标,这与SpringBoot CORS配置中的allowedOringins方法不同,您的请求源仍然是htpp://localhost (请求的页面)。所以正确的代码是:
@Configuration
public class WebMVCConfig implements WebMvcConfigurer {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/api/**")
.allowedHeaders("*")
.allowedMethods("*")
.maxAge(1800)
.allowedOrigins("http://localhost");
}
}和
proxyTable: {
'/api':{
target: "http://localhost:8080",
changeOrigin:true,
}},;P
https://stackoverflow.com/questions/62881437
复制相似问题