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     */
019    package org.apache.shiro.spring.security.interceptor;
020    
021    import java.lang.reflect.Method;
022    
023    import org.springframework.aop.support.StaticMethodMatcherPointcutAdvisor;
024    import org.springframework.beans.factory.InitializingBean;
025    
026    import org.slf4j.Logger;
027    import org.slf4j.LoggerFactory;
028    
029    import org.apache.shiro.authz.annotation.RequiresAuthentication;
030    import org.apache.shiro.authz.annotation.RequiresGuest;
031    import org.apache.shiro.authz.annotation.RequiresPermissions;
032    import org.apache.shiro.authz.annotation.RequiresRoles;
033    import org.apache.shiro.authz.annotation.RequiresUser;
034    import org.apache.shiro.mgt.SecurityManager;
035    
036    
037    /**
038     * TODO - complete JavaDoc
039     * @author Les Hazlewood
040     * @since 0.1
041     */
042    public class AuthorizationAttributeSourceAdvisor extends StaticMethodMatcherPointcutAdvisor
043            implements InitializingBean {
044    
045        private static final Logger log = LoggerFactory.getLogger(AuthorizationAttributeSourceAdvisor.class);
046    
047        protected SecurityManager securityManager = null;
048    
049        /**
050         * Create a new AuthorizationAttributeSourceAdvisor.
051         */
052        public AuthorizationAttributeSourceAdvisor() {
053        }
054    
055        public SecurityManager getSecurityManager() {
056            return securityManager;
057        }
058    
059        public void setSecurityManager(org.apache.shiro.mgt.SecurityManager securityManager) {
060            this.securityManager = securityManager;
061        }
062    
063        /**
064         * Returns <tt>true</tt> if the method has any Shiro annotations, false otherwise.
065         * The annotations inspected are:
066         * <ul>
067         * <li>{@link org.apache.shiro.authz.annotation.RequiresAuthentication RequiresAuthentication}</li>
068         * <li>{@link org.apache.shiro.authz.annotation.RequiresUser RequiresUser}</li>
069         * <li>{@link org.apache.shiro.authz.annotation.RequiresGuest RequiresGuest}</li>
070         * <li>{@link org.apache.shiro.authz.annotation.RequiresRoles RequiresRoles}</li>
071         * <li>{@link org.apache.shiro.authz.annotation.RequiresPermissions RequiresPermissions}</li>
072         * </ul>
073         *
074         * @param method      the method to check for a Shiro annotation
075         * @param targetClass the class potentially declaring Shiro annotations
076         * @return <tt>true</tt> if the method has a Shiro annotation, false otherwise.
077         * @see org.springframework.aop.MethodMatcher#matches(java.lang.reflect.Method, Class)
078         */
079        public boolean matches(Method method, Class targetClass) {
080            return ((method.getAnnotation(RequiresPermissions.class) != null) ||
081                    (method.getAnnotation(RequiresRoles.class) != null) ||
082                    (method.getAnnotation(RequiresUser.class) != null) ||
083                    (method.getAnnotation(RequiresGuest.class) != null ) ||
084                    (method.getAnnotation(RequiresAuthentication.class) != null ));
085        }
086    
087        public void afterPropertiesSet() throws Exception {
088            if (getAdvice() == null) {
089                if (log.isTraceEnabled()) {
090                    log.trace("No authorization advice explicitly configured via the 'advice' " +
091                            "property.  Attempting to set " +
092                            "default instance of type [" +
093                            AopAllianceAnnotationsAuthorizingMethodInterceptor.class.getName() + "]");
094                }
095                AopAllianceAnnotationsAuthorizingMethodInterceptor interceptor =
096                        new AopAllianceAnnotationsAuthorizingMethodInterceptor();
097                setAdvice(interceptor);
098            }
099        }
100    }