首页 > 解决方案 > Spring boot 2.0.3 + 安全 + Oauth2 自动配置

问题描述

Spring boot 2.0.3 + 安全 + Oauth2 自动配置

我正在使用 OAuth2 和微服务,我创建了一个微服务来生成授权令牌和另一个微服务作为客户端。令牌的生成正在工作,但是当我尝试在客户端服务上使用此生成的令牌进行身份验证时,它不起作用。

生成令牌的微服务:localhost:9999

使用 url 生成的令牌:estrutura:estruturasecret@localhost:9999/oauth/token

   [
       {
          "key":"grant_type",
          "value":"password"
       },
       {
          "key":"username",
          "value":"matheus"
       },
       {
          "key":"password",
          "value":"teste"
       },
       {
          "key":"client_id",
          "value":"estrutura"
       }
    ]

返回:

 {
        "access_token": "2e4c26b3-0fcf-493e-a255-6216b98811c5",
        "token_type": "bearer",
        "refresh_token": "5e33740a-ccb9-4ec1-94be-3a4643b8097a",
        "expires_in": 42479,
        "scope": "read write"
    }

客户微服务:本地主机:9090

@SpringBootApplication
@EnableResourceServer
public class ClientServer {
   public static void main(String[] args) {
      SpringApplication.run(ClientServer.class, args);
   }
}

应用程序.yml:

server:
  port: 9090
  servlet:
    context-path: /client
spring:
  application:
    name: client-server
  security:
    oauth2:
      client:
        client-id: estrutura
        client-secret: estruturasecret
        access-token-uri: localhost:9999/oauth/token
        user-authorization-uri: localhost:9999/oauth/authorize
      resource:
        token-info-uri: localhost:9999/oauth/check_token
logging:
  level:
    org.springframework.security: DEBUG

错误:

<error>invalid_token</error>
<error_description>Invalid access token: 2e4c26b3-0fcf-493e-a255-6216b98811c5</error_description>

日志:

2018-06-26 11:24:42.641 DEBUG 18658 --- [nio-9090-exec-2] o.s.security.web.FilterChainProxy        : /alunos at position 5 of 11 in additional filter chain; firing Filter: 'OAuth2AuthenticationProcessingFilter'
2018-06-26 11:24:42.641 DEBUG 18658 --- [nio-9090-exec-2] p.a.OAuth2AuthenticationProcessingFilter : Authentication request failed: error="invalid_token", error_description="Invalid access token: 2e4c26b3-0fcf-493e-a255-6216b98811c5"
2018-06-26 11:24:42.645 DEBUG 18658 --- [nio-9090-exec-2] o.s.s.w.header.writers.HstsHeaderWriter  : Not injecting HSTS header since it did not match the requestMatcher org.springframework.security.web.header.writers.HstsHeaderWriter$SecureRequestMatcher@1fa75b
2018-06-26 11:24:42.647 DEBUG 18658 --- [nio-9090-exec-2] s.s.o.p.e.DefaultOAuth2ExceptionRenderer : Written [error="invalid_token", error_description="Invalid access token: 2e4c26b3-0fcf-493e-a255-6216b98811c5"] as "application/xml;charset=UTF-8" using [org.springframework.http.converter.xml.MappingJackson2XmlHttpMessageConverter@30d6fa45]
2018-06-26 11:24:42.647 DEBUG 18658 --- [nio-9090-exec-2] s.s.w.c.SecurityContextPersistenceFilter : SecurityContextHolder now cleared, as request processing completed

文档: https ://docs.spring.io/spring-security-oauth2-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-security-oauth2-authorization-server

标签: javaspringspring-bootspring-securityoauth-2.0

解决方案


我试图复制您的用例:我开发了一个带有 Spring Cloud Security 和 ResourceServer 的 AuthServer。我看到使用 token-info-uri 策略的问题是 spring 为了检查令牌使用 RemoteTokenServices 并且它使用 OAuth2RestTemplate 来检索令牌信息说如果你想使用你的配置你已经插入这种如下配置:

@EnableOAuth2Client
@EnableResourceServer
@SpringBootApplication
public class HelloOauthServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(HelloOauthServiceApplication.class, args);
    }

    @Bean
    public OAuth2RestTemplate oAuth2RestTemplate(OAuth2ProtectedResourceDetails resource){
        return new OAuth2RestTemplate(resource);
    }
}

注意@EnableOAuth2ClientOAuth2RestTemplatebean的定义。spring 使用这些配置来验证和刷新您的令牌。

但是,通过这种方式,任何资源服务器也必须是客户端应用程序,并且根据我的经验,它不能扩展。我个人的建议是使用 user-info-uri 策略。在这种情况下,spring 将使用一个特殊的端点来检索用户信息。在您定义的资源服务器中的配置非常简单,@EnableResourceServer就像在您的 yaml 中的示例中一样,您只能配置资源部分,如下所示

security:
  oauth2:
    resource:
     user-info-uri: http://localhost:9090/account/userInfo.json
     preferTokenInfo: false

唯一的额外开发是在您的身份验证服务器中,它必须在如下端点中公开用户信息:

@RestController
@RequestMapping("/account")
class UserRestFullEndPoint {

    @GetMapping("/userInfo")
    public Principal userInfo(Principal principal){
        return principal;
    }
}

我多次使用这种方法,我注意到它工作得很好并且可以扩展,因为在你的资源服务器中你没有像客户端应用程序那样定义它。

我希望它可能有用。

PS:在您的配置中,您忘记了 http 协议:

server:
  port: 9090
  servlet:
    context-path: /client
    spring:
      application:
        name: client-server
      security:
        oauth2:
          client:
            client-id: estrutura
            client-secret: estruturasecret
            access-token-uri: http://localhost:9999/oauth/token
            user-authorization-uri: http://localhost:9999/oauth/authorize
          resource:
            token-info-uri: http://localhost:9999/oauth/check_token
    logging:
      level:
        org.springframework.security: DEBUG

推荐阅读