1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.spring.security.interceptor;
20
21 import org.apache.shiro.authz.UnauthenticatedException;
22 import org.apache.shiro.realm.Realm;
23 import org.apache.shiro.subject.PrincipalCollection;
24 import org.apache.shiro.subject.SimplePrincipalCollection;
25 import org.apache.shiro.subject.Subject;
26 import org.apache.shiro.subject.support.SubjectThreadState;
27 import org.apache.shiro.util.ThreadState;
28 import org.junit.After;
29 import org.junit.Test;
30 import org.junit.runner.RunWith;
31 import org.springframework.beans.factory.annotation.Autowired;
32 import org.springframework.test.context.ContextConfiguration;
33 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
34
35
36
37
38
39
40
41
42 @RunWith(SpringJUnit4ClassRunner.class)
43 @ContextConfiguration
44 public abstract class AbstractAuthorizationAnnotationTest {
45
46 @Autowired
47 protected TestService testService;
48 @Autowired
49 private org.apache.shiro.mgt.SecurityManager securityManager;
50 @Autowired
51 private Realm realm;
52
53 private ThreadState threadState;
54
55 protected void bind(Subject subject) {
56 clearSubject();
57 this.threadState = new SubjectThreadState(subject);
58 this.threadState.bind();
59 }
60
61 @After
62 public void clearSubject() {
63 if (threadState != null) {
64 threadState.clear();
65 }
66 }
67
68 protected void bindGuest() {
69 bind(new Subject.Builder(securityManager).buildSubject());
70 }
71
72 protected void bindUser() {
73 PrincipalCollection principals = new SimplePrincipalCollection("test", realm.getName());
74 bind(new Subject.Builder(securityManager).principals(principals).buildSubject());
75 }
76
77 protected void bindAuthenticatedUser() {
78 PrincipalCollection principals = new SimplePrincipalCollection("test", realm.getName());
79 bind(new Subject.Builder(securityManager).
80 principals(principals).authenticated(true).buildSubject());
81 }
82
83
84
85 @Test
86 public void testGuestImplementation() {
87 bindGuest();
88 testService.guestImplementation();
89 }
90
91 @Test(expected = UnauthenticatedException.class)
92 public void testGuestImplementationFailure() {
93 bindUser();
94 testService.guestImplementation();
95 }
96
97 @Test
98 public void testGuestInterface() {
99 bindGuest();
100 testService.guestInterface();
101 }
102
103
104
105
106
107
108
109
110 @Test
111 public void testUserImplementation() {
112 bindUser();
113 testService.userImplementation();
114 }
115
116 @Test(expected = UnauthenticatedException.class)
117 public void testUserImplementationFailure() {
118 bindGuest();
119 testService.userImplementation();
120 }
121
122 @Test
123 public void testUserInterface() {
124 bindUser();
125 testService.userInterface();
126 }
127
128
129
130
131
132
133
134
135 @Test
136 public void testAuthenticatedImplementation() {
137 bindAuthenticatedUser();
138 testService.authenticatedImplementation();
139 }
140
141 @Test(expected = UnauthenticatedException.class)
142 public void testAuthenticatedImplementationFailure() {
143 bindUser();
144 testService.authenticatedImplementation();
145 }
146
147 @Test
148 public void testAuthenticatedInterface() {
149 bindAuthenticatedUser();
150 testService.authenticatedInterface();
151 }
152
153
154
155
156 }