You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@shiro.apache.org by bd...@apache.org on 2017/01/06 19:43:22 UTC

[1/2] shiro git commit: unit test against AuthorizationAttributeSourceAdvisor.matches

Repository: shiro
Updated Branches:
  refs/heads/master 85edb748e -> 0aec37340


unit test against AuthorizationAttributeSourceAdvisor.matches

Signed-off-by: Laszlo Hornyak <la...@gmail.com>


Project: http://git-wip-us.apache.org/repos/asf/shiro/repo
Commit: http://git-wip-us.apache.org/repos/asf/shiro/commit/98810db1
Tree: http://git-wip-us.apache.org/repos/asf/shiro/tree/98810db1
Diff: http://git-wip-us.apache.org/repos/asf/shiro/diff/98810db1

Branch: refs/heads/master
Commit: 98810db1514d515cdf9409bb951140dd38bc4cf2
Parents: 85edb74
Author: Laszlo Hornyak <la...@gmail.com>
Authored: Thu Dec 29 22:44:08 2016 +0100
Committer: Laszlo Hornyak <la...@gmail.com>
Committed: Thu Dec 29 22:44:08 2016 +0100

----------------------------------------------------------------------
 ...AuthorizationAttributeSourceAdvisorTest.java | 84 ++++++++++++++++++++
 1 file changed, 84 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/shiro/blob/98810db1/support/spring/src/test/java/org/apache/shiro/spring/security/interceptor/AuthorizationAttributeSourceAdvisorTest.java
----------------------------------------------------------------------
diff --git a/support/spring/src/test/java/org/apache/shiro/spring/security/interceptor/AuthorizationAttributeSourceAdvisorTest.java b/support/spring/src/test/java/org/apache/shiro/spring/security/interceptor/AuthorizationAttributeSourceAdvisorTest.java
new file mode 100644
index 0000000..7c33d2f
--- /dev/null
+++ b/support/spring/src/test/java/org/apache/shiro/spring/security/interceptor/AuthorizationAttributeSourceAdvisorTest.java
@@ -0,0 +1,84 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.shiro.spring.security.interceptor;
+
+import org.apache.shiro.authz.annotation.RequiresAuthentication;
+import org.junit.Test;
+
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class AuthorizationAttributeSourceAdvisorTest {
+
+    static class Secured {
+        @RequiresAuthentication
+        public void secureMethod() {
+        }
+
+        public void unsecuredMethod() {
+        }
+    }
+
+    interface ServiceInterface {
+        @RequiresAuthentication
+        String secureMethod();
+
+        String unsecuredMethod();
+    }
+
+    static class ServiceImpl implements ServiceInterface {
+
+        @Override
+        public String secureMethod() {
+            return "";
+        }
+
+        @Override
+        public String unsecuredMethod() {
+            return "";
+        }
+    }
+
+    @Test
+    public void matches() throws NoSuchMethodException {
+        assertTrue(
+                "the method is annotated, should match",
+                new AuthorizationAttributeSourceAdvisor().matches(
+                        Secured.class.getDeclaredMethod("secureMethod"), Secured.class
+                ));
+        assertFalse(
+                "the method is not annotated, should not match",
+                new AuthorizationAttributeSourceAdvisor().matches(
+                        Secured.class.getDeclaredMethod("unsecuredMethod"), Secured.class
+                ));
+
+        assertTrue(
+                "the method declaration is annotated in the interface, should match",
+                new AuthorizationAttributeSourceAdvisor().matches(
+                        ServiceInterface.class.getDeclaredMethod("secureMethod"), ServiceImpl.class
+                ));
+        assertFalse(
+                "not annotated method, should not match",
+                new AuthorizationAttributeSourceAdvisor().matches(
+                        ServiceInterface.class.getDeclaredMethod("unsecuredMethod"), ServiceImpl.class
+                ));
+
+    }
+
+}


[2/2] shiro git commit: SHIRO-607: find the annotations on the types as well

Posted by bd...@apache.org.
SHIRO-607: find the annotations on the types as well

Fixes: #54

Signed-off-by: Laszlo Hornyak <la...@gmail.com>
Signed-off-by: Brian Demers <bd...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/shiro/repo
Commit: http://git-wip-us.apache.org/repos/asf/shiro/commit/0aec3734
Tree: http://git-wip-us.apache.org/repos/asf/shiro/tree/0aec3734
Diff: http://git-wip-us.apache.org/repos/asf/shiro/diff/0aec3734

Branch: refs/heads/master
Commit: 0aec373400bd5306d75537c4a2edd31954bcd1d4
Parents: 98810db
Author: Laszlo Hornyak <la...@gmail.com>
Authored: Fri Dec 30 21:15:03 2016 +0100
Committer: Brian Demers <bd...@apache.org>
Committed: Fri Jan 6 14:42:16 2017 -0500

----------------------------------------------------------------------
 .../AuthorizationAttributeSourceAdvisor.java    | 16 +++++++++----
 ...AuthorizationAttributeSourceAdvisorTest.java | 24 ++++++++++++++++++++
 2 files changed, 36 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/shiro/blob/0aec3734/support/spring/src/main/java/org/apache/shiro/spring/security/interceptor/AuthorizationAttributeSourceAdvisor.java
----------------------------------------------------------------------
diff --git a/support/spring/src/main/java/org/apache/shiro/spring/security/interceptor/AuthorizationAttributeSourceAdvisor.java b/support/spring/src/main/java/org/apache/shiro/spring/security/interceptor/AuthorizationAttributeSourceAdvisor.java
index b9deb2b..45fe713 100644
--- a/support/spring/src/main/java/org/apache/shiro/spring/security/interceptor/AuthorizationAttributeSourceAdvisor.java
+++ b/support/spring/src/main/java/org/apache/shiro/spring/security/interceptor/AuthorizationAttributeSourceAdvisor.java
@@ -63,7 +63,7 @@ public class AuthorizationAttributeSourceAdvisor extends StaticMethodMatcherPoin
     }
 
     /**
-     * Returns <tt>true</tt> if the method has any Shiro annotations, false otherwise.
+     * Returns <tt>true</tt> if the method or the class has any Shiro annotations, false otherwise.
      * The annotations inspected are:
      * <ul>
      * <li>{@link org.apache.shiro.authz.annotation.RequiresAuthentication RequiresAuthentication}</li>
@@ -90,9 +90,7 @@ public class AuthorizationAttributeSourceAdvisor extends StaticMethodMatcherPoin
         if ( targetClass != null) {
             try {
                 m = targetClass.getMethod(m.getName(), m.getParameterTypes());
-                if ( isAuthzAnnotationPresent(m) ) {
-                    return true;
-                }
+                return isAuthzAnnotationPresent(m) || isAuthzAnnotationPresent(targetClass);
             } catch (NoSuchMethodException ignored) {
                 //default return value is false.  If we can't find the method, then obviously
                 //there is no annotation, so just use the default return value.
@@ -102,6 +100,16 @@ public class AuthorizationAttributeSourceAdvisor extends StaticMethodMatcherPoin
         return false;
     }
 
+    private boolean isAuthzAnnotationPresent(Class<?> targetClazz) {
+        for( Class<? extends Annotation> annClass : AUTHZ_ANNOTATION_CLASSES ) {
+            Annotation a = AnnotationUtils.findAnnotation(targetClazz, annClass);
+            if ( a != null ) {
+                return true;
+            }
+        }
+        return false;
+    }
+
     private boolean isAuthzAnnotationPresent(Method method) {
         for( Class<? extends Annotation> annClass : AUTHZ_ANNOTATION_CLASSES ) {
             Annotation a = AnnotationUtils.findAnnotation(method, annClass);

http://git-wip-us.apache.org/repos/asf/shiro/blob/0aec3734/support/spring/src/test/java/org/apache/shiro/spring/security/interceptor/AuthorizationAttributeSourceAdvisorTest.java
----------------------------------------------------------------------
diff --git a/support/spring/src/test/java/org/apache/shiro/spring/security/interceptor/AuthorizationAttributeSourceAdvisorTest.java b/support/spring/src/test/java/org/apache/shiro/spring/security/interceptor/AuthorizationAttributeSourceAdvisorTest.java
index 7c33d2f..5294f02 100644
--- a/support/spring/src/test/java/org/apache/shiro/spring/security/interceptor/AuthorizationAttributeSourceAdvisorTest.java
+++ b/support/spring/src/test/java/org/apache/shiro/spring/security/interceptor/AuthorizationAttributeSourceAdvisorTest.java
@@ -55,6 +55,19 @@ public class AuthorizationAttributeSourceAdvisorTest {
         }
     }
 
+    @RequiresAuthentication
+    interface SafeServiceInterface {
+        String someMethod();
+    }
+
+    static class SafeServiceImpl implements SafeServiceInterface {
+
+        @Override
+        public String someMethod() {
+            return "";
+        }
+    }
+
     @Test
     public void matches() throws NoSuchMethodException {
         assertTrue(
@@ -79,6 +92,17 @@ public class AuthorizationAttributeSourceAdvisorTest {
                         ServiceInterface.class.getDeclaredMethod("unsecuredMethod"), ServiceImpl.class
                 ));
 
+        assertTrue(
+                "the method declaration is in the interface with type-annotation, should match",
+                new AuthorizationAttributeSourceAdvisor().matches(
+                        SafeServiceInterface.class.getDeclaredMethod("someMethod"), SafeServiceInterface.class
+                ));
+        assertTrue(
+                "the method declaration is in the interface with type-annotation, should match",
+                new AuthorizationAttributeSourceAdvisor().matches(
+                        SafeServiceImpl.class.getDeclaredMethod("someMethod"), SafeServiceImpl.class
+                ));
+
     }
 
 }