Spring Boot 中的 Actuator 使用 Postwoman 调试出现的 CORS 问题

2020-09-01 17:24:23 +08:00
 u6b7b5fc3

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 中的已尝试过的方法,另外也去搜索过PostwomanGithub Issue,里面是有提到CORS的问题,但是没有涉及到Spring Boot

3 所以

所以求助各位大神这个CORS问题应该怎么处理???

(真给跪了。。。。)

1596 次点击
所在节点    Java
9 条回复
tcfenix
2020-09-01 17:27:43 +08:00
你配置的 allow origins 是 http://localhost:3000
那么你的请求的 header 带上 origin http://localhost:3000 了么?
tcfenix
2020-09-01 17:30:23 +08:00
如果 spring boot 的代码没有写错的话,只要你的 header 里面带上 origin:http://localhost:3000
那么他就会在回包的时候把这些 Access-Control-Allow-Origin 还有 Methods Credentials Headers 啥的都带上
u6b7b5fc3
2020-09-01 17:30:35 +08:00
补充一下,Postwoman 的请求 Header 也带上了 http://localhost:3000 了:


![]( https://img-blog.csdnimg.cn/20200901172954464.png)
css3
2020-09-01 17:35:28 +08:00
跨域问题,前后端请求的域名不一致
css3
2020-09-01 17:36:09 +08:00
可以用 nginx 转发啊,前后端都转到 nginx 就是同域名了
u6b7b5fc3
2020-09-01 17:37:19 +08:00
都没有部署啊,就本地测试的一个简单的 Spring Boot Actuator
zhenjiachen
2020-09-02 20:48:05 +08:00
因为你集成了 security,需要配置一个 security 的配置,开放所有的 Actuator 接口就行
zhenjiachen
2020-09-02 20:49:35 +08:00
u6b7b5fc3
2020-09-03 17:20:34 +08:00
@zhenjiachen 添加了 ActuatorSecurity 之后还是不行

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/703231

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX