Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

358
Visualizações
How to store OAuth token in database?

Using Java 8 and Spring Boot 1.5.2

Spring Security: I'm trying to store tokens in Cassandra database. To achieve this I've implemented TokenStore. I ran the program and noticed that getAccessToken(...) method is called before storeAccessToken(...) and due to this token is always null. What do I need to do to first store access token in database and then query for it?

@Override
public OAuth2AccessToken getAccessToken(final OAuth2Authentication authentication) {
    OAuth2AccessToken accessToken = null;

    final String key = authenticationKeyGenerator.extractKey(authentication);

    final Select select = QueryBuilder.select("token_body").from("authentication_service", "oauth_access_token");
    select.where(QueryBuilder.eq("authentication_id", key));
    final ByteBuffer token = cassandraOperations.queryForObject(select, ByteBuffer.class);
    if (token != null) {
        accessToken = deserializeAccessToken(token.array());
        if (accessToken != null && !key.equals(authenticationKeyGenerator.extractKey(readAuthentication(accessToken.getValue())))) {
            removeAccessToken(accessToken.getValue());
            storeAccessToken(accessToken, authentication);
        }
    }
    return accessToken;
}

storeAccessToken(...)

@Override
public void storeAccessToken(final OAuth2AccessToken token, final OAuth2Authentication authentication) {
    String refreshToken = null;
    if (token.getRefreshToken() != null) {
        refreshToken = token.getRefreshToken().getValue();
    }

    if (readAccessToken(token.getValue()) != null) {
        removeAccessToken(token.getValue());
    }

    final AccessTokenBuilder accessTokenBuilder = new AccessTokenBuilder();
    accessTokenRepository.save(accessTokenBuilder
                                       .authenticationId(authenticationKeyGenerator.extractKey(authentication))
                                       .tokenId(extractTokenKey(token.getValue()))
                                       .tokenBody(ByteBuffer.wrap(serializeAccessToken(token)))
                                       .username(authentication.getName())
                                       .clientId(authentication.getOAuth2Request().getClientId())
                                       .authentication(ByteBuffer.wrap(serializeAuthentication(authentication)))
                                       .refreshTokenId(extractTokenKey(refreshToken))
                                       .createAccessToken());

}
about 4 years ago · Santiago Trujillo
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda