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.guice.aop;
020    
021    import com.google.inject.AbstractModule;
022    import com.google.inject.matcher.AbstractMatcher;
023    import com.google.inject.matcher.Matchers;
024    import org.apache.shiro.aop.AnnotationMethodInterceptor;
025    import org.apache.shiro.aop.AnnotationResolver;
026    import org.apache.shiro.aop.DefaultAnnotationResolver;
027    import org.apache.shiro.authz.aop.*;
028    
029    import java.lang.annotation.Annotation;
030    import java.lang.reflect.Method;
031    
032    /**
033     * Install this module to enable Shiro AOP functionality in Guice.  You may extend it to add your own Shiro
034     * interceptors, override the default ones, or provide a specific {@link org.apache.shiro.aop.AnnotationResolver}.
035     */
036    public class ShiroAopModule extends AbstractModule {
037        @Override
038        protected final void configure() {
039            AnnotationResolver resolver = createAnnotationResolver();
040            configureDefaultInterceptors(resolver);
041            configureInterceptors(resolver);
042        }
043    
044        protected final void bindShiroInterceptor(final AnnotationMethodInterceptor methodInterceptor) {
045            bindInterceptor(Matchers.any(), new AbstractMatcher<Method>() {
046                public boolean matches(Method method) {
047                    Class<? extends Annotation> annotation = methodInterceptor.getHandler().getAnnotationClass();
048                    return method.getAnnotation(annotation) != null
049                            || method.getDeclaringClass().getAnnotation(annotation) != null;
050                }
051            }, new AopAllianceMethodInterceptorAdapter(methodInterceptor));
052        }
053    
054        protected AnnotationResolver createAnnotationResolver() {
055            return new DefaultAnnotationResolver();
056        }
057    
058        protected void configureDefaultInterceptors(AnnotationResolver resolver) {
059            bindShiroInterceptor(new RoleAnnotationMethodInterceptor(resolver));
060            bindShiroInterceptor(new PermissionAnnotationMethodInterceptor(resolver));
061            bindShiroInterceptor(new AuthenticatedAnnotationMethodInterceptor(resolver));
062            bindShiroInterceptor(new UserAnnotationMethodInterceptor(resolver));
063            bindShiroInterceptor(new GuestAnnotationMethodInterceptor(resolver));
064        }
065    
066        protected void configureInterceptors(AnnotationResolver resolver) {
067    
068        }
069    }