mirror of
https://github.com/MewoLab/AquaDX.git
synced 2026-02-14 01:37:28 +08:00
[+] Find session when validating request
This commit is contained in:
@@ -23,7 +23,8 @@ import org.springframework.web.servlet.config.annotation.WebMvcConfigurer
|
|||||||
@Component
|
@Component
|
||||||
@ConditionalOnBean(AllNetSecureInit::class)
|
@ConditionalOnBean(AllNetSecureInit::class)
|
||||||
class TokenChecker(
|
class TokenChecker(
|
||||||
val keyChipRepo: KeyChipRepo
|
val keyChipRepo: KeyChipRepo,
|
||||||
|
val keychipSessionService: KeychipSessionService,
|
||||||
) : HandlerInterceptor {
|
) : HandlerInterceptor {
|
||||||
val log = LoggerFactory.getLogger(TokenChecker::class.java)
|
val log = LoggerFactory.getLogger(TokenChecker::class.java)
|
||||||
|
|
||||||
@@ -40,7 +41,8 @@ class TokenChecker(
|
|||||||
|
|
||||||
// Check whether the token exists in the database
|
// Check whether the token exists in the database
|
||||||
// The token can either be a keychip id (old method) or a session id (new method)
|
// The token can either be a keychip id (old method) or a session id (new method)
|
||||||
if (token.isNotBlank() && keyChipRepo.existsByKeychipId(token))
|
val session = keychipSessionService.find(token)
|
||||||
|
if (token.isNotBlank() && (keyChipRepo.existsByKeychipId(token) || session != null))
|
||||||
{
|
{
|
||||||
// Forward the request
|
// Forward the request
|
||||||
val w = RewriteWrapper(req, token).apply { setAttribute("token", token) }
|
val w = RewriteWrapper(req, token).apply { setAttribute("token", token) }
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ class KeychipSession(
|
|||||||
val token: String = genUrlSafeToken(32),
|
val token: String = genUrlSafeToken(32),
|
||||||
|
|
||||||
@Column(nullable = false)
|
@Column(nullable = false)
|
||||||
val lastUse: Long = System.currentTimeMillis()
|
var lastUse: Long = System.currentTimeMillis()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -45,9 +45,6 @@ interface KeychipSessionRepo : JpaRepository<KeychipSession, String> {
|
|||||||
fun deleteAllByLastUseBefore(expire: Long)
|
fun deleteAllByLastUseBefore(expire: Long)
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Service to regularly delete unused keychip sessions.
|
|
||||||
*/
|
|
||||||
@Service
|
@Service
|
||||||
class KeychipSessionService(
|
class KeychipSessionService(
|
||||||
val keychipSessionRepo: KeychipSessionRepo,
|
val keychipSessionRepo: KeychipSessionRepo,
|
||||||
@@ -55,6 +52,9 @@ class KeychipSessionService(
|
|||||||
) {
|
) {
|
||||||
val logger = LoggerFactory.getLogger(KeychipSessionService::class.java)
|
val logger = LoggerFactory.getLogger(KeychipSessionService::class.java)
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete sessions that are older than the expire time.
|
||||||
|
*/
|
||||||
@Scheduled(fixedDelayString = "\${allnet.server.keychip-ses-clean-interval}")
|
@Scheduled(fixedDelayString = "\${allnet.server.keychip-ses-clean-interval}")
|
||||||
fun cleanup() {
|
fun cleanup() {
|
||||||
logger.info("!!! Keychip session cleanup !!!")
|
logger.info("!!! Keychip session cleanup !!!")
|
||||||
@@ -62,8 +62,19 @@ class KeychipSessionService(
|
|||||||
keychipSessionRepo.deleteAllByLastUseBefore(expire)
|
keychipSessionRepo.deleteAllByLastUseBefore(expire)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new session.
|
||||||
|
*/
|
||||||
fun new(user: AquaNetUser): KeychipSession {
|
fun new(user: AquaNetUser): KeychipSession {
|
||||||
val session = KeychipSession(user = user)
|
val session = KeychipSession(user = user)
|
||||||
return keychipSessionRepo.save(session)
|
return keychipSessionRepo.save(session)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find a session. If found, renew the last use time.
|
||||||
|
*/
|
||||||
|
fun find(token: String) = keychipSessionRepo.findByToken(token)?.apply {
|
||||||
|
lastUse = System.currentTimeMillis()
|
||||||
|
keychipSessionRepo.save(this)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user