【问题标题】:Store tokens to access other applications on Spring Boot存储令牌以访问 Spring Boot 上的其他应用程序
【发布时间】:2019-01-01 23:22:31
【问题描述】:
我在 Spring Boot 中有一个 rest 应用程序,为它公开的服务配置了安全性并实现了 JWT 令牌。但是这个应用程序还连接到其他第 3 方应用程序,这些应用程序也使用 JWT 和每个应用程序使用不同的令牌进行保护。
我的问题是:存储这些 3rd 方代币的最佳策略是什么?是否有类似SecurityContextHolder 的东西,但用于存储应用程序用于在其他服务上进行身份验证的令牌?
【问题讨论】:
标签:
spring-boot
spring-security
【解决方案1】:
在配置时
OAuth2RestOperations restTemplate
您可以将令牌持久化在客户端中
public OAuth2RestOperations restTemplate() {
OAuth2RestTemplate template = new OAuth2RestTemplate(resource(), new
DefaultOAuth2ClientContext(accessTokenRequest));
AccessTokenProviderChain provider = new
AccessTokenProviderChain(Arrays.asList(new AuthorizationCodeAccessTokenProvider()));
provider.setClientTokenServices(clientTokenServices());
return template;
}
如 spring security oauth docs here 中所述
在客户端中持久化令牌
客户端不需要持久化令牌,但是每次重新启动客户端应用程序时都不需要用户批准新的令牌授予,这可能会很好。 ClientTokenServices 接口定义了为特定用户保留 OAuth 2.0 令牌所需的操作。提供了一个 JDBC 实现,但如果您愿意实现自己的服务,将访问令牌和关联的身份验证实例存储在持久性数据库中,您也可以这样做。如果您想使用此功能,您需要为 OAuth2RestTemplate 提供一个特殊配置的 TokenProvider