@Override
    public Mono<AccessToken> requestToken(AuthorizationCodeTokenRequest request) {

        return Mono
                .justOrEmpty(request.code())
                .map(this::getRedisKey)
                .flatMap(redis.opsForValue()::get)
                .switchIfEmpty(Mono.error(() -> new OAuth2Exception(ErrorType.ILLEGAL_CODE)))
                //移除code
                .flatMap(cache -> redis.opsForValue().delete(getRedisKey(cache.getCode())).thenReturn(cache))
                .flatMap(cache -> {
                    if (!request.getClient().getClientId().equals(cache.getClientId())) {
                        return Mono.error(new OAuth2Exception(ErrorType.ILLEGAL_CLIENT_ID));
                    }
                    return accessTokenManager
                            .createAccessToken(cache.getClientId(), cache.getAuthentication(), false)
                            .flatMap(token -> new OAuth2GrantedEvent(request.getClient(),
                                                                     token,
                                                                     cache.getAuthentication(),
                                                                     cache.getScope(),
                                                                     GrantType.authorization_code,
                                                                     request.getParameters())
                                    .publish(eventPublisher)
                                    .onErrorResume(err -> accessTokenManager
                                            .removeToken(cache.getClientId(), token.getAccessToken())
                                            .then(Mono.error(err)))
                                    .thenReturn(token));
                })
                ;

    }
@Test
    public void testRequestToken() {

        StaticApplicationContext context = new StaticApplicationContext();
        context.refresh();
        context.start();

        DefaultAuthorizationCodeGranter codeGranter = new DefaultAuthorizationCodeGranter(
                new RedisAccessTokenManager(RedisHelper.factory), context, RedisHelper.factory
        );

        OAuth2Client client = new OAuth2Client();
        client.setClientId("test");
        client.setClientSecret("test");
        SimpleAuthentication authentication = new SimpleAuthentication();
        authentication.setUser(SimpleUser
                                       .builder()
                                       .id("test")
                                       .build());

        codeGranter
                .requestCode(new AuthorizationCodeRequest(client, authentication, Collections.emptyMap()))
                .doOnNext(System.out::println)
                .flatMap(response -> codeGranter
                        .requestToken(new AuthorizationCodeTokenRequest(client, Collections.singletonMap("code", response.getCode()))))
                .doOnNext(System.out::println)
                .as(StepVerifier::create)
                .expectNextCount(1)
                .verifyComplete();

    }