[英]cross origin on spring boot from chrome extension
In my spring application, I have a method:在我的 spring 应用程序中,我有一个方法:
@CrossOrigin(origins="*")
@PostMapping("/datum")
Datum test(@RequestBody String data) {
return repository.save(new Datum(data));
}
I want that all the application can access it, so I have put that @CrossOrigin(origins="/**")
.我希望所有应用程序都可以访问它,所以我放了@CrossOrigin(origins="/**")
。 But when I try to do so, it runs without any complain.但是当我尝试这样做时,它运行时没有任何抱怨。
This is how I'm sending data from a chrome extension:这就是我从 chrome 扩展发送数据的方式:
var request = new XMLHttpRequest();
let url='http://localhost:8080/datum';
let data=JSON.stringify({ "data": body});
request.open('POST', url, true);
request.setRequestHeader("Content-Type", "application/json");
request.send(data);
My extension manifest:我的扩展清单:
"permissions": [
"activeTab",
"tabs",
"<all_urls>",
"background",
"http://*/*",
"https://*/*",
"http://localhost:8080/datum"
],
Gives me error 403给我错误403
But client still can't access this, how this can be solved?但是客户端仍然无法访问这个,如何解决?
In my @SpringBootApplication
annoted class, I added this:在我的@SpringBootApplication
注释 class 中,我添加了这个:
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("*");
}
};
}
And in manifest.json
:在manifest.json
中:
"permissions": [
"activeTab",
"tabs",
"background",
"http://*/"
]
Now it works.现在它起作用了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.