|
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 |
| |
|
20 |
| import org.acegisecurity.context.SecurityContext; |
|
21 |
| import org.acegisecurity.context.SecurityContextHolder; |
|
22 |
| import org.acegisecurity.userdetails.UserDetails; |
|
23 |
| |
|
24 |
| import java.io.IOException; |
|
25 |
| |
|
26 |
| import java.lang.reflect.InvocationTargetException; |
|
27 |
| import java.lang.reflect.Method; |
|
28 |
| |
|
29 |
| import java.util.HashSet; |
|
30 |
| import java.util.Set; |
|
31 |
| |
|
32 |
| import javax.servlet.jsp.JspException; |
|
33 |
| import javax.servlet.jsp.tagext.Tag; |
|
34 |
| import javax.servlet.jsp.tagext.TagSupport; |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| public class AuthenticationTag extends TagSupport { |
|
52 |
| |
|
53 |
| |
|
54 |
| private final static Set methodPrefixValidOptions = new HashSet(); |
|
55 |
| |
|
56 |
| static { |
|
57 |
1
| methodPrefixValidOptions.add("get");
|
|
58 |
1
| methodPrefixValidOptions.add("is");
|
|
59 |
| } |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| private String methodPrefix = "get"; |
|
64 |
| private String operation = ""; |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
2
| public void setMethodPrefix(String methodPrefix) {
|
|
69 |
2
| this.methodPrefix = methodPrefix;
|
|
70 |
| } |
|
71 |
| |
|
72 |
24
| public String getMethodPrefix() {
|
|
73 |
24
| return methodPrefix;
|
|
74 |
| } |
|
75 |
| |
|
76 |
8
| public void setOperation(String operation) {
|
|
77 |
8
| this.operation = operation;
|
|
78 |
| } |
|
79 |
| |
|
80 |
4
| public String getOperation() {
|
|
81 |
4
| return operation;
|
|
82 |
| } |
|
83 |
| |
|
84 |
8
| public int doStartTag() throws JspException {
|
|
85 |
8
| if ((null == operation) || "".equals(operation)) {
|
|
86 |
1
| return Tag.SKIP_BODY;
|
|
87 |
| } |
|
88 |
| |
|
89 |
7
| validateArguments();
|
|
90 |
| |
|
91 |
6
| if ((SecurityContextHolder.getContext() == null)
|
|
92 |
| || !(SecurityContextHolder.getContext() instanceof SecurityContext) |
|
93 |
| || (((SecurityContext) SecurityContextHolder.getContext()) |
|
94 |
| .getAuthentication() == null)) { |
|
95 |
1
| return Tag.SKIP_BODY;
|
|
96 |
| } |
|
97 |
| |
|
98 |
5
| Authentication auth = SecurityContextHolder.getContext()
|
|
99 |
| .getAuthentication(); |
|
100 |
| |
|
101 |
5
| if (auth.getPrincipal() == null) {
|
|
102 |
1
| return Tag.SKIP_BODY;
|
|
103 |
4
| } else if (auth.getPrincipal() instanceof UserDetails) {
|
|
104 |
3
| writeMessage(invokeOperation(auth.getPrincipal()));
|
|
105 |
| |
|
106 |
2
| return Tag.SKIP_BODY;
|
|
107 |
| } else { |
|
108 |
1
| writeMessage(auth.getPrincipal().toString());
|
|
109 |
| |
|
110 |
1
| return Tag.SKIP_BODY;
|
|
111 |
| } |
|
112 |
| } |
|
113 |
| |
|
114 |
3
| protected String invokeOperation(Object obj) throws JspException {
|
|
115 |
3
| Class clazz = obj.getClass();
|
|
116 |
3
| String methodToInvoke = getOperation();
|
|
117 |
3
| StringBuffer methodName = new StringBuffer();
|
|
118 |
3
| methodName.append(getMethodPrefix());
|
|
119 |
3
| methodName.append(methodToInvoke.substring(0, 1).toUpperCase());
|
|
120 |
3
| methodName.append(methodToInvoke.substring(1));
|
|
121 |
| |
|
122 |
3
| Method method = null;
|
|
123 |
| |
|
124 |
3
| try {
|
|
125 |
3
| method = clazz.getMethod(methodName.toString(), (Class[]) null);
|
|
126 |
| } catch (SecurityException se) { |
|
127 |
0
| throw new JspException(se);
|
|
128 |
| } catch (NoSuchMethodException nsme) { |
|
129 |
1
| throw new JspException(nsme);
|
|
130 |
| } |
|
131 |
| |
|
132 |
2
| Object retVal = null;
|
|
133 |
| |
|
134 |
2
| try {
|
|
135 |
2
| retVal = method.invoke(obj, (Object[]) null);
|
|
136 |
| } catch (IllegalArgumentException iae) { |
|
137 |
0
| throw new JspException(iae);
|
|
138 |
| } catch (IllegalAccessException iae) { |
|
139 |
0
| throw new JspException(iae);
|
|
140 |
| } catch (InvocationTargetException ite) { |
|
141 |
0
| throw new JspException(ite);
|
|
142 |
| } |
|
143 |
| |
|
144 |
2
| if (retVal == null) {
|
|
145 |
0
| retVal = "";
|
|
146 |
| } |
|
147 |
| |
|
148 |
2
| return retVal.toString();
|
|
149 |
| } |
|
150 |
| |
|
151 |
7
| protected void validateArguments() throws JspException {
|
|
152 |
7
| if ((getMethodPrefix() != null) && !getMethodPrefix().equals("")) {
|
|
153 |
7
| if (!methodPrefixValidOptions.contains(getMethodPrefix())) {
|
|
154 |
1
| throw new JspException(
|
|
155 |
| "Authorization tag : no valid method prefix available"); |
|
156 |
| } |
|
157 |
| } else { |
|
158 |
0
| throw new JspException(
|
|
159 |
| "Authorization tag : no method prefix available"); |
|
160 |
| } |
|
161 |
| } |
|
162 |
| |
|
163 |
0
| protected void writeMessage(String msg) throws JspException {
|
|
164 |
0
| try {
|
|
165 |
0
| pageContext.getOut().write(String.valueOf(msg));
|
|
166 |
| } catch (IOException ioe) { |
|
167 |
0
| throw new JspException(ioe);
|
|
168 |
| } |
|
169 |
| } |
|
170 |
| } |