Released version 1.0.0
[examples/facebook-app] / src / main / java / de / juplo / yourshouter / SpringSecurityContextUserIdSource.java
1 package de.juplo.yourshouter;
2
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;
8
9
10 /**
11  * Implementation of {@link UserIdSource}, that retrieves the ID of the current
12  * user from the {@link SecurityContext}.
13  *
14  * @author Kai Moritz
15  */
16 public class SpringSecurityContextUserIdSource implements UserIdSource
17 {
18   /**
19    * Retrieves the ID of the current user from the {@link SecurityContext}.
20    * If no ID is found, an exception is thrown.
21    *
22    * @return The ID of the current user
23    * @throws IllegalStateException, if no current user is found.
24    */
25   @Override
26   public String getUserId()
27   {
28     SecurityContext context = SecurityContextHolder.getContext();
29     Authentication authentication = context.getAuthentication();
30     Assert.state(authentication != null, "No user signed in!");
31     return authentication.getName();
32   }
33 }