package ee.tuleva.onboarding.auth.session;

import java.io.Serializable;
import java.util.Optional;
import javax.servlet.http.HttpSession;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

@Component
public class GenericSessionStore {

  public <T extends Serializable> void save(T sessionAttribute) {
    session().setAttribute(sessionAttribute.getClass().getName(), sessionAttribute);
  }

  public <T extends Serializable> Optional<T> get(Class clazz) {
    @SuppressWarnings("unchecked")
    T sessionAttribute = (T) session().getAttribute(clazz.getName());

    if (sessionAttribute == null) {
      return Optional.empty();
    }

    return Optional.of(sessionAttribute);
  }

  private static HttpSession session() {
    ServletRequestAttributes attr =
        (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
    boolean allowCreate = true;
    return attr.getRequest().getSession(allowCreate);
  }
}

@@ -37,16 +37,39 @@
 ssion;%0A%0A
+import java.io.Serial;%0A
 import j
@@ -110,55 +110,64 @@
 til.
-Optional;%0Aimport javax.servlet.http.HttpSess
+HashMap;%0Aimport java.util.Map;%0Aimport java.util.Opt
 ion
+al
 ;%0Aim
@@ -195,28 +195,32 @@
 ork.
-stereotype.Component
+context.annotation.Scope
 ;%0Aim
@@ -240,36 +240,32 @@
 ngframework.
-web.
 context.
 request.Requ
@@ -256,36 +256,34 @@
 ext.
-request.RequestContextHol
+annotation.ScopedProxyMo
 de
-r
 ;%0Aim
@@ -311,100 +311,320 @@
 ork.
-web.context.request.ServletRequestAttributes;%0A%0A@Component%0Apublic class GenericSessionStore %7B
+stereotype.Component;%0A%0A@Component%0A@Scope(value = %22session%22, proxyMode = ScopedProxyMode.TARGET_CLASS)%0Apublic class GenericSessionStore implements Serializable %7B%0A%0A  @Serial private static final long serialVersionUID = -648103071415508424L;%0A%0A  private final Map%3CString, Object%3E sessionAttributes = new HashMap%3C%3E();
 %0A%0A  
@@ -702,14 +702,8 @@
 sion
-().set
 Attr
@@ -707,16 +707,21 @@
 ttribute
+s.put
 (session
@@ -912,22 +912,16 @@
  session
-().get
 Attribut
@@ -921,16 +921,21 @@
 ttribute
+s.get
 (clazz.g
@@ -1069,259 +1069,7 @@
 %0A  %7D
-%0A%0A  private static HttpSession session() %7B%0A    ServletRequestAttributes attr =%0A        (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();%0A    boolean allowCreate = true;%0A    return attr.getRequest().getSession(allowCreate);%0A  %7D
 %0A%7D%0A

Replace manual session handling with spring session scoped bean