1 | /* Copyright 2006-2009 the original author or authors. |
---|
2 | * |
---|
3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
---|
4 | * you may not use this file except in compliance with the License. |
---|
5 | * You may obtain a copy of the License at |
---|
6 | * |
---|
7 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
8 | * |
---|
9 | * Unless required by applicable law or agreed to in writing, software |
---|
10 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
12 | * See the License for the specific language governing permissions and |
---|
13 | * limitations under the License. |
---|
14 | */ |
---|
15 | package org.codehaus.groovy.grails.plugins.springsecurity |
---|
16 | |
---|
17 | import javax.servlet.FilterChain |
---|
18 | import javax.servlet.ServletException |
---|
19 | import javax.servlet.http.HttpServletRequest |
---|
20 | import javax.servlet.http.HttpServletResponse |
---|
21 | |
---|
22 | import org.springframework.security.Authentication |
---|
23 | import org.springframework.security.AuthenticationException |
---|
24 | import org.springframework.security.ui.webapp.AuthenticationProcessingFilter |
---|
25 | |
---|
26 | /** |
---|
27 | * Extends the default {@link AuthenticationProcessingFilter} to override the <code>sendRedirect()</code> |
---|
28 | * logic and always send absolute redirects. |
---|
29 | * |
---|
30 | * @author Tsuyoshi Yamamoto |
---|
31 | */ |
---|
32 | class GrailsAuthenticationProcessingFilter extends AuthenticationProcessingFilter { |
---|
33 | |
---|
34 | /** |
---|
35 | * Dependency injection for the authentication service. |
---|
36 | */ |
---|
37 | def authenticateService |
---|
38 | |
---|
39 | /** |
---|
40 | * Dependency injection for the Ajax auth fail url. |
---|
41 | */ |
---|
42 | String ajaxAuthenticationFailureUrl |
---|
43 | |
---|
44 | /** |
---|
45 | * {@inheritDoc} |
---|
46 | * @see org.springframework.security.ui.AbstractProcessingFilter#doFilterHttp( |
---|
47 | * javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, |
---|
48 | * javax.servlet.FilterChain) |
---|
49 | */ |
---|
50 | @Override |
---|
51 | void doFilterHttp(HttpServletRequest request, HttpServletResponse response, |
---|
52 | FilterChain chain) throws IOException, ServletException { |
---|
53 | |
---|
54 | SecurityRequestHolder.set request, response |
---|
55 | try { |
---|
56 | super.doFilterHttp(request, response, chain) |
---|
57 | } |
---|
58 | finally { |
---|
59 | SecurityRequestHolder.reset() |
---|
60 | } |
---|
61 | } |
---|
62 | |
---|
63 | /** |
---|
64 | * {@inheritDoc} |
---|
65 | * @see org.springframework.security.ui.AbstractProcessingFilter#sendRedirect( |
---|
66 | * javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse, |
---|
67 | * java.lang.String) |
---|
68 | */ |
---|
69 | @Override |
---|
70 | protected void sendRedirect( |
---|
71 | HttpServletRequest request, |
---|
72 | HttpServletResponse response, |
---|
73 | String url) throws IOException { |
---|
74 | RedirectUtils.sendRedirect(request, response, url); |
---|
75 | } |
---|
76 | |
---|
77 | /** |
---|
78 | * {@inheritDoc} |
---|
79 | * @see org.springframework.security.ui.AbstractProcessingFilter#determineFailureUrl( |
---|
80 | * javax.servlet.http.HttpServletRequest, org.springframework.security.AuthenticationException) |
---|
81 | */ |
---|
82 | @Override |
---|
83 | protected String determineFailureUrl(HttpServletRequest request, AuthenticationException failed) { |
---|
84 | String url = super.determineFailureUrl(request, failed) |
---|
85 | if (url == authenticationFailureUrl && authenticateService.isAjax(request)) { |
---|
86 | url = ajaxAuthenticationFailureUrl ?: authenticationFailureUrl |
---|
87 | } |
---|
88 | return url |
---|
89 | } |
---|
90 | } |
---|