1 package de.juplo.yourshouter;
3 import org.springframework.security.core.Authentication;
4 import org.springframework.security.core.context.SecurityContext;
5 import org.springframework.security.core.context.SecurityContextHolder;
6 import org.springframework.social.UserIdSource;
7 import org.springframework.util.Assert;
11 * Implementation of {@link UserIdSource}, that retrieves the ID of the current
12 * user from the {@link SecurityContext}.
16 public class SpringSecurityContextUserIdSource implements UserIdSource
19 * Retrieves the ID of the current user from the {@link SecurityContext}.
20 * If no ID is found, an exception is thrown.
22 * @return The ID of the current user
23 * @throws IllegalStateException, if no current user is found.
26 public String getUserId()
28 SecurityContext context = SecurityContextHolder.getContext();
29 Authentication authentication = context.getAuthentication();
30 Assert.state(authentication != null, "No user signed in!");
31 return authentication.getName();