Current user based on pure Spring Security
authorMathieu Baudier <mbaudier@argeo.org>
Sat, 14 Feb 2015 16:55:56 +0000 (16:55 +0000)
committerMathieu Baudier <mbaudier@argeo.org>
Sat, 14 Feb 2015 16:55:56 +0000 (16:55 +0000)
git-svn-id: https://svn.argeo.org/commons/trunk@7873 4cfe0d0a-d680-48aa-b62c-e0a02a3f76cc

org.argeo.security.ui/src/org/argeo/security/ui/internal/CurrentUser.java

index 944bc2321e8e9387fbda0366e44361c29dc32ff6..b7287f66c97efe7e19ca3765c6a7e4540dbae558 100644 (file)
  */
 package org.argeo.security.ui.internal;
 
-import java.security.AccessController;
-import java.security.Principal;
 import java.util.Collections;
 import java.util.HashSet;
 import java.util.Set;
 
-import javax.security.auth.Subject;
-
-import org.argeo.ArgeoException;
 import org.springframework.security.core.Authentication;
 import org.springframework.security.core.GrantedAuthority;
+import org.springframework.security.core.context.SecurityContextHolder;
 
 /**
  * Retrieves information about the current user. Not an API, can change without
  * notice.
  */
 public class CurrentUser {
-       public final static String getUsername() {
-               Subject subject = getSubject();
-               if (subject == null)
-                       return null;
-               Principal principal = subject.getPrincipals().iterator().next();
-               return principal.getName();
+       // public final static String getUsername() {
+       // Subject subject = getSubject();
+       // if (subject == null)
+       // return null;
+       // Principal principal = subject.getPrincipals().iterator().next();
+       // return principal.getName();
+       //
+       // }
 
+       public final static String getUsername() {
+               return getAuthentication().getName();
        }
 
        public final static Set<String> roles() {
@@ -51,20 +51,24 @@ public class CurrentUser {
        }
 
        public final static Authentication getAuthentication() {
-               Set<Authentication> authens = getSubject().getPrincipals(
-                               Authentication.class);
-               if (authens != null && !authens.isEmpty()) {
-                       Principal principal = authens.iterator().next();
-                       Authentication authentication = (Authentication) principal;
-                       return authentication;
-               }
-               throw new ArgeoException("No authentication found");
+               return SecurityContextHolder.getContext().getAuthentication();
        }
 
-       public final static Subject getSubject() {
-               Subject subject = Subject.getSubject(AccessController.getContext());
-               if (subject == null)
-                       throw new ArgeoException("Not authenticated.");
-               return subject;
-       }
+       // public final static Authentication getAuthentication() {
+       // Set<Authentication> authens = getSubject().getPrincipals(
+       // Authentication.class);
+       // if (authens != null && !authens.isEmpty()) {
+       // Principal principal = authens.iterator().next();
+       // Authentication authentication = (Authentication) principal;
+       // return authentication;
+       // }
+       // throw new ArgeoException("No authentication found");
+       // }
+
+       // public final static Subject getSubject() {
+       // Subject subject = Subject.getSubject(AccessController.getContext());
+       // if (subject == null)
+       // throw new ArgeoException("Not authenticated.");
+       // return subject;
+       // }
 }