1111import org .springframework .security .oauth2 .common .OAuth2AccessToken ;
1212import org .springframework .security .oauth2 .common .OAuth2RefreshToken ;
1313import org .springframework .security .oauth2 .provider .OAuth2Authentication ;
14- import org .springframework .security .oauth2 .provider .token .DefaultAuthenticationKeyGenerator ;
14+ import org .springframework .security .oauth2 .provider .token .AuthenticationKeyGenerator ;
1515import org .springframework .security .oauth2 .provider .token .store .JdbcTokenStore ;
1616import org .springframework .stereotype .Component ;
1717import org .springframework .transaction .annotation .Transactional ;
@@ -26,49 +26,43 @@ public class CustomJdbcTokenStore extends JdbcTokenStore {
2626 private static final String REMOVE_EXPIRED_REFRESH_TOKENS_SQL = "delete from oauth_refresh_token where expiration < ?" ;
2727
2828 private final JdbcTemplate jdbcTemplate ;
29+ private final AuthenticationKeyGenerator authenticationKeyGenerator ;
2930
3031 public CustomJdbcTokenStore (DataSource dataSource ) {
3132 super (dataSource );
3233 this .jdbcTemplate = new JdbcTemplate (dataSource );
33- this .setAuthenticationKeyGenerator (new AuthenticationKeyGeneratorImpl ());
34+ this .authenticationKeyGenerator = new AuthenticationKeyGeneratorImpl ();
35+ this .setAuthenticationKeyGenerator (this .authenticationKeyGenerator );
3436 }
3537
3638 @ Override
3739 public void storeAccessToken (OAuth2AccessToken token , OAuth2Authentication authentication ) {
38- String refreshToken = getRefreshToken ( token ) ;
39- if (this . readAccessToken ( token .getValue () ) != null ) {
40- this . removeAccessToken ( token .getValue () );
40+ String refreshToken = null ;
41+ if (token .getRefreshToken ( ) != null ) {
42+ refreshToken = token . getRefreshToken () .getValue ();
4143 }
4244
43- DefaultAuthenticationKeyGenerator defaultAuthenticationKeyGenerator = new DefaultAuthenticationKeyGenerator ();
45+ if (readAccessToken (token .getValue ()) != null ) {
46+ removeAccessToken (token .getValue ());
47+ }
4448
45- this .jdbcTemplate .update (
46- INSERT_ACCESS_TOKEN_WITH_EXPIRATION_SQL ,
47- new Object []{this .extractTokenKey (token .getValue ()),
48- new SqlLobValue (this .serializeAccessToken (token )),
49- defaultAuthenticationKeyGenerator .extractKey (authentication ),
49+ jdbcTemplate .update (INSERT_ACCESS_TOKEN_WITH_EXPIRATION_SQL , new Object []{extractTokenKey (token .getValue ()),
50+ new SqlLobValue (serializeAccessToken (token )), this .authenticationKeyGenerator .extractKey (authentication ),
5051 authentication .isClientOnly () ? null : authentication .getName (),
5152 authentication .getOAuth2Request ().getClientId (),
52- new SqlLobValue (this .serializeAuthentication (authentication )),
53- this .extractTokenKey (refreshToken ), token .getExpiration ()},
53+ new SqlLobValue (serializeAuthentication (authentication )), extractTokenKey (refreshToken ), token .getExpiration ()},
5454 new int []{Types .VARCHAR , Types .BLOB , Types .VARCHAR , Types .VARCHAR , Types .VARCHAR , Types .BLOB , Types .VARCHAR , Types .TIMESTAMP }
5555 );
5656 }
5757
58- private String getRefreshToken (OAuth2AccessToken token ) {
59- if (token .getRefreshToken () != null ) {
60- return token .getRefreshToken ().getValue ();
61- }
62- return null ;
63- }
64-
6558 @ Override
6659 public void storeRefreshToken (OAuth2RefreshToken refreshToken , OAuth2Authentication authentication ) {
6760 DefaultExpiringOAuth2RefreshToken oAuth2RefreshToken = (DefaultExpiringOAuth2RefreshToken ) refreshToken ;
68- this .jdbcTemplate .update (
69- INSERT_REFRESH_TOKEN_WITH_EXPIRATION_SQL ,
70- new Object []{this .extractTokenKey (refreshToken .getValue ()), new SqlLobValue (this .serializeRefreshToken (refreshToken )), new SqlLobValue (
71- this .serializeAuthentication (authentication )), oAuth2RefreshToken .getExpiration ()},
61+ jdbcTemplate .update (INSERT_REFRESH_TOKEN_WITH_EXPIRATION_SQL , new Object []{
62+ extractTokenKey (refreshToken .getValue ()),
63+ new SqlLobValue (serializeRefreshToken (refreshToken )),
64+ new SqlLobValue (serializeAuthentication (authentication )),
65+ oAuth2RefreshToken .getExpiration ()},
7266 new int []{Types .VARCHAR , Types .BLOB , Types .BLOB , Types .TIMESTAMP }
7367 );
7468 }
0 commit comments