mirror of
https://github.com/Stirling-Tools/Stirling-PDF.git
synced 2025-04-19 11:11:18 +00:00
AnonymusSession
This commit is contained in:
parent
1c33c39c57
commit
69b12030d5
@ -0,0 +1,48 @@
|
||||
package stirling.software.SPDF.config.anonymus.session;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
|
||||
public class AnonymusSessionInfo {
|
||||
private HttpSession session;
|
||||
private final Date createdAt;
|
||||
private Date lastRequest;
|
||||
private Boolean expired;
|
||||
|
||||
public AnonymusSessionInfo(
|
||||
HttpSession session, Date createdAt, Date lastRequest, Boolean expired) {
|
||||
this.session = session;
|
||||
this.createdAt = createdAt;
|
||||
this.expired = expired;
|
||||
this.lastRequest = lastRequest;
|
||||
}
|
||||
|
||||
public void setSession(HttpSession session) {
|
||||
this.session = session;
|
||||
}
|
||||
|
||||
public HttpSession getSession() {
|
||||
return session;
|
||||
}
|
||||
|
||||
public Date getCreatedAt() {
|
||||
return createdAt;
|
||||
}
|
||||
|
||||
public void setExpired(Boolean expired) {
|
||||
this.expired = expired;
|
||||
}
|
||||
|
||||
public Boolean isExpired() {
|
||||
return expired;
|
||||
}
|
||||
|
||||
public void setLastRequest(Date lastRequest) {
|
||||
this.lastRequest = lastRequest;
|
||||
}
|
||||
|
||||
public Date getLastRequest() {
|
||||
return lastRequest;
|
||||
}
|
||||
}
|
@ -0,0 +1,75 @@
|
||||
package stirling.software.SPDF.config.anonymus.session;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
import jakarta.servlet.http.HttpSessionEvent;
|
||||
import jakarta.servlet.http.HttpSessionListener;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class AnonymusSessionRegistry implements HttpSessionListener {
|
||||
|
||||
// Map zur Speicherung der Sessions inkl. Timestamp
|
||||
private static final Map<String, AnonymusSessionInfo> sessions = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
public void sessionCreated(HttpSessionEvent event) {
|
||||
HttpSession session = event.getSession();
|
||||
if (session == null) {
|
||||
log.info("Session ist null");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.println("");
|
||||
System.out.println("Session created with id: " + session.getId());
|
||||
System.out.println("");
|
||||
|
||||
if (sessions.containsKey(session.getId())) {
|
||||
log.info("Session {} existiert bereits", session.getId());
|
||||
return;
|
||||
}
|
||||
|
||||
// Speichern des anonymousUser-Flags
|
||||
session.setAttribute("anonymousUser", true);
|
||||
// Speichern des Erstellungszeitpunkts
|
||||
Date creationTime = new Date();
|
||||
session.setAttribute("creationTimestamp", creationTime);
|
||||
sessions.put(
|
||||
session.getId(),
|
||||
new AnonymusSessionInfo(session, creationTime, creationTime, false));
|
||||
|
||||
log.info("Session {} erstellt um {}", session.getId(), creationTime);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void sessionDestroyed(HttpSessionEvent event) {
|
||||
HttpSession session = event.getSession();
|
||||
if (session == null) {
|
||||
log.info("Session ist null");
|
||||
return;
|
||||
}
|
||||
AnonymusSessionInfo sessionsInfo = sessions.get(session.getId());
|
||||
if (sessionsInfo == null) {
|
||||
log.info("Session {} existiert nicht", session.getId());
|
||||
return;
|
||||
}
|
||||
sessionsInfo.setExpired(true);
|
||||
log.info("Session {} wurde Expired=TRUE", session.getId());
|
||||
}
|
||||
|
||||
public Collection<AnonymusSessionInfo> getAllSessions() {
|
||||
return sessions.values();
|
||||
}
|
||||
|
||||
public Collection<AnonymusSessionInfo> getAllNonExpiredSessions() {
|
||||
return sessions.values().stream().filter(info -> !info.isExpired()).toList();
|
||||
}
|
||||
}
|
@ -0,0 +1,29 @@
|
||||
package stirling.software.SPDF.config.anonymus.session;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.scheduling.annotation.Scheduled;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@Component
|
||||
@Slf4j
|
||||
public class AnonymusSessionService {
|
||||
|
||||
@Autowired private AnonymusSessionRegistry sessionRegistry;
|
||||
|
||||
@Scheduled(cron = "0 0/1 * * * ?")
|
||||
public void expireSessions() {
|
||||
List<AnonymusSessionInfo> allNonExpiredSessions =
|
||||
new ArrayList<>(sessionRegistry.getAllNonExpiredSessions());
|
||||
if (allNonExpiredSessions.isEmpty()) {
|
||||
log.info("Keine nicht abgelaufenen Sessions gefunden.");
|
||||
return;
|
||||
} else {
|
||||
log.info("Es gibt {} nicht abgelaufene Sessions", allNonExpiredSessions.size());
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,48 @@
|
||||
package stirling.software.SPDF.config.anonymus.session;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpSession;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
@RestController
|
||||
@Slf4j
|
||||
public class AnonymusSessionStatusController {
|
||||
|
||||
@Autowired private AnonymusSessionRegistry sessionRegistry;
|
||||
private static final int MAX_SESSIONS = 3;
|
||||
|
||||
@GetMapping("/session/status")
|
||||
public ResponseEntity<String> getSessionStatus(HttpServletRequest request) {
|
||||
HttpSession session = request.getSession(false);
|
||||
List<AnonymusSessionInfo> allNonExpiredSessions =
|
||||
new ArrayList<>(sessionRegistry.getAllNonExpiredSessions());
|
||||
|
||||
for (AnonymusSessionInfo info : allNonExpiredSessions) {
|
||||
log.info(
|
||||
"Session ID: {}, Created At: {}, Last Request: {}, Expired: {}",
|
||||
info.getSession().getId(),
|
||||
info.getCreatedAt(),
|
||||
info.getLastRequest(),
|
||||
info.isExpired());
|
||||
}
|
||||
|
||||
if (allNonExpiredSessions.size() > MAX_SESSIONS) {
|
||||
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
|
||||
.body("Session ungültig oder abgelaufen");
|
||||
} else if (session != null) {
|
||||
return ResponseEntity.ok("Session gültig: " + session.getId());
|
||||
} else {
|
||||
return ResponseEntity.ok("User has session");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user