| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||||||
| HttpRequestIntegrationFilter |
|
| 2.6666666666666665;2.667 |
| 1 | /* Copyright 2004, 2005, 2006 Acegi Technology Pty Limited |
|
| 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 | ||
| 16 | package org.acegisecurity.adapters; |
|
| 17 | ||
| 18 | import org.acegisecurity.Authentication; |
|
| 19 | ||
| 20 | import org.acegisecurity.context.SecurityContextHolder; |
|
| 21 | ||
| 22 | import org.apache.commons.logging.Log; |
|
| 23 | import org.apache.commons.logging.LogFactory; |
|
| 24 | ||
| 25 | import java.io.IOException; |
|
| 26 | ||
| 27 | import java.security.Principal; |
|
| 28 | ||
| 29 | import javax.servlet.Filter; |
|
| 30 | import javax.servlet.FilterChain; |
|
| 31 | import javax.servlet.FilterConfig; |
|
| 32 | import javax.servlet.ServletException; |
|
| 33 | import javax.servlet.ServletRequest; |
|
| 34 | import javax.servlet.ServletResponse; |
|
| 35 | import javax.servlet.http.HttpServletRequest; |
|
| 36 | ||
| 37 | ||
| 38 | /** |
|
| 39 | * Populates <code>SecurityContext</code> with the <code>Authentication</code> obtained from the container's |
|
| 40 | * <code>HttpServletRequest.getUserPrincipal()</code>.<p>Use this filter with container adapters only.</p> |
|
| 41 | * <p>This filter <b>never</b> preserves the <code>Authentication</code> on the <code>SecurityContext</code> - it |
|
| 42 | * is replaced every request.</p> |
|
| 43 | * <p>See {@link org.acegisecurity.context.HttpSessionContextIntegrationFilter} for further information.</p> |
|
| 44 | * |
|
| 45 | * @author Ben Alex |
|
| 46 | * @version $Id: HttpRequestIntegrationFilter.java 1496 2006-05-23 13:38:33Z benalex $ |
|
| 47 | */ |
|
| 48 | 3 | public class HttpRequestIntegrationFilter implements Filter { |
| 49 | //~ Static fields/initializers ===================================================================================== |
|
| 50 | ||
| 51 | 2 | private static final Log logger = LogFactory.getLog(HttpRequestIntegrationFilter.class); |
| 52 | ||
| 53 | //~ Methods ======================================================================================================== |
|
| 54 | ||
| 55 | /** |
|
| 56 | * Does nothing. We use IoC container lifecycle services instead. |
|
| 57 | */ |
|
| 58 | 0 | public void destroy() {} |
| 59 | ||
| 60 | public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) |
|
| 61 | throws IOException, ServletException { |
|
| 62 | 3 | if (request instanceof HttpServletRequest) { |
| 63 | 2 | Principal principal = ((HttpServletRequest) request).getUserPrincipal(); |
| 64 | ||
| 65 | 2 | if ((principal != null) && principal instanceof Authentication) { |
| 66 | 1 | SecurityContextHolder.getContext().setAuthentication((Authentication) principal); |
| 67 | ||
| 68 | 1 | if (logger.isDebugEnabled()) { |
| 69 | 0 | logger.debug("SecurityContextHolder updated with Authentication from container: '" + principal |
| 70 | + "'"); |
|
| 71 | } |
|
| 72 | } else { |
|
| 73 | 1 | if (logger.isDebugEnabled()) { |
| 74 | 0 | logger.debug("SecurityContextHolder not set with new Authentication as Principal was: '" |
| 75 | + principal + "'"); |
|
| 76 | } |
|
| 77 | } |
|
| 78 | 2 | } else { |
| 79 | 1 | throw new IllegalArgumentException("Only HttpServletRequest is acceptable"); |
| 80 | } |
|
| 81 | ||
| 82 | 2 | chain.doFilter(request, response); |
| 83 | 2 | } |
| 84 | ||
| 85 | /** |
|
| 86 | * Does nothing. We use IoC container lifecycle services instead. |
|
| 87 | * |
|
| 88 | * @param arg0 ignored |
|
| 89 | * |
|
| 90 | * @throws ServletException ignored |
|
| 91 | */ |
|
| 92 | 0 | public void init(FilterConfig arg0) throws ServletException {} |
| 93 | } |