Quellcode durchsuchen

Support getUserPrincipal().

Ben Alex vor 20 Jahren
Ursprung
Commit
834f69168d

+ 19 - 1
core/src/main/java/org/acegisecurity/ui/wrapper/ContextHolderAwareRequestWrapper.java

@@ -1,4 +1,4 @@
-/* Copyright 2004 Acegi Technology Pty Limited
+/* Copyright 2004, 2005 Acegi Technology Pty Limited
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,6 +21,8 @@ import net.sf.acegisecurity.UserDetails;
 import net.sf.acegisecurity.context.ContextHolder;
 import net.sf.acegisecurity.context.SecureContext;
 
+import java.security.Principal;
+
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletRequestWrapper;
 
@@ -88,6 +90,22 @@ public class ContextHolderAwareRequestWrapper extends HttpServletRequestWrapper
         return isGranted(role);
     }
 
+    /**
+     * Returns the <code>Authentication</code> (which is a subclass of
+     * <code>Principal</code>), or <code>null</code> if unavailable.
+     *
+     * @return the <code>Authentication</code>, or <code>null</code>
+     */
+    public Principal getUserPrincipal() {
+        Authentication auth = getAuthentication();
+
+        if ((auth == null) || (auth.getPrincipal() == null)) {
+            return null;
+        }
+
+        return auth;
+    }
+
     private Authentication getAuthentication() {
         if ((ContextHolder.getContext() != null)
             && ContextHolder.getContext() instanceof SecureContext) {

+ 6 - 1
core/src/test/java/org/acegisecurity/ui/wrapper/ContextHolderAwareRequestWrapperTests.java

@@ -1,4 +1,4 @@
-/* Copyright 2004 Acegi Technology Pty Limited
+/* Copyright 2004, 2005 Acegi Technology Pty Limited
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -70,6 +70,7 @@ public class ContextHolderAwareRequestWrapperTests extends TestCase {
         assertEquals("marissa", wrapper.getRemoteUser());
         assertTrue(wrapper.isUserInRole("ROLE_FOO"));
         assertFalse(wrapper.isUserInRole("ROLE_NOT_GRANTED"));
+        assertEquals(auth, wrapper.getUserPrincipal());
 
         ContextHolder.setContext(null);
     }
@@ -93,6 +94,7 @@ public class ContextHolderAwareRequestWrapperTests extends TestCase {
         assertFalse(wrapper.isUserInRole("ROLE_NOT_GRANTED"));
         assertTrue(wrapper.isUserInRole("ROLE_FOOBAR"));
         assertTrue(wrapper.isUserInRole("ROLE_HELLO"));
+        assertEquals(auth, wrapper.getUserPrincipal());
 
         ContextHolder.setContext(null);
     }
@@ -106,6 +108,7 @@ public class ContextHolderAwareRequestWrapperTests extends TestCase {
                     "/"));
         assertNull(wrapper.getRemoteUser());
         assertFalse(wrapper.isUserInRole("ROLE_ANY"));
+        assertNull(wrapper.getUserPrincipal());
 
         ContextHolder.setContext(null);
     }
@@ -117,6 +120,7 @@ public class ContextHolderAwareRequestWrapperTests extends TestCase {
                     "/"));
         assertNull(wrapper.getRemoteUser());
         assertFalse(wrapper.isUserInRole("ROLE_ANY"));
+        assertNull(wrapper.getUserPrincipal());
     }
 
     public void testNullPrincipalHandling() throws Exception {
@@ -133,6 +137,7 @@ public class ContextHolderAwareRequestWrapperTests extends TestCase {
         assertNull(wrapper.getRemoteUser());
         assertFalse(wrapper.isUserInRole("ROLE_HELLO")); // principal is null, so reject
         assertFalse(wrapper.isUserInRole("ROLE_FOOBAR")); // principal is null, so reject
+        assertNull(wrapper.getUserPrincipal());
 
         ContextHolder.setContext(null);
     }

+ 1 - 0
doc/xdocs/changes.xml

@@ -31,6 +31,7 @@
       <action dev="benalex" type="update">FilterToBeanProxy now searches hierarchical bean factories</action>
       <action dev="benalex" type="update">Improved Tapestry support in AbstractProcessingFilter</action>
       <action dev="benalex" type="update">User now accepted blank passwords (null passwords still rejected)</action>
+      <action dev="benalex" type="update">ContextHolderAwareRequestWrapper now provides a getUserPrincipal() method</action>
       <action dev="benalex" type="fix">Contacts sample web.xml no longer expect Log4j to be in classpath</action>
       <action dev="raykrueger" type="update">JaasAuthenticatinProvider now uses System.property "java.security.auth.login.config"</action>
       <action dev="raykrueger" type="update">JaasAuthenticationCallbackHandler Authentication is passed to handle method setAuthenticatoin removed</action>