从Spring Security Oauth2中的Token Store检索访问和刷新令牌的方法

问题描述 投票:-1回答:1

如果您在令牌的内存存储中使用,是否有办法从Spring Security Oauth2中的TokenStore检索访问和刷新令牌?

是否有任何端点可以使这更容易,还是有任何其他解决方法?

根据我的分析,TokenEndpoint除了/ oauth / token之外不提供任何端点:https://docs.spring.io/spring-security/oauth/apidocs/org/springframework/security/oauth2/provider/endpoint/TokenEndpoint.html

spring spring-security spring-security-oauth2
1个回答
1
投票

如果您在配置中将TokenStore定义为Bean,则可以将其注入您自己的控制器中:

@Configuration
@EnableAuthorizationServer
public class AuthServerOAuth2Config extends AuthorizationServerConfigurerAdapter {
    //...
    @Bean
    public TokenStore tokenStore() {
       return new InMemoryTokenStore();
    }
}

在控制器中,您可以提供一个读取令牌并返回它们的处理程序:

@RestController
@RequestMapping("/api/tokens")
public class TokensEndpoint {
  @Autowired
  TokenStore tokenStore;

  @GetMapping
  public ResponseEntity<List<String>> allTokens(@RequestParam String clientId) {
    Collection<OAuth2AccessToken> tokens = tokenStore.findTokensByClientId(clientId);
    List<String> tokenValues = tokens.stream().map(OAuth2AccessToken::getValue).collect(Collectors.toList());
    return ResponseEntity.ok(tokenValues);
  }
}

您应该注意端点本身是否正确安全。

然后你可以像这样检索访问令牌:

curl -X GET 'http://localhost:8080/api/tokens?clientId=sampleClientId'

如果您还想包含刷新令牌,则只需要在处理程序方法中扩展映射。

© www.soinside.com 2019 - 2024. All rights reserved.