【发布时间】:2020-08-05 19:51:09
【问题描述】:
我正在尝试使用端口 3000 允许来自同一服务器的 cors。
由于我不知道我的服务器将部署在哪里,所以我尝试将服务器名称作为字符串访问,而不是与我想要的端口连接。
所以这是我的方法:
@SpringBootApplication
public class TestApplication {
@Autowired
private HttpServletRequest request;
public static void main(String[] args) {
SpringApplication.run(TestApplication.class, args);
}
@Bean
public WebMvcConfigurer corsConfigurer() {
System.out.println(request.getServerName());
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry
.addMapping("/**")
.allowedOrigins(request.getServerName()+":3000");
}
};
}
}
我认为这些是我的错误信息的亮点:
Error creating bean with name 'corsConfigurer'
org.springframework.beans.BeanInstantiationException: Failed to instantiate
java.lang.IllegalStateException: No thread-bound request found: Are you referring to request attributes outside of an actual web request, or processing a request outside of the originally receiving thread?
【问题讨论】:
-
如果要部署在同一台服务器上,你对使用本地主机地址有什么限制吗?也许这篇文章有帮助:stackoverflow.com/questions/9481865/…
标签: java spring spring-boot cors