--- /dev/null
+package de.juplo.yourshouter;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.social.UserIdSource;
+
+
+/**
+ * Implementation of {@link UserIdSource}, that retrieves the ID of the current
+ * user from the {@link SecurityContext}.
+ *
+ * @author Kai Moritz
+ */
+public class SecurityContextUserIdSource implements UserIdSource
+{
+ private final static Logger LOG =
+ LoggerFactory.getLogger(SecurityContextUserIdSource.class);
+
+
+ /**
+ * Retrieves the ID of the current user from the {@link SecurityContext}.
+ *
+ * @return
+ * The ID of the current user, or the special ID <code>anonymous</code>,
+ * if no current user is present.
+ */
+ @Override
+ public String getUserId()
+ {
+ String user = SecurityContext.getCurrentUser();
+ if (user != null)
+ {
+ LOG.debug("found user \"{}\" in the security-context", user);
+ }
+ else
+ {
+ LOG.info("found no user in the security-context, using \"anonymous\"");
+ user = "anonymous";
+ }
+ return user;
+ }
+}