View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.shiro.guice.aop;
20  
21  import com.google.inject.AbstractModule;
22  import com.google.inject.matcher.AbstractMatcher;
23  import com.google.inject.matcher.Matchers;
24  import org.apache.shiro.aop.AnnotationMethodInterceptor;
25  import org.apache.shiro.aop.AnnotationResolver;
26  import org.apache.shiro.aop.DefaultAnnotationResolver;
27  import org.apache.shiro.authz.aop.*;
28  
29  import java.lang.annotation.Annotation;
30  import java.lang.reflect.Method;
31  
32  /**
33   * Install this module to enable Shiro AOP functionality in Guice.  You may extend it to add your own Shiro
34   * interceptors, override the default ones, or provide a specific {@link org.apache.shiro.aop.AnnotationResolver}.
35   */
36  public class ShiroAopModule extends AbstractModule {
37      @Override
38      protected final void configure() {
39          AnnotationResolver resolver = createAnnotationResolver();
40          configureDefaultInterceptors(resolver);
41          configureInterceptors(resolver);
42      }
43  
44      protected final void bindShiroInterceptor(final AnnotationMethodInterceptor methodInterceptor) {
45          bindInterceptor(Matchers.any(), new AbstractMatcher<Method>() {
46              public boolean matches(Method method) {
47                  Class<? extends Annotation> annotation = methodInterceptor.getHandler().getAnnotationClass();
48                  return method.getAnnotation(annotation) != null
49                          || method.getDeclaringClass().getAnnotation(annotation) != null;
50              }
51          }, new AopAllianceMethodInterceptorAdapter(methodInterceptor));
52      }
53  
54      protected AnnotationResolver createAnnotationResolver() {
55          return new DefaultAnnotationResolver();
56      }
57  
58      protected void configureDefaultInterceptors(AnnotationResolver resolver) {
59          bindShiroInterceptor(new RoleAnnotationMethodInterceptor(resolver));
60          bindShiroInterceptor(new PermissionAnnotationMethodInterceptor(resolver));
61          bindShiroInterceptor(new AuthenticatedAnnotationMethodInterceptor(resolver));
62          bindShiroInterceptor(new UserAnnotationMethodInterceptor(resolver));
63          bindShiroInterceptor(new GuestAnnotationMethodInterceptor(resolver));
64      }
65  
66      protected void configureInterceptors(AnnotationResolver resolver) {
67  
68      }
69  }