Spring Boot 中的 Actuator 使用 Postwoman 调试出现的 CORS 问题 - V2EX
V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
u6b7b5fc3
V2EX    Java

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

  •  
  •   u6b7b5fc3 2020-09-01 17:24:23 +08:00 2207 次点击
    这是一个创建于 1949 天前的主题,其中的信息可能已经有所发展或是发生改变。

    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问题应该怎么处理???

    (真给跪了。。。。)

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


    ![]( https://img-blog.csdnimg.cn/20200901172954464.png)
    css3
        4
    css3  
       2020-09-01 17:35:28 +08:00
    跨域问题,前后端请求的域名不一致
    css3
        5
    css3  
       2020-09-01 17:36:09 +08:00
    可以用 nginx 转发啊,前后端都转到 nginx 就是同域名了
    u6b7b5fc3
        6
    u6b7b5fc3  
    OP
       2020-09-01 17:37:19 +08:00
    都没有部署啊,就本地测试的一个简单的 Spring Boot Actuator
    zhenjiachen
        7
    zhenjiachen  
       2020-09-02 20:48:05 +08:00 via iPhone
    因为你集成了 security,需要配置一个 security 的配置,开放所有的 Actuator 接口就行
    u6b7b5fc3
        9
    u6b7b5fc3  
    OP
       2020-09-03 17:20:34 +08:00
    @zhenjiachen 添加了 ActuatorSecurity 之后还是不行
    关于     帮助文档     自助推广系统     博客     API     FAQ     Solana     2418 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 22ms UTC 04:12 PVG 12:12 LAX 20:12 JFK 23:12
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86