|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.adapters.resin; |
|
17 |
| |
|
18 |
| import com.caucho.http.security.AbstractAuthenticator; |
|
19 |
| |
|
20 |
| import org.acegisecurity.Authentication; |
|
21 |
| import org.acegisecurity.AuthenticationException; |
|
22 |
| import org.acegisecurity.AuthenticationManager; |
|
23 |
| |
|
24 |
| import org.acegisecurity.adapters.PrincipalAcegiUserToken; |
|
25 |
| |
|
26 |
| import org.acegisecurity.providers.UsernamePasswordAuthenticationToken; |
|
27 |
| |
|
28 |
| import org.apache.commons.logging.Log; |
|
29 |
| import org.apache.commons.logging.LogFactory; |
|
30 |
| |
|
31 |
| import org.springframework.context.support.ClassPathXmlApplicationContext; |
|
32 |
| |
|
33 |
| import java.security.Principal; |
|
34 |
| |
|
35 |
| import java.util.Map; |
|
36 |
| |
|
37 |
| import javax.servlet.ServletContext; |
|
38 |
| import javax.servlet.ServletException; |
|
39 |
| import javax.servlet.http.HttpServletRequest; |
|
40 |
| import javax.servlet.http.HttpServletResponse; |
|
41 |
| |
|
42 |
| |
|
43 |
| |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| public class ResinAcegiAuthenticator extends AbstractAuthenticator { |
|
57 |
| |
|
58 |
| |
|
59 |
| private static final Log logger = LogFactory.getLog(ResinAcegiAuthenticator.class); |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| private AuthenticationManager authenticationManager; |
|
64 |
| private String appContextLocation; |
|
65 |
| private String key; |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
15
| public void setAppContextLocation(String appContextLocation) {
|
|
70 |
15
| this.appContextLocation = appContextLocation;
|
|
71 |
| } |
|
72 |
| |
|
73 |
1
| public String getAppContextLocation() {
|
|
74 |
1
| return appContextLocation;
|
|
75 |
| } |
|
76 |
| |
|
77 |
15
| public void setKey(String key) {
|
|
78 |
15
| this.key = key;
|
|
79 |
| } |
|
80 |
| |
|
81 |
1
| public String getKey() {
|
|
82 |
1
| return key;
|
|
83 |
| } |
|
84 |
| |
|
85 |
5
| public boolean isUserInRole(HttpServletRequest request,
|
|
86 |
| HttpServletResponse response, ServletContext application, |
|
87 |
| Principal principal, String role) { |
|
88 |
5
| if (!(principal instanceof PrincipalAcegiUserToken)) {
|
|
89 |
2
| if (logger.isWarnEnabled()) {
|
|
90 |
2
| logger.warn(
|
|
91 |
| "Expected passed principal to be of type PrincipalAcegiUserToken"); |
|
92 |
| } |
|
93 |
| |
|
94 |
2
| return false;
|
|
95 |
| } |
|
96 |
| |
|
97 |
3
| PrincipalAcegiUserToken test = (PrincipalAcegiUserToken) principal;
|
|
98 |
| |
|
99 |
3
| return test.isUserInRole(role);
|
|
100 |
| } |
|
101 |
| |
|
102 |
16
| public void init() throws ServletException {
|
|
103 |
16
| super.init();
|
|
104 |
| |
|
105 |
16
| if ((appContextLocation == null) || "".equals(appContextLocation)) {
|
|
106 |
2
| throw new ServletException("appContextLocation must be defined");
|
|
107 |
| } |
|
108 |
| |
|
109 |
14
| if ((key == null) || "".equals(key)) {
|
|
110 |
2
| throw new ServletException("key must be defined");
|
|
111 |
| } |
|
112 |
| |
|
113 |
12
| if (Thread.currentThread().getContextClassLoader().getResource(appContextLocation) == null) {
|
|
114 |
1
| throw new ServletException("Cannot locate " + appContextLocation);
|
|
115 |
| } |
|
116 |
| |
|
117 |
11
| ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext(appContextLocation);
|
|
118 |
11
| Map beans = ctx.getBeansOfType(AuthenticationManager.class, true, true);
|
|
119 |
| |
|
120 |
11
| if (beans.size() == 0) {
|
|
121 |
1
| throw new ServletException(
|
|
122 |
| "Bean context must contain at least one bean of type AuthenticationManager"); |
|
123 |
| } |
|
124 |
| |
|
125 |
10
| String beanName = (String) beans.keySet().iterator().next();
|
|
126 |
10
| authenticationManager = (AuthenticationManager) beans.get(beanName);
|
|
127 |
10
| logger.info("ResinAcegiAuthenticator Started");
|
|
128 |
| } |
|
129 |
| |
|
130 |
6
| protected Principal loginImpl(String username, String credentials) {
|
|
131 |
6
| if (username == null) {
|
|
132 |
1
| return null;
|
|
133 |
| } |
|
134 |
| |
|
135 |
5
| if (credentials == null) {
|
|
136 |
1
| credentials = "";
|
|
137 |
| } |
|
138 |
| |
|
139 |
5
| Authentication request = new UsernamePasswordAuthenticationToken(username,
|
|
140 |
| credentials); |
|
141 |
5
| Authentication response = null;
|
|
142 |
| |
|
143 |
5
| try {
|
|
144 |
5
| response = authenticationManager.authenticate(request);
|
|
145 |
| } catch (AuthenticationException failed) { |
|
146 |
3
| if (logger.isDebugEnabled()) {
|
|
147 |
0
| logger.debug("Authentication request for user: " + username
|
|
148 |
| + " failed: " + failed.toString()); |
|
149 |
| } |
|
150 |
| |
|
151 |
3
| return null;
|
|
152 |
| } |
|
153 |
| |
|
154 |
2
| return new PrincipalAcegiUserToken(this.key,
|
|
155 |
| response.getPrincipal().toString(), |
|
156 |
| response.getCredentials().toString(), response.getAuthorities(), |
|
157 |
| response.getPrincipal()); |
|
158 |
| } |
|
159 |
| |
|
160 |
1
| protected Principal loginImpl(HttpServletRequest request,
|
|
161 |
| HttpServletResponse response, ServletContext application, |
|
162 |
| String userName, String password) throws ServletException { |
|
163 |
1
| return loginImpl(userName, password);
|
|
164 |
| } |
|
165 |
| } |