SpringCloud consul 微服务(注册到主机名的问题)- SpringSecurityOAuth2 authorization_code 模式遇到的坑...

目前项目在使用consul做服务注册与发现,做SpringSecurityOAuth2 权限认证的authorization_code模式的时候发现一个异常坑爹的问题

这是开始的服务注册代码块 bootstrap.yml:

spring:
  cloud:
    consul:
      port: 8500
      host: localhost
      discovery:
        serviceName: auth
        locator:
          lower-case-service-id: true
          enabled: true
        register: true

这是注册完后的健康检查

他会把你的主机地址给注册上来。 平时使用可能没问题,但是 当做OAuth2的 authorization_code 模式认证的时候,会出现跨域异常情况如下:

这是请求路径:

http://localhost:8001/auth/oauth/authorize?response_type=code&client_id=client_name&redirect_uri=http://localhost:8001/auth/callback&scope=auth

访问后跳转到默认的登录界面:

仔细看,url位置访问地址变成了之前注册的主机名 从而导致的结果就是,点击登录界面出现下图:

没有权限 返回401。 问题就出在了,跳转回主机名导致了跨域问题。

解决该问题的措施就是修改开始的bootstrap.yml的文件:

spring:
  cloud:
    consul:
      port: 8500
      host: localhost
      discovery:
        serviceName: auth
        locator:
          lower-case-service-id: true
          enabled: true
        register: true
        prefer-ip-address: true #这个必须配
        tags: version=1.0
        instance-id: ${spring.application.name}:${spring.cloud.client.ip-address}
        healthCheckInterval: 15s
        health-check-url: http://${spring.cloud.client.ip-address}:${server.port}/actuator/health

consul注册增加强制限制 prefer-ip-address:true 强制获取ip的方式注册到consul。

2019.07.09

由于没有配置

spring.cloud.consul.discovery.prefer-ip-address=true

微服务将会注册所在主机/容器的主机名 注册到consul,然而,我们会发现有时可能造成注册IP到consul  主机名是通过调用Java API获取的,有时候Java API无法获得主机名,于是会将IP地址发送给 consul + 只要你配置了环境变量HOST_NAME ,就可以将你所配置的环境变量注册

spring.cloud.gateway.discovery.instance.hostname=${HOST_NAME}

获取主机名的相关代码:

https://github.com/spring-cloud/spring-cloud-commons/blob/master/spring-cloud-commons/src/main/java/org/springframework/cloud/commons/util/InetUtils.java

相关Issue

https://github.com/spring-cloud/spring-cloud-netflix/issues/2084

从ip跳转回主机名导致跨域权限异常。

转载于:https://my.oschina.net/u/3500033/blog/3071284

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值