|
1 |
| |
|
2 |
| |
|
3 |
| |
|
4 |
| |
|
5 |
| |
|
6 |
| |
|
7 |
| |
|
8 |
| |
|
9 |
| |
|
10 |
| |
|
11 |
| |
|
12 |
| |
|
13 |
| |
|
14 |
| |
|
15 |
| |
|
16 |
| package org.acegisecurity.ui.basicauth; |
|
17 |
| |
|
18 |
| import org.acegisecurity.Authentication; |
|
19 |
| import org.acegisecurity.AuthenticationException; |
|
20 |
| import org.acegisecurity.AuthenticationManager; |
|
21 |
| import org.acegisecurity.context.SecurityContextHolder; |
|
22 |
| import org.acegisecurity.intercept.web.AuthenticationEntryPoint; |
|
23 |
| import org.acegisecurity.providers.UsernamePasswordAuthenticationToken; |
|
24 |
| import org.acegisecurity.ui.WebAuthenticationDetails; |
|
25 |
| |
|
26 |
| import org.apache.commons.codec.binary.Base64; |
|
27 |
| import org.apache.commons.logging.Log; |
|
28 |
| import org.apache.commons.logging.LogFactory; |
|
29 |
| |
|
30 |
| import org.springframework.beans.factory.InitializingBean; |
|
31 |
| |
|
32 |
| import org.springframework.util.Assert; |
|
33 |
| |
|
34 |
| import java.io.IOException; |
|
35 |
| |
|
36 |
| import javax.servlet.Filter; |
|
37 |
| import javax.servlet.FilterChain; |
|
38 |
| import javax.servlet.FilterConfig; |
|
39 |
| import javax.servlet.ServletException; |
|
40 |
| import javax.servlet.ServletRequest; |
|
41 |
| import javax.servlet.ServletResponse; |
|
42 |
| import javax.servlet.http.HttpServletRequest; |
|
43 |
| import javax.servlet.http.HttpServletResponse; |
|
44 |
| |
|
45 |
| |
|
46 |
| |
|
47 |
| |
|
48 |
| |
|
49 |
| |
|
50 |
| |
|
51 |
| |
|
52 |
| |
|
53 |
| |
|
54 |
| |
|
55 |
| |
|
56 |
| |
|
57 |
| |
|
58 |
| |
|
59 |
| |
|
60 |
| |
|
61 |
| |
|
62 |
| |
|
63 |
| |
|
64 |
| |
|
65 |
| |
|
66 |
| |
|
67 |
| |
|
68 |
| |
|
69 |
| |
|
70 |
| |
|
71 |
| |
|
72 |
| |
|
73 |
| |
|
74 |
| |
|
75 |
| |
|
76 |
| |
|
77 |
| |
|
78 |
| |
|
79 |
| |
|
80 |
| |
|
81 |
| |
|
82 |
| |
|
83 |
| |
|
84 |
| |
|
85 |
| |
|
86 |
| |
|
87 |
| |
|
88 |
| |
|
89 |
| |
|
90 |
| |
|
91 |
| |
|
92 |
| |
|
93 |
| |
|
94 |
| |
|
95 |
| |
|
96 |
| |
|
97 |
| |
|
98 |
| |
|
99 |
| |
|
100 |
| |
|
101 |
| |
|
102 |
| |
|
103 |
| |
|
104 |
| public class BasicProcessingFilter implements Filter, InitializingBean { |
|
105 |
| |
|
106 |
| |
|
107 |
| private static final Log logger = LogFactory.getLog(BasicProcessingFilter.class); |
|
108 |
| |
|
109 |
| |
|
110 |
| |
|
111 |
| private AuthenticationEntryPoint authenticationEntryPoint; |
|
112 |
| private AuthenticationManager authenticationManager; |
|
113 |
| |
|
114 |
| |
|
115 |
| |
|
116 |
8
| public void setAuthenticationEntryPoint(
|
|
117 |
| AuthenticationEntryPoint authenticationEntryPoint) { |
|
118 |
8
| this.authenticationEntryPoint = authenticationEntryPoint;
|
|
119 |
| } |
|
120 |
| |
|
121 |
1
| public AuthenticationEntryPoint getAuthenticationEntryPoint() {
|
|
122 |
1
| return authenticationEntryPoint;
|
|
123 |
| } |
|
124 |
| |
|
125 |
8
| public void setAuthenticationManager(
|
|
126 |
| AuthenticationManager authenticationManager) { |
|
127 |
8
| this.authenticationManager = authenticationManager;
|
|
128 |
| } |
|
129 |
| |
|
130 |
1
| public AuthenticationManager getAuthenticationManager() {
|
|
131 |
1
| return authenticationManager;
|
|
132 |
| } |
|
133 |
| |
|
134 |
8
| public void afterPropertiesSet() throws Exception {
|
|
135 |
8
| Assert.notNull(this.authenticationManager,
|
|
136 |
| "An AuthenticationManager is required"); |
|
137 |
7
| Assert.notNull(this.authenticationEntryPoint,
|
|
138 |
| "An AuthenticationEntryPoint is required"); |
|
139 |
| } |
|
140 |
| |
|
141 |
7
| public void destroy() {}
|
|
142 |
| |
|
143 |
9
| public void doFilter(ServletRequest request, ServletResponse response,
|
|
144 |
| FilterChain chain) throws IOException, ServletException { |
|
145 |
9
| if (!(request instanceof HttpServletRequest)) {
|
|
146 |
1
| throw new ServletException("Can only process HttpServletRequest");
|
|
147 |
| } |
|
148 |
| |
|
149 |
8
| if (!(response instanceof HttpServletResponse)) {
|
|
150 |
1
| throw new ServletException("Can only process HttpServletResponse");
|
|
151 |
| } |
|
152 |
| |
|
153 |
7
| HttpServletRequest httpRequest = (HttpServletRequest) request;
|
|
154 |
| |
|
155 |
7
| String header = httpRequest.getHeader("Authorization");
|
|
156 |
| |
|
157 |
7
| if (logger.isDebugEnabled()) {
|
|
158 |
0
| logger.debug("Authorization header: " + header);
|
|
159 |
| } |
|
160 |
| |
|
161 |
7
| if ((header != null) && header.startsWith("Basic ")) {
|
|
162 |
5
| String base64Token = header.substring(6);
|
|
163 |
5
| String token = new String(Base64.decodeBase64(
|
|
164 |
| base64Token.getBytes())); |
|
165 |
| |
|
166 |
5
| String username = "";
|
|
167 |
5
| String password = "";
|
|
168 |
5
| int delim = token.indexOf(":");
|
|
169 |
| |
|
170 |
5
| if (delim != -1) {
|
|
171 |
4
| username = token.substring(0, delim);
|
|
172 |
4
| password = token.substring(delim + 1);
|
|
173 |
| } |
|
174 |
| |
|
175 |
| |
|
176 |
5
| Authentication existingAuth = SecurityContextHolder.getContext()
|
|
177 |
| .getAuthentication(); |
|
178 |
| |
|
179 |
5
| if ((existingAuth == null)
|
|
180 |
| || !existingAuth.getName().equals(username) |
|
181 |
| || !existingAuth.isAuthenticated()) { |
|
182 |
5
| UsernamePasswordAuthenticationToken authRequest = new UsernamePasswordAuthenticationToken(username,
|
|
183 |
| password); |
|
184 |
5
| authRequest.setDetails(new WebAuthenticationDetails(
|
|
185 |
| httpRequest, false)); |
|
186 |
| |
|
187 |
5
| Authentication authResult;
|
|
188 |
| |
|
189 |
5
| try {
|
|
190 |
5
| authResult = authenticationManager.authenticate(authRequest);
|
|
191 |
| } catch (AuthenticationException failed) { |
|
192 |
| |
|
193 |
3
| if (logger.isDebugEnabled()) {
|
|
194 |
0
| logger.debug("Authentication request for user: "
|
|
195 |
| + username + " failed: " + failed.toString()); |
|
196 |
| } |
|
197 |
| |
|
198 |
3
| SecurityContextHolder.getContext().setAuthentication(null);
|
|
199 |
3
| authenticationEntryPoint.commence(request, response, failed);
|
|
200 |
| |
|
201 |
3
| return;
|
|
202 |
| } |
|
203 |
| |
|
204 |
| |
|
205 |
2
| if (logger.isDebugEnabled()) {
|
|
206 |
0
| logger.debug("Authentication success: "
|
|
207 |
| + authResult.toString()); |
|
208 |
| } |
|
209 |
| |
|
210 |
2
| SecurityContextHolder.getContext().setAuthentication(authResult);
|
|
211 |
| } |
|
212 |
| } |
|
213 |
| |
|
214 |
4
| chain.doFilter(request, response);
|
|
215 |
| } |
|
216 |
| |
|
217 |
7
| public void init(FilterConfig arg0) throws ServletException {}
|
|
218 |
| } |