Update AnonymusSessionStatusController.java

This commit is contained in:
Ludy87 2025-03-26 12:06:10 +01:00
parent fe378042f0
commit 5ca84f4aa3
No known key found for this signature in database
GPG Key ID: 92696155E0220F94

View File

@ -1,7 +1,8 @@
package stirling.software.SPDF.config.anonymus.session;
import java.util.ArrayList;
import java.util.List;
import java.util.Collection;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
@ -14,35 +15,67 @@ import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import stirling.software.SPDF.config.interfaces.SessionsInterface;
@RestController
@Slf4j
public class AnonymusSessionStatusController {
@Autowired private AnonymusSessionRegistry sessionRegistry;
private static final int MAX_SESSIONS = 3;
@Autowired private SessionsInterface sessionsInterface;
private static final int MAX_SESSIONS = 1;
@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 (session == null) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("No session found");
}
if (allNonExpiredSessions.size() > MAX_SESSIONS) {
Collection<AnonymusSessionInfo> allNonExpiredSessions =
new ArrayList<>(sessionRegistry.getAllNonExpiredSessions());
if (allNonExpiredSessions.isEmpty()) {
allNonExpiredSessions.add(
new AnonymusSessionInfo(session, new Date(), new Date(), false));
}
// wenn session expire ist dann UNAUTHORIZED
if (allNonExpiredSessions.stream()
.anyMatch(s -> s.getSession().getId().equals(session.getId()) && s.isExpired())) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("Session expired");
}
// wenn nicht in der Liste dann UNAUTHORIZED
if (allNonExpiredSessions.stream()
.noneMatch(s -> s.getSession().getId().equals(session.getId()))) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("No session found");
}
if (allNonExpiredSessions.size() > MAX_SESSIONS
&& sessionsInterface.isSessionValid(session.getId())
&& sessionsInterface.isOldestNonExpiredSession(session.getId())) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body("Session ungültig oder abgelaufen");
} else if (session != null) {
return ResponseEntity.ok("Session gültig: " + session.getId());
}
return ResponseEntity.ok("Session gültig: " + session.getId());
}
@GetMapping("/session/expire")
public ResponseEntity<String> expireSession(HttpServletRequest request) {
HttpSession session = request.getSession(false);
if (session != null) {
session.invalidate();
return ResponseEntity.ok("Session invalidated");
} else {
return ResponseEntity.ok("User has session");
return ResponseEntity.ok("No session to invalidate");
}
}
@GetMapping("/session/expire/all")
public ResponseEntity<String> expireAllSessions() {
sessionRegistry
.getAllNonExpiredSessions()
.forEach(sessionInfo -> sessionInfo.getSession().invalidate());
return ResponseEntity.ok("All sessions invalidated");
}
}