1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
34
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 }