1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.shiro.mgt;
20
21 import org.apache.shiro.SecurityUtils;
22 import org.apache.shiro.authc.AuthenticationToken;
23 import org.apache.shiro.authc.UsernamePasswordToken;
24 import org.apache.shiro.config.Ini;
25 import org.apache.shiro.realm.text.IniRealm;
26 import org.apache.shiro.subject.Subject;
27 import org.apache.shiro.util.ThreadContext;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31
32 import static org.junit.Assert.assertTrue;
33
34
35
36
37
38 public class VMSingletonDefaultSecurityManagerTest {
39
40 @Before
41 public void setUp() {
42 ThreadContext.remove();
43 }
44
45 @After
46 public void tearDown() {
47 ThreadContext.remove();
48 }
49
50 @Test
51 public void testVMSingleton() {
52 DefaultSecurityManager sm = new DefaultSecurityManager();
53 Ini ini = new Ini();
54 Ini.Section section = ini.addSection(IniRealm.USERS_SECTION_NAME);
55 section.put("guest", "guest");
56 sm.setRealm(new IniRealm(ini));
57 SecurityUtils.setSecurityManager(sm);
58
59 try {
60 Subject subject = SecurityUtils.getSubject();
61
62 AuthenticationToken token = new UsernamePasswordToken("guest", "guest");
63 subject.login(token);
64 subject.getSession().setAttribute("key", "value");
65 assertTrue(subject.getSession().getAttribute("key").equals("value"));
66
67 subject = SecurityUtils.getSubject();
68
69 assertTrue(subject.isAuthenticated());
70 assertTrue(subject.getSession().getAttribute("key").equals("value"));
71 } finally {
72 sm.destroy();
73
74 SecurityUtils.setSecurityManager(null);
75 }
76 }
77 }