X-Git-Url: https://juplo.de/gitweb/?p=examples%2Ffacebook-app;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fyourshouter%2FSecurityContextUserIdSource.java;fp=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fyourshouter%2FSecurityContextUserIdSource.java;h=9fae3235dd5a95dd55e50979bf6a16fd3093b9d5;hp=0000000000000000000000000000000000000000;hb=02f599692669d48f9865764fda994ad61d203ffb;hpb=931f5c9b9a43acb81775e995a613bd0c5b8aab52 diff --git a/src/main/java/de/juplo/yourshouter/SecurityContextUserIdSource.java b/src/main/java/de/juplo/yourshouter/SecurityContextUserIdSource.java new file mode 100644 index 0000000..9fae323 --- /dev/null +++ b/src/main/java/de/juplo/yourshouter/SecurityContextUserIdSource.java @@ -0,0 +1,42 @@ +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 anonymous, + * 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; + } +}