瀏覽代碼

Implemented a fix for a NullPointerException as reported by Pierre-Antoine Gr�goire (pa.gregoire@free.fr)
"The error comes from line 115 in AuthorizeTag....It seems there's no control
for a null value here..."

* test/net/sf/acegisecurity/taglibs/authz/AuthorizeTagTests.java:
Added a new test to confirm the existence of the bug.

* src/net/sf/acegisecurity/taglibs/authz/AuthorizeTag.java:
And fixed the failing test.

Francois Beausoleil 21 年之前
父節點
當前提交
d5a6ea044d

+ 4 - 0
core/src/main/java/org/acegisecurity/taglibs/authz/AuthorizeTag.java

@@ -112,6 +112,10 @@ public class AuthorizeTag extends TagSupport {
 
         Authentication currentUser = context.getAuthentication();
 
+        if (null == currentUser) {
+            return Collections.EMPTY_LIST;
+        }
+
         Collection granted = Arrays.asList(currentUser.getAuthorities());
 
         return granted;

+ 9 - 0
core/src/test/java/org/acegisecurity/taglibs/authz/AuthorizeTagTests.java

@@ -42,6 +42,15 @@ public class AuthorizeTagTests extends TestCase {
 
     //~ Methods ================================================================
 
+    public void testAlwaysReturnsUnauthorizedIfNoUserFound()
+        throws JspException {
+        context.setAuthentication(null);
+
+        authorizeTag.setIfAllGranted("ROLE_TELLER");
+        assertEquals("prevents request - no principal in Context",
+            Tag.SKIP_BODY, authorizeTag.doStartTag());
+    }
+
     public void testDefaultsToNotOutputtingBodyWhenNoRequiredAuthorities()
         throws JspException {
         assertEquals("", authorizeTag.getIfAllGranted());