mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-06-06 18:30:57 +00:00
Update AnonymusSessionRegistry.java
This commit is contained in:
parent
c14cb03390
commit
9ec728ef00
@ -19,6 +19,7 @@ import jakarta.servlet.http.HttpSessionListener;
|
|||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import stirling.software.SPDF.config.interfaces.SessionsInterface;
|
import stirling.software.SPDF.config.interfaces.SessionsInterface;
|
||||||
|
import stirling.software.SPDF.config.interfaces.SessionsModelInterface;
|
||||||
|
|
||||||
@Component
|
@Component
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@ -27,10 +28,8 @@ public class AnonymusSessionRegistry implements HttpSessionListener, SessionsInt
|
|||||||
@Value("${server.servlet.session.timeout:120s}") // TODO: Change to 30m
|
@Value("${server.servlet.session.timeout:120s}") // TODO: Change to 30m
|
||||||
private Duration defaultMaxInactiveInterval;
|
private Duration defaultMaxInactiveInterval;
|
||||||
|
|
||||||
private static final int MAX_SESSIONS = 1;
|
|
||||||
|
|
||||||
// Map zur Speicherung der Sessions inkl. Timestamp
|
// Map zur Speicherung der Sessions inkl. Timestamp
|
||||||
private static final Map<String, AnonymusSessionInfo> sessions = new ConcurrentHashMap<>();
|
private static final Map<String, SessionsModelInterface> sessions = new ConcurrentHashMap<>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void sessionCreated(HttpSessionEvent event) {
|
public void sessionCreated(HttpSessionEvent event) {
|
||||||
@ -43,15 +42,14 @@ public class AnonymusSessionRegistry implements HttpSessionListener, SessionsInt
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Speichern des anonymousUser-Flags
|
session.setAttribute("principalName", "anonymousUser");
|
||||||
session.setAttribute("anonymousUser", true);
|
|
||||||
// Speichern des Erstellungszeitpunkts
|
// Speichern des Erstellungszeitpunkts
|
||||||
Date creationTime = new Date();
|
Date creationTime = new Date();
|
||||||
session.setAttribute("creationTimestamp", creationTime);
|
|
||||||
|
|
||||||
int allNonExpiredSessions = getAllNonExpiredSessions().size();
|
int allNonExpiredSessions = getAllNonExpiredSessions().size();
|
||||||
|
|
||||||
if (allNonExpiredSessions >= MAX_SESSIONS) {
|
if (allNonExpiredSessions >= getMaxUserSessions()) {
|
||||||
sessions.put(
|
sessions.put(
|
||||||
session.getId(),
|
session.getId(),
|
||||||
new AnonymusSessionInfo(session, creationTime, creationTime, true));
|
new AnonymusSessionInfo(session, creationTime, creationTime, true));
|
||||||
@ -68,7 +66,7 @@ public class AnonymusSessionRegistry implements HttpSessionListener, SessionsInt
|
|||||||
if (session == null) {
|
if (session == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
AnonymusSessionInfo sessionsInfo = sessions.get(session.getId());
|
AnonymusSessionInfo sessionsInfo = (AnonymusSessionInfo) sessions.get(session.getId());
|
||||||
if (sessionsInfo == null) {
|
if (sessionsInfo == null) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@ -86,6 +84,56 @@ public class AnonymusSessionRegistry implements HttpSessionListener, SessionsInt
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Make a session as expired
|
||||||
|
public void expireSession(String sessionId) {
|
||||||
|
if (sessions.containsKey(sessionId)) {
|
||||||
|
AnonymusSessionInfo sessionInfo = (AnonymusSessionInfo) sessions.get(sessionId);
|
||||||
|
sessionInfo.setExpired(true);
|
||||||
|
try {
|
||||||
|
sessionInfo.getSession().invalidate();
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
log.info("Session {} ist bereits invalidiert", sessionInfo.getSession().getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Make all sessions as expired
|
||||||
|
public void expireAllSessions() {
|
||||||
|
sessions.values()
|
||||||
|
.forEach(
|
||||||
|
sessionInfo -> {
|
||||||
|
AnonymusSessionInfo info = (AnonymusSessionInfo) sessionInfo;
|
||||||
|
info.setExpired(true);
|
||||||
|
HttpSession session = info.getSession();
|
||||||
|
try {
|
||||||
|
session.invalidate();
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
log.info("Session {} ist bereits invalidiert", session.getId());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Mark all sessions as expired by username
|
||||||
|
public void expireAllSessionsByUsername(String username) {
|
||||||
|
sessions.values().stream()
|
||||||
|
.filter(
|
||||||
|
sessionInfo -> {
|
||||||
|
AnonymusSessionInfo info = (AnonymusSessionInfo) sessionInfo;
|
||||||
|
return info.getPrincipalName().equals(username);
|
||||||
|
})
|
||||||
|
.forEach(
|
||||||
|
sessionInfo -> {
|
||||||
|
AnonymusSessionInfo info = (AnonymusSessionInfo) sessionInfo;
|
||||||
|
info.setExpired(true);
|
||||||
|
HttpSession session = info.getSession();
|
||||||
|
try {
|
||||||
|
session.invalidate();
|
||||||
|
} catch (IllegalStateException e) {
|
||||||
|
log.info("Session {} ist bereits invalidiert", session.getId());
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isSessionValid(String sessionId) {
|
public boolean isSessionValid(String sessionId) {
|
||||||
boolean exists = sessions.containsKey(sessionId);
|
boolean exists = sessions.containsKey(sessionId);
|
||||||
@ -95,28 +143,67 @@ public class AnonymusSessionRegistry implements HttpSessionListener, SessionsInt
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public boolean isOldestNonExpiredSession(String sessionId) {
|
public boolean isOldestNonExpiredSession(String sessionId) {
|
||||||
Collection<AnonymusSessionInfo> nonExpiredSessions = getAllNonExpiredSessions();
|
Collection<SessionsModelInterface> nonExpiredSessions = getAllNonExpiredSessions();
|
||||||
return nonExpiredSessions.stream()
|
return nonExpiredSessions.stream()
|
||||||
.min(Comparator.comparing(AnonymusSessionInfo::getLastRequest))
|
.min(Comparator.comparing(SessionsModelInterface::getLastRequest))
|
||||||
.map(oldest -> oldest.getSession().getId().equals(sessionId))
|
.map(oldest -> oldest.getSessionId().equals(sessionId))
|
||||||
.orElse(false);
|
.orElse(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void updateSessionLastRequest(String sessionId) {
|
public void updateSessionLastRequest(String sessionId) {
|
||||||
if (sessions.containsKey(sessionId)) {
|
if (sessions.containsKey(sessionId)) {
|
||||||
AnonymusSessionInfo sessionInfo = sessions.get(sessionId);
|
AnonymusSessionInfo sessionInfo = (AnonymusSessionInfo) sessions.get(sessionId);
|
||||||
sessionInfo.setLastRequest(new Date());
|
sessionInfo.setLastRequest(new Date());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<AnonymusSessionInfo> getAllSessions() {
|
public Collection<SessionsModelInterface> getAllSessions() {
|
||||||
return sessions.values();
|
return sessions.values().stream().toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Collection<AnonymusSessionInfo> getAllNonExpiredSessions() {
|
public Collection<SessionsModelInterface> getAllNonExpiredSessions() {
|
||||||
return sessions.values().stream().filter(info -> !info.isExpired()).toList();
|
return sessions.values().stream().filter(info -> !info.isExpired()).toList();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Collection<SessionsModelInterface> getAllIsExpiredSessions() {
|
||||||
|
return sessions.values().stream().filter(SessionsModelInterface::isExpired).toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void clear() {
|
||||||
|
sessions.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Collection<SessionsModelInterface> getAllNonExpiredSessionsBySessionId(
|
||||||
|
String sessionId) {
|
||||||
|
return sessions.values().stream()
|
||||||
|
.filter(info -> !info.isExpired() && info.getSessionId().equals(sessionId))
|
||||||
|
.toList();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerSession(HttpSession session) {
|
||||||
|
if (!sessions.containsKey(session.getId())) {
|
||||||
|
AnonymusSessionInfo sessionInfo =
|
||||||
|
new AnonymusSessionInfo(session, new Date(), new Date(), false);
|
||||||
|
sessions.put(session.getId(), sessionInfo);
|
||||||
|
log.info("Session {} wurde registriert", session.getId());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMaxApplicationSessions() {
|
||||||
|
return getMaxUserSessions();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeSession(HttpSession session) {
|
||||||
|
AnonymusSessionInfo sessionsInfo = (AnonymusSessionInfo) sessions.get(session.getId());
|
||||||
|
sessionsInfo.setExpired(true);
|
||||||
|
session.invalidate();
|
||||||
|
sessions.remove(session.getId());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user