1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package waffle.spring;
18
19 import java.io.IOException;
20
21 import javax.servlet.ServletException;
22 import javax.servlet.http.HttpServletRequest;
23 import javax.servlet.http.HttpServletResponse;
24
25 import org.slf4j.Logger;
26 import org.slf4j.LoggerFactory;
27 import org.springframework.security.access.AccessDeniedException;
28 import org.springframework.security.authentication.AuthenticationManager;
29 import org.springframework.security.core.Authentication;
30 import org.springframework.security.core.AuthenticationException;
31 import org.springframework.security.core.context.SecurityContextHolder;
32 import org.springframework.security.web.access.AccessDeniedHandler;
33 import org.springframework.security.web.authentication.AuthenticationFailureHandler;
34 import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
35
36
37
38
39
40
41
42
43
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 public class DelegatingNegotiateSecurityFilter extends NegotiateSecurityFilter {
84
85
86 private static final Logger LOGGER = LoggerFactory.getLogger(NegotiateSecurityFilter.class);
87
88
89 private AuthenticationManager authenticationManager;
90
91
92 private AuthenticationSuccessHandler authenticationSuccessHandler;
93
94
95 private AuthenticationFailureHandler authenticationFailureHandler;
96
97
98 private AccessDeniedHandler accessDeniedHandler;
99
100
101
102
103
104
105 public AccessDeniedHandler getAccessDeniedHandler() {
106 return this.accessDeniedHandler;
107 }
108
109
110
111
112
113
114
115 public void setAccessDeniedHandler(final AccessDeniedHandler accessDeniedHandler) {
116 this.accessDeniedHandler = accessDeniedHandler;
117 }
118
119
120
121
122
123
124 public AuthenticationFailureHandler getAuthenticationFailureHandler() {
125 return this.authenticationFailureHandler;
126 }
127
128
129
130
131
132
133
134 public void setAuthenticationFailureHandler(final AuthenticationFailureHandler authenticationFailureHandler) {
135 this.authenticationFailureHandler = authenticationFailureHandler;
136 }
137
138
139
140
141 public DelegatingNegotiateSecurityFilter() {
142 super();
143 DelegatingNegotiateSecurityFilter.LOGGER.debug("[waffle.spring.NegotiateSecurityFilter] loaded");
144 }
145
146
147
148
149 @Override
150 protected boolean setAuthentication(final HttpServletRequest request, final HttpServletResponse response,
151 final Authentication authentication) {
152 try {
153 if (this.authenticationManager != null) {
154 this.logger.debug("Delegating to custom authenticationmanager");
155 final Authentication customAuthentication = this.authenticationManager.authenticate(authentication);
156 SecurityContextHolder.getContext().setAuthentication(customAuthentication);
157 }
158 if (this.authenticationSuccessHandler != null) {
159 try {
160 this.authenticationSuccessHandler.onAuthenticationSuccess(request, response, authentication);
161 } catch (final IOException e) {
162 this.logger.warn("Error calling authenticationSuccessHandler: " + e.getMessage());
163 return false;
164 } catch (final ServletException e) {
165 this.logger.warn("Error calling authenticationSuccessHandler: " + e.getMessage());
166 return false;
167 }
168 }
169 } catch (final AuthenticationException e) {
170
171 this.logger.warn("Error authenticating user in custom authenticationmanager: " + e.getMessage());
172 this.sendAuthenticationFailed(request, response, e);
173 return false;
174 } catch (final AccessDeniedException e) {
175 this.logger.warn("Error authorizing user in custom authenticationmanager: " + e.getMessage());
176 this.sendAccessDenied(request, response, e);
177 return false;
178 }
179 return true;
180 }
181
182
183
184
185 @Override
186 public void afterPropertiesSet() throws ServletException {
187 super.afterPropertiesSet();
188
189 if (this.getProvider() == null) {
190 throw new ServletException("Missing NegotiateSecurityFilter.Provider");
191 }
192 }
193
194
195
196
197
198
199
200
201
202
203
204 private void sendAuthenticationFailed(final HttpServletRequest request, final HttpServletResponse response,
205 final AuthenticationException ae) {
206 if (this.authenticationFailureHandler != null) {
207 try {
208 this.authenticationFailureHandler.onAuthenticationFailure(request, response, ae);
209 return;
210 } catch (final IOException e) {
211 DelegatingNegotiateSecurityFilter.LOGGER.warn("IOException invoking authenticationFailureHandler: " + e.getMessage());
212 } catch (final ServletException e) {
213 DelegatingNegotiateSecurityFilter.LOGGER.warn("ServletException invoking authenticationFailureHandler: " + e.getMessage());
214 }
215 }
216 super.sendUnauthorized(response, true);
217 }
218
219
220
221
222
223
224
225
226
227
228
229 private void sendAccessDenied(final HttpServletRequest request, final HttpServletResponse response,
230 final AccessDeniedException ae) {
231 if (this.accessDeniedHandler != null) {
232 try {
233 this.accessDeniedHandler.handle(request, response, ae);
234 return;
235 } catch (final IOException e) {
236 DelegatingNegotiateSecurityFilter.LOGGER.warn("IOException invoking accessDeniedHandler: " + e.getMessage());
237 } catch (final ServletException e) {
238 DelegatingNegotiateSecurityFilter.LOGGER.warn("ServletException invoking accessDeniedHandler: " + e.getMessage());
239 }
240 }
241
242 this.sendUnauthorized(response, true);
243 }
244
245
246
247
248
249
250 public AuthenticationSuccessHandler getAuthenticationSuccessHandler() {
251 return this.authenticationSuccessHandler;
252 }
253
254
255
256
257
258
259
260 public void setAuthenticationSuccessHandler(final AuthenticationSuccessHandler authenticationSuccessHandler) {
261 this.authenticationSuccessHandler = authenticationSuccessHandler;
262 }
263
264
265
266
267
268
269 public AuthenticationManager getAuthenticationManager() {
270 return this.authenticationManager;
271 }
272
273
274
275
276
277
278
279 public void setAuthenticationManager(final AuthenticationManager authenticationManager) {
280 this.authenticationManager = authenticationManager;
281 }
282
283 }