|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.taglibs.authz; |
|
17 |
| |
|
18 |
| import org.acegisecurity.Authentication; |
|
19 |
| import org.acegisecurity.GrantedAuthority; |
|
20 |
| import org.acegisecurity.GrantedAuthorityImpl; |
|
21 |
| import org.acegisecurity.context.SecurityContextHolder; |
|
22 |
| |
|
23 |
| import org.springframework.util.StringUtils; |
|
24 |
| |
|
25 |
| import org.springframework.web.util.ExpressionEvaluationUtils; |
|
26 |
| |
|
27 |
| import java.util.Arrays; |
|
28 |
| import java.util.Collection; |
|
29 |
| import java.util.Collections; |
|
30 |
| import java.util.HashSet; |
|
31 |
| import java.util.Iterator; |
|
32 |
| import java.util.Set; |
|
33 |
| |
|
34 |
| import javax.servlet.jsp.JspException; |
|
35 |
| import javax.servlet.jsp.tagext.Tag; |
|
36 |
| import javax.servlet.jsp.tagext.TagSupport; |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| public class AuthorizeTag extends TagSupport { |
|
47 |
| |
|
48 |
| |
|
49 |
| private String ifAllGranted = ""; |
|
50 |
| private String ifAnyGranted = ""; |
|
51 |
| private String ifNotGranted = ""; |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
7
| public void setIfAllGranted(String ifAllGranted) throws JspException {
|
|
56 |
7
| this.ifAllGranted = ifAllGranted;
|
|
57 |
| } |
|
58 |
| |
|
59 |
1
| public String getIfAllGranted() {
|
|
60 |
1
| return ifAllGranted;
|
|
61 |
| } |
|
62 |
| |
|
63 |
10
| public void setIfAnyGranted(String ifAnyGranted) throws JspException {
|
|
64 |
10
| this.ifAnyGranted = ifAnyGranted;
|
|
65 |
| } |
|
66 |
| |
|
67 |
1
| public String getIfAnyGranted() {
|
|
68 |
1
| return ifAnyGranted;
|
|
69 |
| } |
|
70 |
| |
|
71 |
5
| public void setIfNotGranted(String ifNotGranted) throws JspException {
|
|
72 |
5
| this.ifNotGranted = ifNotGranted;
|
|
73 |
| } |
|
74 |
| |
|
75 |
1
| public String getIfNotGranted() {
|
|
76 |
1
| return ifNotGranted;
|
|
77 |
| } |
|
78 |
| |
|
79 |
20
| public int doStartTag() throws JspException {
|
|
80 |
20
| if (((null == ifAllGranted) || "".equals(ifAllGranted))
|
|
81 |
| && ((null == ifAnyGranted) || "".equals(ifAnyGranted)) |
|
82 |
| && ((null == ifNotGranted) || "".equals(ifNotGranted))) { |
|
83 |
1
| return Tag.SKIP_BODY;
|
|
84 |
| } |
|
85 |
| |
|
86 |
19
| final Collection granted = getPrincipalAuthorities();
|
|
87 |
| |
|
88 |
19
| final String evaledIfNotGranted = ExpressionEvaluationUtils
|
|
89 |
| .evaluateString("ifNotGranted", ifNotGranted, pageContext); |
|
90 |
| |
|
91 |
19
| if ((null != evaledIfNotGranted) && !"".equals(evaledIfNotGranted)) {
|
|
92 |
5
| Set grantedCopy = retainAll(granted,
|
|
93 |
| parseAuthoritiesString(evaledIfNotGranted)); |
|
94 |
| |
|
95 |
5
| if (!grantedCopy.isEmpty()) {
|
|
96 |
3
| return Tag.SKIP_BODY;
|
|
97 |
| } |
|
98 |
| } |
|
99 |
| |
|
100 |
16
| final String evaledIfAllGranted = ExpressionEvaluationUtils
|
|
101 |
| .evaluateString("ifAllGranted", ifAllGranted, pageContext); |
|
102 |
| |
|
103 |
16
| if ((null != evaledIfAllGranted) && !"".equals(evaledIfAllGranted)) {
|
|
104 |
6
| if (!granted.containsAll(parseAuthoritiesString(evaledIfAllGranted))) {
|
|
105 |
3
| return Tag.SKIP_BODY;
|
|
106 |
| } |
|
107 |
| } |
|
108 |
| |
|
109 |
13
| final String evaledIfAnyGranted = ExpressionEvaluationUtils
|
|
110 |
| .evaluateString("ifAnyGranted", ifAnyGranted, pageContext); |
|
111 |
| |
|
112 |
13
| if ((null != evaledIfAnyGranted) && !"".equals(evaledIfAnyGranted)) {
|
|
113 |
8
| Set grantedCopy = retainAll(granted,
|
|
114 |
| parseAuthoritiesString(evaledIfAnyGranted)); |
|
115 |
| |
|
116 |
7
| if (grantedCopy.isEmpty()) {
|
|
117 |
3
| return Tag.SKIP_BODY;
|
|
118 |
| } |
|
119 |
| } |
|
120 |
| |
|
121 |
9
| return Tag.EVAL_BODY_INCLUDE;
|
|
122 |
| } |
|
123 |
| |
|
124 |
19
| private Collection getPrincipalAuthorities() {
|
|
125 |
19
| Authentication currentUser = SecurityContextHolder.getContext()
|
|
126 |
| .getAuthentication(); |
|
127 |
| |
|
128 |
19
| if (null == currentUser) {
|
|
129 |
2
| return Collections.EMPTY_LIST;
|
|
130 |
| } |
|
131 |
| |
|
132 |
17
| if ((null == currentUser.getAuthorities())
|
|
133 |
| || (currentUser.getAuthorities().length < 1)) { |
|
134 |
0
| return Collections.EMPTY_LIST;
|
|
135 |
| } |
|
136 |
| |
|
137 |
17
| Collection granted = Arrays.asList(currentUser.getAuthorities());
|
|
138 |
| |
|
139 |
17
| return granted;
|
|
140 |
| } |
|
141 |
| |
|
142 |
25
| private Set authoritiesToRoles(Collection c) {
|
|
143 |
25
| Set target = new HashSet();
|
|
144 |
| |
|
145 |
25
| for (Iterator iterator = c.iterator(); iterator.hasNext();) {
|
|
146 |
33
| GrantedAuthority authority = (GrantedAuthority) iterator.next();
|
|
147 |
| |
|
148 |
33
| if (null == authority.getAuthority()) {
|
|
149 |
1
| throw new IllegalArgumentException(
|
|
150 |
| "Cannot process GrantedAuthority objects which return null from getAuthority() - attempting to process " |
|
151 |
| + authority.toString()); |
|
152 |
| } |
|
153 |
| |
|
154 |
32
| target.add(authority.getAuthority());
|
|
155 |
| } |
|
156 |
| |
|
157 |
24
| return target;
|
|
158 |
| } |
|
159 |
| |
|
160 |
19
| private Set parseAuthoritiesString(String authorizationsString) {
|
|
161 |
19
| final Set requiredAuthorities = new HashSet();
|
|
162 |
19
| final String[] authorities = StringUtils
|
|
163 |
| .commaDelimitedListToStringArray(authorizationsString); |
|
164 |
| |
|
165 |
19
| for (int i = 0; i < authorities.length; i++) {
|
|
166 |
25
| String authority = authorities[i];
|
|
167 |
| |
|
168 |
| |
|
169 |
| |
|
170 |
25
| String role = StringUtils.replace(authority, " ", "");
|
|
171 |
25
| role = StringUtils.replace(role, "\t", "");
|
|
172 |
25
| role = StringUtils.replace(role, "\r", "");
|
|
173 |
25
| role = StringUtils.replace(role, "\n", "");
|
|
174 |
25
| role = StringUtils.replace(role, "\f", "");
|
|
175 |
| |
|
176 |
25
| requiredAuthorities.add(new GrantedAuthorityImpl(role));
|
|
177 |
| } |
|
178 |
| |
|
179 |
19
| return requiredAuthorities;
|
|
180 |
| } |
|
181 |
| |
|
182 |
| |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
| |
|
192 |
| |
|
193 |
| |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
| |
|
203 |
| |
|
204 |
| |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
| |
|
213 |
| |
|
214 |
| |
|
215 |
| |
|
216 |
| |
|
217 |
| |
|
218 |
| |
|
219 |
| |
|
220 |
| |
|
221 |
13
| private Set retainAll(final Collection granted, final Set required) {
|
|
222 |
13
| Set grantedRoles = authoritiesToRoles(granted);
|
|
223 |
12
| Set requiredRoles = authoritiesToRoles(required);
|
|
224 |
12
| grantedRoles.retainAll(requiredRoles);
|
|
225 |
| |
|
226 |
12
| return rolesToAuthorities(grantedRoles, granted);
|
|
227 |
| } |
|
228 |
| |
|
229 |
12
| private Set rolesToAuthorities(Set grantedRoles, Collection granted) {
|
|
230 |
12
| Set target = new HashSet();
|
|
231 |
| |
|
232 |
12
| for (Iterator iterator = grantedRoles.iterator(); iterator.hasNext();) {
|
|
233 |
7
| String role = (String) iterator.next();
|
|
234 |
| |
|
235 |
7
| for (Iterator grantedIterator = granted.iterator();
|
|
236 |
10
| grantedIterator.hasNext();) {
|
|
237 |
10
| GrantedAuthority authority = (GrantedAuthority) grantedIterator
|
|
238 |
| .next(); |
|
239 |
| |
|
240 |
10
| if (authority.getAuthority().equals(role)) {
|
|
241 |
7
| target.add(authority);
|
|
242 |
| |
|
243 |
7
| break;
|
|
244 |
| } |
|
245 |
| } |
|
246 |
| } |
|
247 |
| |
|
248 |
12
| return target;
|
|
249 |
| } |
|
250 |
| } |