001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, 013 * software distributed under the License is distributed on an 014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 015 * KIND, either express or implied. See the License for the 016 * specific language governing permissions and limitations 017 * under the License. 018 */ 019package org.apache.shiro.authz.aop; 020 021import java.util.ArrayList; 022import java.util.Collection; 023 024import org.apache.shiro.aop.MethodInvocation; 025import org.apache.shiro.authz.AuthorizationException; 026 027/** 028 * An <tt>AnnotationsAuthorizingMethodInterceptor</tt> is a MethodInterceptor that asserts a given method is authorized 029 * to execute based on one or more configured <tt>AuthorizingAnnotationMethodInterceptor</tt>s. 030 * 031 * <p>This allows multiple annotations on a method to be processed before the method 032 * executes, and if any of the <tt>AuthorizingAnnotationMethodInterceptor</tt>s indicate that the method should not be 033 * executed, an <tt>AuthorizationException</tt> will be thrown, otherwise the method will be invoked as expected. 034 * 035 * <p>It is essentially a convenience mechanism to allow multiple annotations to be processed in a single method 036 * interceptor. 037 * 038 * @since 0.2 039 */ 040public abstract class AnnotationsAuthorizingMethodInterceptor extends AuthorizingMethodInterceptor { 041 042 /** 043 * The method interceptors to execute for the annotated method. 044 */ 045 protected Collection<AuthorizingAnnotationMethodInterceptor> methodInterceptors; 046 047 /** 048 * Default no-argument constructor that defaults the 049 * {@link #methodInterceptors methodInterceptors} attribute to contain two interceptors by default - the 050 * {@link RoleAnnotationMethodInterceptor RoleAnnotationMethodInterceptor} and the 051 * {@link PermissionAnnotationMethodInterceptor PermissionAnnotationMethodInterceptor} to 052 * support role and permission annotations. 053 */ 054 public AnnotationsAuthorizingMethodInterceptor() { 055 methodInterceptors = new ArrayList<AuthorizingAnnotationMethodInterceptor>(5); 056 methodInterceptors.add(new RoleAnnotationMethodInterceptor()); 057 methodInterceptors.add(new PermissionAnnotationMethodInterceptor()); 058 methodInterceptors.add(new AuthenticatedAnnotationMethodInterceptor()); 059 methodInterceptors.add(new UserAnnotationMethodInterceptor()); 060 methodInterceptors.add(new GuestAnnotationMethodInterceptor()); 061 } 062 063 /** 064 * Returns the method interceptors to execute for the annotated method. 065 * <p/> 066 * Unless overridden by the {@link #setMethodInterceptors(java.util.Collection)} method, the default collection 067 * contains a 068 * {@link RoleAnnotationMethodInterceptor RoleAnnotationMethodInterceptor} and a 069 * {@link PermissionAnnotationMethodInterceptor PermissionAnnotationMethodInterceptor} to 070 * support role and permission annotations automatically. 071 * 072 * @return the method interceptors to execute for the annotated method. 073 */ 074 public Collection<AuthorizingAnnotationMethodInterceptor> getMethodInterceptors() { 075 return methodInterceptors; 076 } 077 078 /** 079 * Sets the method interceptors to execute for the annotated method. 080 * 081 * @param methodInterceptors the method interceptors to execute for the annotated method. 082 * @see #getMethodInterceptors() 083 */ 084 public void setMethodInterceptors(Collection<AuthorizingAnnotationMethodInterceptor> methodInterceptors) { 085 this.methodInterceptors = methodInterceptors; 086 } 087 088 /** 089 * Iterates over the internal {@link #getMethodInterceptors() methodInterceptors} collection, and for each one, 090 * ensures that if the interceptor 091 * {@link AuthorizingAnnotationMethodInterceptor#supports(org.apache.shiro.aop.MethodInvocation) supports} 092 * the invocation, that the interceptor 093 * {@link AuthorizingAnnotationMethodInterceptor#assertAuthorized(org.apache.shiro.aop.MethodInvocation) asserts} 094 * that the invocation is authorized to proceed. 095 */ 096 protected void assertAuthorized(MethodInvocation methodInvocation) throws AuthorizationException { 097 //default implementation just ensures no deny votes are cast: 098 Collection<AuthorizingAnnotationMethodInterceptor> aamis = getMethodInterceptors(); 099 if (aamis != null && !aamis.isEmpty()) { 100 for (AuthorizingAnnotationMethodInterceptor aami : aamis) { 101 if (aami.supports(methodInvocation)) { 102 aami.assertAuthorized(methodInvocation); 103 } 104 } 105 } 106 } 107}