Correction of token service for old memoried user removing
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
package org.codiki.core.security;
|
package org.codiki.core.security;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
import java.util.LinkedList;
|
import java.util.LinkedList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
@@ -17,6 +18,12 @@ public class TokenService {
|
|||||||
|
|
||||||
private static final String HEADER_TOKEN = "token";
|
private static final String HEADER_TOKEN = "token";
|
||||||
|
|
||||||
|
private static final long INTERVAL_USER_CLEANING = 5;
|
||||||
|
|
||||||
|
private static final long INTERVAL_USER_CLEANING_VAL = INTERVAL_USER_CLEANING * 60 * 1000;
|
||||||
|
|
||||||
|
private Date lastUsersCleaning;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class constructor
|
* Class constructor
|
||||||
*/
|
*/
|
||||||
@@ -44,7 +51,12 @@ public class TokenService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// clear all the expired sessions
|
// clear all the expired sessions
|
||||||
clearExpiredUsers();
|
final Date now = new Date();
|
||||||
|
if(lastUsersCleaning == null || now.getTime() - lastUsersCleaning.getTime() >= INTERVAL_USER_CLEANING_VAL) {
|
||||||
|
new Thread(this::clearExpiredUsers).start();
|
||||||
|
lastUsersCleaning = now;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
@@ -53,7 +65,6 @@ public class TokenService {
|
|||||||
* Remove from the connected users map all the elements which their token is
|
* Remove from the connected users map all the elements which their token is
|
||||||
* expired.
|
* expired.
|
||||||
*/
|
*/
|
||||||
@SuppressWarnings("unlikely-arg-type")
|
|
||||||
private void clearExpiredUsers() {
|
private void clearExpiredUsers() {
|
||||||
synchronized (this) {
|
synchronized (this) {
|
||||||
List<User> usersToRemove = new LinkedList<>();
|
List<User> usersToRemove = new LinkedList<>();
|
||||||
@@ -62,7 +73,7 @@ public class TokenService {
|
|||||||
usersToRemove.add(user.getValue());
|
usersToRemove.add(user.getValue());
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
usersToRemove.stream().forEach(connectedUsers::remove);
|
usersToRemove.stream().map(User::getKey).forEach(connectedUsers::remove);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user