1 问题描述
一个简单的 Spring Boot Demo,使用了 Actuator,配置文件如下:
spring:
security:
user:
name: xf
password: 123456
management:
server:
port: 8090
servlet:
context-path: /sys
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: '*'
cors:
allowed-origins: http://localhost:3000
allowed-methods: GET
allowed-headers: '*'
exposed-headers: 'Access-Control-Allow-Origin'
allow-credentials: true
浏览器能正常访问http://localhost:8090/sys/actuator。
但是使用Postwoman调试的时候出现CORS问题:

2 已经尝试过的方法
2.1 换浏览器
使用火狐测试如下:

2.2 配置
其实这个问题笔者之前遇到过,在方法上加上@CrossOrigin注解即可:
@CrossOrigin(origins = "http://localhost:3000")
但这个只是针对某个方法的,而且一般在 Controller 层加上,并且尝试过在启动类加上上面的注解无效。
另外去搜索过也添加了如下的配置:
@Configuration
public class WebConfig {
@Bean
public WebMvcConfigurer corsConfigurer() {
return new WebMvcConfigurer() {
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedOrigins("http://localhost:4200");
}
};
}
}
也无效。
最后也尝试过修改配置文件:
management:
server:
port: 8090
servlet:
context-path: /sys
endpoint:
health:
show-details: always
endpoints:
web:
exposure:
include: '*'
cors:
allowed-origins: http://localhost:3000
allowed-methods: GET
allowed-headers: '*'
exposed-headers: 'Access-Control-Allow-Origin'
allow-credentials: true
也无效。
2.3 代理
因为笔者有使用浏览器代理扩展,但是关了代理也无效。
2.4 Postman
加入Basic Auth认证后,使用Postman测试一切正常:

2.5 官网
去搜索过Spring Boot的文档,里面只是简单的提到配置文件的设置:

也就是上面 2.2 中的已尝试过的方法,另外也去搜索过Postwoman的Github Issue,里面是有提到CORS的问题,但是没有涉及到Spring Boot:

3 所以
所以求助各位大神这个CORS问题应该怎么处理???
(真给跪了。。。。)