如何使用Spring Security 5 OAuth2客户端和RestTemplate刷新OAuth2令牌

Ste*_*ich 4 java spring spring-security spring-boot spring-security-oauth2

Spring Security 5.1.0.M2(发行说明)添加了对使用时自动刷新令牌的支持WebClient。但是,我正在使用RestTemplate。是否有类似的机制RestTemplate或者我需要自己实施该行为?

OAuth2RestTemplate类看起来很有希望,但它从单独的Spring Security的OAuth模块的,我想如果可能使用普通的Spring Security 5.1的客户端上。

Joh*_*anB 5

OAuth2RestTemplate将自动刷新令牌。RestTemplate不会(刷新令牌是OAut2规范的一部分,因此是OAuth2RestTemplate。

您有2个选择:

  1. 使用Spring Security OAuth2模块,一切将立即可用(Spring提供的配置属性)
  2. 根据Spring的OAut2RestTemplate创建自己的RestTemplate

Spring的OAuth2模块将来会集成到Spring Security中。我会选择选项1。

  • 您能否指向官方文档,说明令牌会自动刷新。对于我的具有授权码授予类型的应用程序,它们没有刷新,并且1小时(访问令牌的过期时间)后事情停止工作 (3认同)