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 | 4 | public class ShiroAopModule extends AbstractModule { |
37 | |
@Override |
38 | |
protected final void configure() { |
39 | 4 | AnnotationResolver resolver = createAnnotationResolver(); |
40 | 4 | configureDefaultInterceptors(resolver); |
41 | 4 | configureInterceptors(resolver); |
42 | 4 | } |
43 | |
|
44 | |
protected final void bindShiroInterceptor(final AnnotationMethodInterceptor methodInterceptor) { |
45 | 12 | bindInterceptor(Matchers.any(), new AbstractMatcher<Method>() { |
46 | |
public boolean matches(Method method) { |
47 | 24 | Class<? extends Annotation> annotation = methodInterceptor.getHandler().getAnnotationClass(); |
48 | 48 | return method.getAnnotation(annotation) != null |
49 | 12 | || method.getDeclaringClass().getAnnotation(annotation) != null; |
50 | |
} |
51 | |
}, new AopAllianceMethodInterceptorAdapter(methodInterceptor)); |
52 | 12 | } |
53 | |
|
54 | |
protected AnnotationResolver createAnnotationResolver() { |
55 | 2 | return new DefaultAnnotationResolver(); |
56 | |
} |
57 | |
|
58 | |
protected void configureDefaultInterceptors(AnnotationResolver resolver) { |
59 | 2 | bindShiroInterceptor(new RoleAnnotationMethodInterceptor(resolver)); |
60 | 2 | bindShiroInterceptor(new PermissionAnnotationMethodInterceptor(resolver)); |
61 | 2 | bindShiroInterceptor(new AuthenticatedAnnotationMethodInterceptor(resolver)); |
62 | 2 | bindShiroInterceptor(new UserAnnotationMethodInterceptor(resolver)); |
63 | 2 | bindShiroInterceptor(new GuestAnnotationMethodInterceptor(resolver)); |
64 | 2 | } |
65 | |
|
66 | |
protected void configureInterceptors(AnnotationResolver resolver) { |
67 | |
|
68 | 0 | } |
69 | |
} |