|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.adapters.jetty; |
|
17 |
| |
|
18 |
| import org.acegisecurity.Authentication; |
|
19 |
| import org.acegisecurity.AuthenticationException; |
|
20 |
| import org.acegisecurity.AuthenticationManager; |
|
21 |
| import org.acegisecurity.providers.UsernamePasswordAuthenticationToken; |
|
22 |
| |
|
23 |
| import org.apache.commons.logging.Log; |
|
24 |
| import org.apache.commons.logging.LogFactory; |
|
25 |
| |
|
26 |
| import org.mortbay.http.HttpRequest; |
|
27 |
| import org.mortbay.http.UserPrincipal; |
|
28 |
| import org.mortbay.http.UserRealm; |
|
29 |
| |
|
30 |
| import org.springframework.context.support.ClassPathXmlApplicationContext; |
|
31 |
| |
|
32 |
| import java.util.Map; |
|
33 |
| |
|
34 |
| |
|
35 |
| |
|
36 |
| |
|
37 |
| |
|
38 |
| |
|
39 |
| |
|
40 |
| |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| public final class JettyAcegiUserRealm implements UserRealm { |
|
49 |
| |
|
50 |
| |
|
51 |
| private static final Log logger = LogFactory.getLog(JettyAcegiUserRealm.class); |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| private AuthenticationManager authenticationManager; |
|
56 |
| private String key; |
|
57 |
| private String realm; |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
20
| public JettyAcegiUserRealm(String realm, String providerKey,
|
|
72 |
| String appContextLocation) { |
|
73 |
20
| this.realm = realm;
|
|
74 |
20
| this.key = providerKey;
|
|
75 |
| |
|
76 |
20
| if ((realm == null) || "".equals(realm)) {
|
|
77 |
2
| throw new IllegalArgumentException("realm must be specified");
|
|
78 |
| } |
|
79 |
| |
|
80 |
18
| if ((key == null) || "".equals(key)) {
|
|
81 |
2
| throw new IllegalArgumentException("key must be specified");
|
|
82 |
| } |
|
83 |
| |
|
84 |
16
| if ((appContextLocation == null) || "".equals(appContextLocation)) {
|
|
85 |
2
| throw new IllegalArgumentException(
|
|
86 |
| "appContextLocation must be specified"); |
|
87 |
| } |
|
88 |
| |
|
89 |
14
| if (Thread.currentThread().getContextClassLoader().getResource(appContextLocation) == null) {
|
|
90 |
1
| throw new IllegalArgumentException("Cannot locate "
|
|
91 |
| + appContextLocation); |
|
92 |
| } |
|
93 |
| |
|
94 |
13
| ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(appContextLocation);
|
|
95 |
13
| Map beans = ctx.getBeansOfType(AuthenticationManager.class, true, true);
|
|
96 |
| |
|
97 |
13
| if (beans.size() == 0) {
|
|
98 |
1
| throw new IllegalArgumentException(
|
|
99 |
| "Bean context must contain at least one bean of type AuthenticationManager"); |
|
100 |
| } |
|
101 |
| |
|
102 |
12
| String beanName = (String) beans.keySet().iterator().next();
|
|
103 |
12
| authenticationManager = (AuthenticationManager) beans.get(beanName);
|
|
104 |
| } |
|
105 |
| |
|
106 |
1
| protected JettyAcegiUserRealm() {
|
|
107 |
1
| throw new IllegalArgumentException("Cannot use default constructor");
|
|
108 |
| } |
|
109 |
| |
|
110 |
| |
|
111 |
| |
|
112 |
1
| public AuthenticationManager getAuthenticationManager() {
|
|
113 |
1
| return authenticationManager;
|
|
114 |
| } |
|
115 |
| |
|
116 |
| |
|
117 |
| |
|
118 |
| |
|
119 |
| |
|
120 |
| |
|
121 |
| |
|
122 |
1
| public String getName() {
|
|
123 |
1
| return this.realm;
|
|
124 |
| } |
|
125 |
| |
|
126 |
5
| public UserPrincipal authenticate(String username, Object password,
|
|
127 |
| HttpRequest httpRequest) { |
|
128 |
5
| if (username == null) {
|
|
129 |
1
| return null;
|
|
130 |
| } |
|
131 |
| |
|
132 |
4
| if (password == null) {
|
|
133 |
1
| password = "";
|
|
134 |
| } |
|
135 |
| |
|
136 |
4
| Authentication request = new UsernamePasswordAuthenticationToken(username
|
|
137 |
| .toString(), password.toString()); |
|
138 |
4
| Authentication response = null;
|
|
139 |
| |
|
140 |
4
| try {
|
|
141 |
4
| response = authenticationManager.authenticate(request);
|
|
142 |
| } catch (AuthenticationException failed) { |
|
143 |
3
| if (logger.isDebugEnabled()) {
|
|
144 |
0
| logger.debug("Authentication request for user: " + username
|
|
145 |
| + " failed: " + failed.toString()); |
|
146 |
| } |
|
147 |
| |
|
148 |
3
| return null;
|
|
149 |
| } |
|
150 |
| |
|
151 |
1
| return new JettyAcegiUserToken(this.key,
|
|
152 |
| response.getPrincipal().toString(), |
|
153 |
| response.getCredentials().toString(), response.getAuthorities()); |
|
154 |
| } |
|
155 |
| |
|
156 |
1
| public void disassociate(UserPrincipal userPrincipal) {
|
|
157 |
| |
|
158 |
| } |
|
159 |
| |
|
160 |
1
| public void logout(UserPrincipal arg0) {
|
|
161 |
| |
|
162 |
| } |
|
163 |
| |
|
164 |
1
| public UserPrincipal popRole(UserPrincipal userPrincipal) {
|
|
165 |
| |
|
166 |
1
| return userPrincipal;
|
|
167 |
| } |
|
168 |
| |
|
169 |
1
| public UserPrincipal pushRole(UserPrincipal userPrincipal, String role) {
|
|
170 |
| |
|
171 |
1
| return userPrincipal;
|
|
172 |
| } |
|
173 |
| } |