--- /dev/null
+package de.juplo.yourshouter;
+
+import org.springframework.security.core.Authentication;
+import org.springframework.security.core.context.SecurityContext;
+import org.springframework.security.core.context.SecurityContextHolder;
+import org.springframework.social.UserIdSource;
+import org.springframework.util.Assert;
+
+
+/**
+ * Implementation of {@link UserIdSource}, that retrieves the ID of the current
+ * user from the {@link SecurityContext}.
+ *
+ * @author Kai Moritz
+ */
+public class SpringSecurityContextUserIdSource implements UserIdSource
+{
+ /**
+ * Retrieves the ID of the current user from the {@link SecurityContext}.
+ * If no ID is found, an exception is thrown.
+ *
+ * @return The ID of the current user
+ * @throws IllegalStateException, if no current user is found.
+ */
+ @Override
+ public String getUserId()
+ {
+ SecurityContext context = SecurityContextHolder.getContext();
+ Authentication authentication = context.getAuthentication();
+ Assert.state(authentication != null, "No user signed in!");
+ return authentication.getName();
+ }
+}