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

Posted

技术标签:

【中文标题】Spring boot 2.0.3 + 安全 + Oauth2 自动配置【英文标题】:Spring boot 2.0.3 + Security + Oauth2 autoconfigure 【发布时间】:2018-12-05 11:42:01 【问题描述】:

我正在使用 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"
    

客户微服务:localhost:9090

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

application.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

【问题讨论】:

***.com/questions/48761624/… 【参考方案1】:

我尝试复制您的用例:我开发了一个具有 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);
    

注意@EnableOAuth2ClientOAuth2RestTemplate的bean定义。 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

【讨论】:

详细回答很好。可以使用第三方 Autorization Server(如 Azure AD 等)并且仍然采用类似的方法吗? 嗨 Turcia 我从未尝试过与其他提供商合作,但我个人的建议是尝试最新的 spring security oauth2。问题中讨论的库基于 Spring Cloud 安全性,并且 SSO 功能已在最新的 Spring Boot 版本中被弃用和删除 我的意思是 Spring Cloud 安全性和遗留 oauth2 库的答案很好,但是这些库在 Spring 中已被弃用,有利于对 oauth2 和 openid connect 的最新 Spring 安全性支持 谢谢 Valerio,我去看看。

以上是关于Spring boot 2.0.3 + 安全 + Oauth2 自动配置的主要内容,如果未能解决你的问题,请参考以下文章

IntelliJ IDEA 2017版 spring-boot 2.0.3 邮件发送搭建,概念梳理

spring-boot-2.0.3不一样系列之shiro - 搭建篇

Spring Boot 版本 2.0.3.RELEASE 中的分页不起作用

将 Spring Boot 版本从 2.0.3.RELEASE 更改为 2.1.0.M4 时出现问题

基于Spring Boot 2.0.3的Spring Cloud Eureka Server与Client

spring boot 2.0.3+spring cloud (Finchley)7微服务监控Spring Cloud Admin