java - How to store OAuth token in database? -


using java 8 , spring boot 1.5.2

spring security: i'm trying store tokens in cassandra database. achieve i've implemented tokenstore. ran program , noticed getaccesstoken(...) method called before storeaccesstoken(...) , due token null. need first store access token in database , query 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());  } 


Comments