Update AnonymusSessionStatusController.java

This commit is contained in:
Ludy87 2025-03-27 13:14:28 +01:00
parent cb725ccf8c
commit 27db4d6de2
No known key found for this signature in database
GPG Key ID: 92696155E0220F94

View File

@ -1,29 +1,20 @@
package stirling.software.SPDF.config.anonymus.session; package stirling.software.SPDF.config.anonymus.session;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity; import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession; import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import stirling.software.SPDF.config.interfaces.SessionsInterface;
@RestController @RestController
@Slf4j
public class AnonymusSessionStatusController { public class AnonymusSessionStatusController {
@Autowired private AnonymusSessionRegistry sessionRegistry; @Autowired private AnonymusSessionRegistry sessionRegistry;
@Autowired private SessionsInterface sessionsInterface;
private static final int MAX_SESSIONS = 1;
@GetMapping("/session/status") @GetMapping("/session/status")
public ResponseEntity<String> getSessionStatus(HttpServletRequest request) { public ResponseEntity<String> getSessionStatus(HttpServletRequest request) {
@ -32,39 +23,32 @@ public class AnonymusSessionStatusController {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("No session found"); return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("No session found");
} }
Collection<AnonymusSessionInfo> allNonExpiredSessions = boolean isActivSesssion =
new ArrayList<>(sessionRegistry.getAllNonExpiredSessions()); sessionRegistry.getAllSessions().stream()
if (allNonExpiredSessions.isEmpty()) { .filter(s -> s.getSessionId().equals(session.getId()))
allNonExpiredSessions.add( .anyMatch(s -> !s.isExpired());
new AnonymusSessionInfo(session, new Date(), new Date(), false));
}
// wenn session expire ist dann UNAUTHORIZED long sessionCount =
if (allNonExpiredSessions.stream() sessionRegistry.getAllSessions().stream().filter(s -> !s.isExpired()).count();
.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 long userSessions = sessionCount;
if (allNonExpiredSessions.stream() int maxUserSessions = sessionRegistry.getMaxUserSessions();
.noneMatch(s -> s.getSession().getId().equals(session.getId()))) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("No session found");
}
if (allNonExpiredSessions.size() > MAX_SESSIONS if (userSessions >= maxUserSessions && !isActivSesssion) {
&& sessionsInterface.isSessionValid(session.getId())
&& sessionsInterface.isOldestNonExpiredSession(session.getId())) {
return ResponseEntity.status(HttpStatus.UNAUTHORIZED) return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
.body("Session ungültig oder abgelaufen"); .body("Session ungültig oder abgelaufen");
} } else if (session.getId() != null && isActivSesssion) {
return ResponseEntity.ok("Session gültig: " + session.getId()); return ResponseEntity.ok("Session gültig: " + session.getId());
} else {
return ResponseEntity.ok("User has " + userSessions + " sessions");
}
} }
@GetMapping("/session/expire") @GetMapping("/session/expire")
public ResponseEntity<String> expireSession(HttpServletRequest request) { public ResponseEntity<String> expireSession(HttpServletRequest request) {
HttpSession session = request.getSession(false); HttpSession session = request.getSession(false);
if (session != null) { if (session != null) {
session.invalidate(); sessionRegistry.expireSession(session.getId());
return ResponseEntity.ok("Session invalidated"); return ResponseEntity.ok("Session invalidated");
} else { } else {
return ResponseEntity.ok("No session to invalidate"); return ResponseEntity.ok("No session to invalidate");
@ -73,9 +57,13 @@ public class AnonymusSessionStatusController {
@GetMapping("/session/expire/all") @GetMapping("/session/expire/all")
public ResponseEntity<String> expireAllSessions() { public ResponseEntity<String> expireAllSessions() {
sessionRegistry sessionRegistry.expireAllSessions();
.getAllNonExpiredSessions()
.forEach(sessionInfo -> sessionInfo.getSession().invalidate());
return ResponseEntity.ok("All sessions invalidated"); return ResponseEntity.ok("All sessions invalidated");
} }
@GetMapping("/session/expire/{username}")
public ResponseEntity<String> expireAllSessionsByUsername(@PathVariable String username) {
sessionRegistry.expireAllSessionsByUsername(username);
return ResponseEntity.ok("All sessions invalidated for user: " + username);
}
} }