目前项目在使用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 模式认证的时候,会出现跨域异常情况如下:
这是请求路径:
访问后跳转到默认的登录界面:
仔细看,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。