package de.juplo.yourshouter;
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
import org.springframework.social.UserIdSource;
+import org.springframework.util.Assert;
/**
*/
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}.
+ * If no ID is found, an exception is thrown.
*
- * @return
- * The ID of the current user, or the special ID <code>anonymous</code>,
- * if no current user is present.
+ * @return The ID of the current user
+ * @throws IllegalStateException, if no current user is found.
*/
@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;
+ Assert.state(SecurityContext.userSignedIn(), "No user signed in!");
+ return SecurityContext.getCurrentUser();
}
}