Skip to content

Commit c695401

Browse files
EFRS-1286: Added a remove expired OAuth tokens scheduler
1 parent 19c8560 commit c695401

File tree

2 files changed

+34
-12
lines changed

2 files changed

+34
-12
lines changed

java/admin/src/main/java/com/exadel/frs/system/security/CustomJdbcTokenStore.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,36 @@
11
package com.exadel.frs.system.security;
22

33
import java.sql.Types;
4+
import java.time.LocalDateTime;
45
import javax.sql.DataSource;
6+
import lombok.extern.slf4j.Slf4j;
57
import org.springframework.jdbc.core.JdbcTemplate;
68
import org.springframework.jdbc.core.support.SqlLobValue;
9+
import org.springframework.scheduling.annotation.Scheduled;
710
import org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken;
811
import org.springframework.security.oauth2.common.OAuth2AccessToken;
912
import org.springframework.security.oauth2.common.OAuth2RefreshToken;
1013
import org.springframework.security.oauth2.provider.OAuth2Authentication;
1114
import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator;
1215
import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore;
16+
import org.springframework.stereotype.Component;
17+
import org.springframework.transaction.annotation.Transactional;
1318

19+
@Slf4j
20+
@Component
1421
public class CustomJdbcTokenStore extends JdbcTokenStore {
1522

1623
private static final String INSERT_ACCESS_TOKEN_WITH_EXPIRATION_SQL = "insert into oauth_access_token (token_id, token, authentication_id, user_name, client_id, authentication, refresh_token, expiration) values (?, ?, ?, ?, ?, ?, ?,?)";
1724
private static final String INSERT_REFRESH_TOKEN_WITH_EXPIRATION_SQL = "insert into oauth_refresh_token (token_id, token, authentication, expiration) values (?, ?, ?, ?)";
25+
private static final String REMOVE_EXPIRED_ACCESS_TOKENS_SQL = "delete from oauth_access_token where expiration < ?";
26+
private static final String REMOVE_EXPIRED_REFRESH_TOKENS_SQL = "delete from oauth_refresh_token where expiration < ?";
1827

1928
private final JdbcTemplate jdbcTemplate;
2029

2130
public CustomJdbcTokenStore(DataSource dataSource) {
2231
super(dataSource);
23-
jdbcTemplate = new JdbcTemplate(dataSource);
32+
this.jdbcTemplate = new JdbcTemplate(dataSource);
33+
this.setAuthenticationKeyGenerator(new AuthenticationKeyGeneratorImpl());
2434
}
2535

2636
@Override
@@ -62,4 +72,23 @@ public void storeRefreshToken(OAuth2RefreshToken refreshToken, OAuth2Authenticat
6272
new int[]{Types.VARCHAR, Types.BLOB, Types.BLOB, Types.TIMESTAMP}
6373
);
6474
}
75+
76+
@Transactional
77+
@Scheduled(cron = "@weekly")
78+
public void removeExpiredTokens() {
79+
LocalDateTime now = LocalDateTime.now();
80+
int accessTokenCount = this.jdbcTemplate.update(
81+
REMOVE_EXPIRED_ACCESS_TOKENS_SQL,
82+
now
83+
);
84+
int refreshTokenCount = this.jdbcTemplate.update(
85+
REMOVE_EXPIRED_REFRESH_TOKENS_SQL,
86+
now
87+
);
88+
log.info(
89+
"Removed {} expired access tokens and {} expired update tokens",
90+
accessTokenCount,
91+
refreshTokenCount
92+
);
93+
}
6594
}

java/admin/src/main/java/com/exadel/frs/system/security/config/AuthServerConfig.java

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,9 @@ public class AuthServerConfig extends AuthorizationServerConfigurerAdapter {
5757
private final AuthenticationManager authenticationManager;
5858
private final ClientService clientService;
5959
private final CustomUserDetailsService userDetailsService;
60-
private final DataSource dataSource;
6160
private final PasswordEncoder passwordEncoder;
6261
private final OAuthClientProperties authClientProperties;
63-
64-
@Bean
65-
public JdbcTokenStore tokenStore() {
66-
JdbcTokenStore tokenStore = new CustomJdbcTokenStore(dataSource);
67-
tokenStore.setAuthenticationKeyGenerator(new AuthenticationKeyGeneratorImpl());
68-
return tokenStore;
69-
}
62+
private final JdbcTokenStore tokenStore;
7063

7164
@Bean
7265
@Primary
@@ -83,7 +76,7 @@ public TokenEndpoint tokenEndpoint(AuthorizationServerEndpointsConfiguration con
8376

8477
@Bean
8578
public DefaultTokenServices tokenServices() {
86-
TokenServicesImpl tokenServices = new TokenServicesImpl(tokenStore());
79+
TokenServicesImpl tokenServices = new TokenServicesImpl(tokenStore);
8780
tokenServices.setClientDetailsService(clientService);
8881
return tokenServices;
8982
}
@@ -119,7 +112,7 @@ public void configure(final ClientDetailsServiceConfigurer clients) throws Excep
119112
@Override
120113
public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
121114
endpoints
122-
.tokenStore(tokenStore())
115+
.tokenStore(tokenStore)
123116
.tokenServices(tokenServices())
124117
.authenticationManager(authenticationManager)
125118
.userDetailsService(userDetailsService)
@@ -136,4 +129,4 @@ public void configure(AuthorizationServerEndpointsConfigurer endpoints) {
136129
}
137130
});
138131
}
139-
}
132+
}

0 commit comments

Comments
 (0)