1 package de.juplo.yourshouter;
3 import org.slf4j.Logger;
4 import org.slf4j.LoggerFactory;
8 * Simple SecurityContext that stores the currently signed-in connection in a
13 public final class SecurityContext
15 private final static Logger LOG = LoggerFactory.getLogger(SecurityContext.class);
16 private final static ThreadLocal<String> CURRENT_USER = new ThreadLocal<>();
20 * Fetches the ID of the current user from the thread-local.
23 * The ID of the current user, or <code>null</code> if no user is known.
25 public static String getCurrentUser()
27 String user = CURRENT_USER.get();
28 LOG.debug("current user: {}", user);
33 * Stores the given ID as the ID of the current user in the thread-local.
36 * The ID to store as the ID of the current user.
38 public static void setCurrentUser(String user)
40 LOG.debug("setting current user: {}", user);
41 CURRENT_USER.set(user);
45 * Checks, if a user is signed in. That is, if the ID of a user is stored in
49 * <code>true</code>, if a user is signed in, <code>false</code> otherwise.
51 public static boolean userSignedIn()
53 boolean signedIn = CURRENT_USER.get() != null;
54 LOG.debug("user signed in: {}", signedIn);
59 * Removes the ID of the current user from the thread-local.
61 public static void remove()
63 LOG.debug("removing current user");
64 CURRENT_USER.remove();