首页 > 解决方案 > 使用身份验证服务器作为尤里卡客户端从资源服务器访问身份验证服务器时出错

问题描述

嗨,我在 springboot 中有一个实现的 oauth2 服务。Auth-server 和资源服务器启用了 eureka 客户端,它们也已成功注册到 eureka 服务器。资源服务器中 auth-server 的 URI 是这样配置的

security:
  oauth2:
    resource:
      token-info-uri: http://auth-server/oauth/check_token

现在auth-server在上面的 URI 中是 authserver 的应用程序名称。我期望资源服务器token-info-uri在从 eureka 服务器解析 url 后应该联系 auth 服务器。但是通过这个设置我得到了一个错误nested exception is java.net.UnknownHostException: auth-server

我将我的配置修改为此

security:
  oauth2:
    resource:
      prefer-token-info: false
      service-id: auth-server
      token-info-uri: http://${security.oauth2.resource.service-id}/oauth/check_token
      loadBalanced: true

但我得到了回应

{
    "error": "invalid_token",
    "error_description": "e2f95093-085c-4b59-90a5-c89fb5d1eccb"
}

当我调试时,我有这个日志

asset-mgmt-v1.1_1  | 2019-11-16 17:52:08.048  INFO 1 --- [nio-6001-exec-2] o.s.web.servlet.DispatcherServlet        : Completed initialization in 21 ms
asset-mgmt-v1.1_1  | 2019-11-16 17:52:08.086 DEBUG 1 --- [nio-6001-exec-2] o.s.b.a.s.o.r.UserInfoTokenServices      : Getting user info from: null
asset-mgmt-v1.1_1  | 2019-11-16 17:52:08.109 DEBUG 1 --- [nio-6001-exec-2] org.springframework.web.HttpLogging      : HTTP GET 
asset-mgmt-v1.1_1  | 2019-11-16 17:52:08.117 DEBUG 1 --- [nio-6001-exec-2] org.springframework.web.HttpLogging      : Accept=[application/json, application/*+json]
asset-mgmt-v1.1_1  | 2019-11-16 17:52:08.119  WARN 1 --- [nio-6001-exec-2] o.s.b.a.s.o.r.UserInfoTokenServices      : Could not fetch user details: class java.lang.IllegalStateException, Request URI does not contain a valid hostname: 
asset-mgmt-v1.1_1  | 2019-11-16 17:52:08.120 DEBUG 1 --- [nio-6001-exec-2] o.s.b.a.s.o.r.UserInfoTokenServices      : userinfo returned error: Could not fetch user details
asset-mgmt-v1.1_1  | 2019-11-16 17:52:08.127 DEBUG 1 --- [nio-6001-exec-2] o.s.b.a.audit.listener.AuditListener     : AuditEvent [timestamp=2019-11-16T17:52:08.125Z, principal=access-token, type=AUTHENTICATION_FAILURE, data={type=org.springframework.security.authentication.BadCredentialsException, message=e2f95093-085c-4b59-90a5-c89fb5d1eccb}]

基本上 URI 被拾起就是我所看到的

标签: spring-bootspring-security-oauth2netflix-eureka

解决方案


you cannot use just name in property files. it should be

security:
 oauth2:
  resource:
   service-id: {Service ID as at eureka server registered}
   token-info-uri: http://${security.oauth2.resource.service-id}/oauth/check_token
   loadBalanced=true
   prefer-token-info=false

P.S I just typed by hand. make sure keep proper spaces


推荐阅读