首页 文章

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

提问于
浏览
-1

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

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

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

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);
      }
    }
    

    You should take care that the endpoint itself is properly secured.

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

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

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

相关问题