|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.adapters.catalina; |
|
17 |
| |
|
18 |
| import org.acegisecurity.Authentication; |
|
19 |
| import org.acegisecurity.AuthenticationException; |
|
20 |
| import org.acegisecurity.AuthenticationManager; |
|
21 |
| |
|
22 |
| import org.acegisecurity.adapters.PrincipalAcegiUserToken; |
|
23 |
| |
|
24 |
| import org.acegisecurity.providers.UsernamePasswordAuthenticationToken; |
|
25 |
| |
|
26 |
| import org.apache.catalina.Container; |
|
27 |
| import org.apache.catalina.LifecycleException; |
|
28 |
| import org.apache.catalina.realm.RealmBase; |
|
29 |
| |
|
30 |
| import org.apache.commons.logging.Log; |
|
31 |
| import org.apache.commons.logging.LogFactory; |
|
32 |
| |
|
33 |
| import org.springframework.context.support.FileSystemXmlApplicationContext; |
|
34 |
| |
|
35 |
| import java.io.File; |
|
36 |
| |
|
37 |
| import java.security.Principal; |
|
38 |
| import java.security.cert.X509Certificate; |
|
39 |
| |
|
40 |
| import java.util.Map; |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| public class CatalinaAcegiUserRealm extends RealmBase { |
|
57 |
| |
|
58 |
| |
|
59 |
| private static final Log logger = LogFactory.getLog(CatalinaAcegiUserRealm.class); |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| protected final String name = "CatalinaSpringUserRealm / $Id: CatalinaAcegiUserRealm.java,v 1.9 2005/11/25 00:26:30 benalex Exp $"; |
|
64 |
| private AuthenticationManager authenticationManager; |
|
65 |
| private Container container; |
|
66 |
| private String appContextLocation; |
|
67 |
| private String key; |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
12
| public void setAppContextLocation(String appContextLocation) {
|
|
72 |
12
| this.appContextLocation = appContextLocation;
|
|
73 |
| } |
|
74 |
| |
|
75 |
1
| public String getAppContextLocation() {
|
|
76 |
1
| return appContextLocation;
|
|
77 |
| } |
|
78 |
| |
|
79 |
12
| public void setKey(String key) {
|
|
80 |
12
| this.key = key;
|
|
81 |
| } |
|
82 |
| |
|
83 |
1
| public String getKey() {
|
|
84 |
1
| return key;
|
|
85 |
| } |
|
86 |
| |
|
87 |
6
| public Principal authenticate(String username, String credentials) {
|
|
88 |
6
| if (username == null) {
|
|
89 |
1
| return null;
|
|
90 |
| } |
|
91 |
| |
|
92 |
5
| if (credentials == null) {
|
|
93 |
1
| credentials = "";
|
|
94 |
| } |
|
95 |
| |
|
96 |
5
| Authentication request = new UsernamePasswordAuthenticationToken(username,
|
|
97 |
| credentials); |
|
98 |
5
| Authentication response = null;
|
|
99 |
| |
|
100 |
5
| try {
|
|
101 |
5
| response = authenticationManager.authenticate(request);
|
|
102 |
| } catch (AuthenticationException failed) { |
|
103 |
3
| if (logger.isDebugEnabled()) {
|
|
104 |
0
| logger.debug("Authentication request for user: " + username
|
|
105 |
| + " failed: " + failed.toString()); |
|
106 |
| } |
|
107 |
| |
|
108 |
3
| return null;
|
|
109 |
| } |
|
110 |
| |
|
111 |
2
| return new PrincipalAcegiUserToken(this.key,
|
|
112 |
| response.getPrincipal().toString(), |
|
113 |
| response.getCredentials().toString(), response.getAuthorities(), |
|
114 |
| response.getPrincipal()); |
|
115 |
| } |
|
116 |
| |
|
117 |
1
| public Principal authenticate(String username, byte[] credentials) {
|
|
118 |
1
| return authenticate(username, new String(credentials));
|
|
119 |
| } |
|
120 |
| |
|
121 |
| |
|
122 |
| |
|
123 |
| |
|
124 |
| |
|
125 |
| |
|
126 |
| |
|
127 |
| |
|
128 |
| |
|
129 |
| |
|
130 |
| |
|
131 |
| |
|
132 |
| |
|
133 |
| |
|
134 |
| |
|
135 |
1
| public java.security.Principal authenticate(java.lang.String username,
|
|
136 |
| java.lang.String digest, java.lang.String nonce, java.lang.String nc, |
|
137 |
| java.lang.String cnonce, java.lang.String qop, java.lang.String realm, |
|
138 |
| java.lang.String md5a2) { |
|
139 |
1
| return null;
|
|
140 |
| } |
|
141 |
| |
|
142 |
| |
|
143 |
| |
|
144 |
| |
|
145 |
| |
|
146 |
| |
|
147 |
| |
|
148 |
| |
|
149 |
1
| public Principal authenticate(X509Certificate[] x509Certificates) {
|
|
150 |
1
| return null;
|
|
151 |
| } |
|
152 |
| |
|
153 |
5
| public boolean hasRole(Principal principal, String role) {
|
|
154 |
5
| if ((principal == null) || (role == null)) {
|
|
155 |
1
| return false;
|
|
156 |
| } |
|
157 |
| |
|
158 |
4
| if (!(principal instanceof PrincipalAcegiUserToken)) {
|
|
159 |
1
| logger.warn(
|
|
160 |
| "Expected passed principal to be of type PrincipalAcegiUserToken but was " |
|
161 |
| + principal.getClass().getName()); |
|
162 |
| |
|
163 |
1
| return false;
|
|
164 |
| } |
|
165 |
| |
|
166 |
3
| PrincipalAcegiUserToken test = (PrincipalAcegiUserToken) principal;
|
|
167 |
| |
|
168 |
3
| return test.isUserInRole(role);
|
|
169 |
| } |
|
170 |
| |
|
171 |
| |
|
172 |
| |
|
173 |
| |
|
174 |
| |
|
175 |
| |
|
176 |
0
| public void start() throws LifecycleException {
|
|
177 |
0
| this.start(true);
|
|
178 |
| } |
|
179 |
| |
|
180 |
1
| protected String getName() {
|
|
181 |
1
| return this.name;
|
|
182 |
| } |
|
183 |
| |
|
184 |
| |
|
185 |
| |
|
186 |
| |
|
187 |
| |
|
188 |
| |
|
189 |
| |
|
190 |
| |
|
191 |
1
| protected String getPassword(String arg0) {
|
|
192 |
1
| return null;
|
|
193 |
| } |
|
194 |
| |
|
195 |
| |
|
196 |
| |
|
197 |
| |
|
198 |
| |
|
199 |
| |
|
200 |
| |
|
201 |
| |
|
202 |
1
| protected Principal getPrincipal(String arg0) {
|
|
203 |
1
| return null;
|
|
204 |
| } |
|
205 |
| |
|
206 |
| |
|
207 |
| |
|
208 |
| |
|
209 |
| |
|
210 |
| |
|
211 |
| |
|
212 |
13
| protected void startForTest() throws LifecycleException {
|
|
213 |
13
| this.start(false);
|
|
214 |
| } |
|
215 |
| |
|
216 |
13
| private void start(boolean startParent) throws LifecycleException {
|
|
217 |
13
| if (startParent) {
|
|
218 |
0
| super.start();
|
|
219 |
| } |
|
220 |
| |
|
221 |
13
| if ((appContextLocation == null) || "".equals(appContextLocation)) {
|
|
222 |
2
| throw new LifecycleException("appContextLocation must be defined");
|
|
223 |
| } |
|
224 |
| |
|
225 |
11
| if ((key == null) || "".equals(key)) {
|
|
226 |
2
| throw new LifecycleException("key must be defined");
|
|
227 |
| } |
|
228 |
| |
|
229 |
9
| File xml = new File(System.getProperty("catalina.base"),
|
|
230 |
| appContextLocation); |
|
231 |
| |
|
232 |
9
| if (!xml.exists()) {
|
|
233 |
1
| throw new LifecycleException(
|
|
234 |
| "appContextLocation does not seem to exist in " |
|
235 |
| + xml.toString()); |
|
236 |
| } |
|
237 |
| |
|
238 |
8
| FileSystemXmlApplicationContext ctx = new FileSystemXmlApplicationContext(
|
|
239 |
| "file:" + xml.getAbsolutePath()); |
|
240 |
8
| Map beans = ctx.getBeansOfType(AuthenticationManager.class, true, true);
|
|
241 |
| |
|
242 |
8
| if (beans.size() == 0) {
|
|
243 |
1
| throw new IllegalArgumentException(
|
|
244 |
| "Bean context must contain at least one bean of type AuthenticationManager"); |
|
245 |
| } |
|
246 |
| |
|
247 |
7
| String beanName = (String) beans.keySet().iterator().next();
|
|
248 |
7
| authenticationManager = (AuthenticationManager) beans.get(beanName);
|
|
249 |
7
| logger.info("CatalinaAcegiUserRealm Started");
|
|
250 |
| } |
|
251 |
| } |