1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
package org.apache.shiro.samples.spring.web; |
20 | |
|
21 | |
import org.apache.shiro.SecurityUtils; |
22 | |
import org.apache.shiro.samples.spring.SampleManager; |
23 | |
import org.apache.shiro.session.Session; |
24 | |
import org.apache.shiro.subject.Subject; |
25 | |
import org.springframework.validation.BindException; |
26 | |
import org.springframework.validation.Errors; |
27 | |
import org.springframework.web.servlet.ModelAndView; |
28 | |
import org.springframework.web.servlet.mvc.SimpleFormController; |
29 | |
|
30 | |
import javax.servlet.http.HttpServletRequest; |
31 | |
import javax.servlet.http.HttpServletResponse; |
32 | |
import java.util.HashMap; |
33 | |
import java.util.LinkedHashMap; |
34 | |
import java.util.Map; |
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | |
|
41 | |
|
42 | 0 | public class IndexController extends SimpleFormController { |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
private SampleManager sampleManager; |
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | |
|
61 | |
|
62 | |
public void setSampleManager(SampleManager sampleManager) { |
63 | 0 | this.sampleManager = sampleManager; |
64 | 0 | } |
65 | |
|
66 | |
|
67 | |
|
68 | |
|
69 | |
|
70 | |
protected Object formBackingObject(HttpServletRequest request) throws Exception { |
71 | 0 | SessionValueCommand command = (SessionValueCommand) createCommand(); |
72 | |
|
73 | 0 | command.setValue(sampleManager.getValue()); |
74 | 0 | return command; |
75 | |
} |
76 | |
|
77 | |
protected Map<String, Object> referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception { |
78 | 0 | Subject subject = SecurityUtils.getSubject(); |
79 | 0 | boolean hasRole1 = subject.hasRole("role1"); |
80 | 0 | boolean hasRole2 = subject.hasRole("role2"); |
81 | |
|
82 | 0 | Map<String, Object> refData = new HashMap<String, Object>(); |
83 | 0 | refData.put("hasRole1", hasRole1); |
84 | 0 | refData.put("hasRole2", hasRole2); |
85 | |
|
86 | 0 | Session session = subject.getSession(); |
87 | 0 | Map<Object, Object> sessionAttributes = new LinkedHashMap<Object, Object>(); |
88 | 0 | for (Object key : session.getAttributeKeys()) { |
89 | 0 | sessionAttributes.put(key, session.getAttribute(key)); |
90 | 0 | } |
91 | 0 | refData.put("sessionAttributes", sessionAttributes); |
92 | |
|
93 | 0 | refData.put("subjectSession", subject.getSession()); |
94 | 0 | return refData; |
95 | |
} |
96 | |
|
97 | |
protected ModelAndView onSubmit(HttpServletRequest request, HttpServletResponse response, Object obj, BindException errors) throws Exception { |
98 | 0 | SessionValueCommand command = (SessionValueCommand) obj; |
99 | |
|
100 | 0 | sampleManager.setValue(command.getValue()); |
101 | |
|
102 | 0 | return showForm(request, response, errors); |
103 | |
} |
104 | |
|
105 | |
} |