X-Git-Url: https://juplo.de/gitweb/?p=examples%2Ffacebook-app;a=blobdiff_plain;f=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fyourshouter%2FSpringSecurityContextUserIdSource.java;fp=src%2Fmain%2Fjava%2Fde%2Fjuplo%2Fyourshouter%2FSpringSecurityContextUserIdSource.java;h=d774060ca96822943aba65cc6158d625569cf103;hp=0000000000000000000000000000000000000000;hb=8f6d3c83aa9651e593b57b3d47cfd50a4ae73661;hpb=ca351a3eb7442fbb183aa62e1a58cd85bc1f2ef7 diff --git a/src/main/java/de/juplo/yourshouter/SpringSecurityContextUserIdSource.java b/src/main/java/de/juplo/yourshouter/SpringSecurityContextUserIdSource.java new file mode 100644 index 0000000..d774060 --- /dev/null +++ b/src/main/java/de/juplo/yourshouter/SpringSecurityContextUserIdSource.java @@ -0,0 +1,33 @@ +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(); + } +}