|
1 | 1 | package com.exadel.frs.system.security; |
2 | 2 |
|
3 | 3 | import java.sql.Types; |
| 4 | +import java.time.LocalDateTime; |
4 | 5 | import javax.sql.DataSource; |
| 6 | +import lombok.extern.slf4j.Slf4j; |
5 | 7 | import org.springframework.jdbc.core.JdbcTemplate; |
6 | 8 | import org.springframework.jdbc.core.support.SqlLobValue; |
| 9 | +import org.springframework.scheduling.annotation.Scheduled; |
7 | 10 | import org.springframework.security.oauth2.common.DefaultExpiringOAuth2RefreshToken; |
8 | 11 | import org.springframework.security.oauth2.common.OAuth2AccessToken; |
9 | 12 | import org.springframework.security.oauth2.common.OAuth2RefreshToken; |
10 | 13 | import org.springframework.security.oauth2.provider.OAuth2Authentication; |
11 | 14 | import org.springframework.security.oauth2.provider.token.DefaultAuthenticationKeyGenerator; |
12 | 15 | import org.springframework.security.oauth2.provider.token.store.JdbcTokenStore; |
| 16 | +import org.springframework.stereotype.Component; |
| 17 | +import org.springframework.transaction.annotation.Transactional; |
13 | 18 |
|
| 19 | +@Slf4j |
| 20 | +@Component |
14 | 21 | public class CustomJdbcTokenStore extends JdbcTokenStore { |
15 | 22 |
|
16 | 23 | 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 (?, ?, ?, ?, ?, ?, ?,?)"; |
17 | 24 | 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 < ?"; |
18 | 27 |
|
19 | 28 | private final JdbcTemplate jdbcTemplate; |
20 | 29 |
|
21 | 30 | public CustomJdbcTokenStore(DataSource dataSource) { |
22 | 31 | super(dataSource); |
23 | | - jdbcTemplate = new JdbcTemplate(dataSource); |
| 32 | + this.jdbcTemplate = new JdbcTemplate(dataSource); |
| 33 | + this.setAuthenticationKeyGenerator(new AuthenticationKeyGeneratorImpl()); |
24 | 34 | } |
25 | 35 |
|
26 | 36 | @Override |
@@ -62,4 +72,23 @@ public void storeRefreshToken(OAuth2RefreshToken refreshToken, OAuth2Authenticat |
62 | 72 | new int[]{Types.VARCHAR, Types.BLOB, Types.BLOB, Types.TIMESTAMP} |
63 | 73 | ); |
64 | 74 | } |
| 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 | + } |
65 | 94 | } |
0 commit comments