|
|
|
@ -2,12 +2,8 @@ package org.springframework.security.ui.preauth; |
|
|
|
|
|
|
|
|
|
|
|
import java.io.IOException; |
|
|
|
import java.io.IOException; |
|
|
|
|
|
|
|
|
|
|
|
import javax.servlet.Filter; |
|
|
|
|
|
|
|
import javax.servlet.FilterChain; |
|
|
|
import javax.servlet.FilterChain; |
|
|
|
import javax.servlet.FilterConfig; |
|
|
|
|
|
|
|
import javax.servlet.ServletException; |
|
|
|
import javax.servlet.ServletException; |
|
|
|
import javax.servlet.ServletRequest; |
|
|
|
|
|
|
|
import javax.servlet.ServletResponse; |
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
import javax.servlet.http.HttpServletResponse; |
|
|
|
|
|
|
|
|
|
|
|
@ -19,6 +15,7 @@ import org.springframework.security.event.authentication.InteractiveAuthenticati |
|
|
|
import org.springframework.security.ui.AuthenticationDetailsSource; |
|
|
|
import org.springframework.security.ui.AuthenticationDetailsSource; |
|
|
|
import org.springframework.security.ui.AuthenticationDetailsSourceImpl; |
|
|
|
import org.springframework.security.ui.AuthenticationDetailsSourceImpl; |
|
|
|
import org.springframework.security.ui.AbstractProcessingFilter; |
|
|
|
import org.springframework.security.ui.AbstractProcessingFilter; |
|
|
|
|
|
|
|
import org.springframework.security.ui.SpringSecurityFilter; |
|
|
|
import org.springframework.security.context.SecurityContextHolder; |
|
|
|
import org.springframework.security.context.SecurityContextHolder; |
|
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.logging.Log; |
|
|
|
import org.apache.commons.logging.Log; |
|
|
|
@ -29,16 +26,16 @@ import org.springframework.context.ApplicationEventPublisherAware; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
import org.springframework.util.Assert; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Base class for processing filters that handle pre-authenticated |
|
|
|
* Base class for processing filters that handle pre-authenticated authentication requests. Subclasses must implement |
|
|
|
* authentication requests. Subclasses must implement the |
|
|
|
* the getPreAuthenticatedPrincipal() and getPreAuthenticatedCredentials() methods. |
|
|
|
* getPreAuthenticatedPrincipal() and getPreAuthenticatedCredentials() methods. |
|
|
|
* |
|
|
|
* <p> |
|
|
|
* @author Ruud Senden |
|
|
|
* This code is partly based on |
|
|
|
* @since 2.0 |
|
|
|
* {@link org.springframework.security.ui.x509.X509ProcessingFilter}. |
|
|
|
|
|
|
|
* </p> |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public abstract class AbstractPreAuthenticatedProcessingFilter implements Filter, InitializingBean, ApplicationEventPublisherAware { |
|
|
|
public abstract class AbstractPreAuthenticatedProcessingFilter extends SpringSecurityFilter implements |
|
|
|
private static final Log LOG = LogFactory.getLog(AbstractPreAuthenticatedProcessingFilter.class); |
|
|
|
InitializingBean, ApplicationEventPublisherAware { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private static final Log LOG = LogFactory.getLog(AbstractPreAuthenticatedProcessingFilter.class); |
|
|
|
|
|
|
|
|
|
|
|
private ApplicationEventPublisher eventPublisher = null; |
|
|
|
private ApplicationEventPublisher eventPublisher = null; |
|
|
|
|
|
|
|
|
|
|
|
@ -54,37 +51,21 @@ public abstract class AbstractPreAuthenticatedProcessingFilter implements Filter |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Try to authenticate a pre-authenticated user with Acegi if the user has |
|
|
|
* Try to authenticate a pre-authenticated user with Spring Security if the user has not yet been authenticated. |
|
|
|
* not yet been authenticated. |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain) throws IOException, ServletException { |
|
|
|
public void doFilterHttp(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws IOException, ServletException { |
|
|
|
if (!(request instanceof HttpServletRequest)) { |
|
|
|
|
|
|
|
throw new ServletException("Can only process HttpServletRequest"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (!(response instanceof HttpServletResponse)) { |
|
|
|
|
|
|
|
throw new ServletException("Can only process HttpServletResponse"); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
HttpServletRequest httpRequest = (HttpServletRequest) request; |
|
|
|
|
|
|
|
HttpServletResponse httpResponse = (HttpServletResponse) response; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (LOG.isDebugEnabled()) { |
|
|
|
if (LOG.isDebugEnabled()) { |
|
|
|
LOG.debug("Checking secure context token: " + SecurityContextHolder.getContext().getAuthentication()); |
|
|
|
LOG.debug("Checking secure context token: " + SecurityContextHolder.getContext().getAuthentication()); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (SecurityContextHolder.getContext().getAuthentication() == null) { |
|
|
|
if (SecurityContextHolder.getContext().getAuthentication() == null) { |
|
|
|
doAuthenticate(httpRequest, httpResponse); |
|
|
|
doAuthenticate(request, response); |
|
|
|
} |
|
|
|
} |
|
|
|
filterChain.doFilter(request, response); |
|
|
|
filterChain.doFilter(request, response); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* Do the actual authentication for a pre-authenticated user. |
|
|
|
* Do the actual authentication for a pre-authenticated user. |
|
|
|
* |
|
|
|
|
|
|
|
* @param httpRequest |
|
|
|
|
|
|
|
* The HttpServletRequest object |
|
|
|
|
|
|
|
* @param httpResponse |
|
|
|
|
|
|
|
* The HttpServletResponse object |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
private void doAuthenticate(HttpServletRequest httpRequest, HttpServletResponse httpResponse) { |
|
|
|
private void doAuthenticate(HttpServletRequest httpRequest, HttpServletResponse httpResponse) { |
|
|
|
Authentication authResult = null; |
|
|
|
Authentication authResult = null; |
|
|
|
@ -126,9 +107,10 @@ public abstract class AbstractPreAuthenticatedProcessingFilter implements Filter |
|
|
|
* when authentication fails. |
|
|
|
* when authentication fails. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) { |
|
|
|
protected void unsuccessfulAuthentication(HttpServletRequest request, HttpServletResponse response, AuthenticationException failed) { |
|
|
|
SecurityContextHolder.getContext().setAuthentication(null); |
|
|
|
SecurityContextHolder.clearContext(); |
|
|
|
if (LOG.isDebugEnabled()) { |
|
|
|
|
|
|
|
LOG.debug("Updated SecurityContextHolder to contain null Authentication due to exception", failed); |
|
|
|
if (LOG.isDebugEnabled()) { |
|
|
|
|
|
|
|
LOG.debug("Cleared security context due to exception", failed); |
|
|
|
} |
|
|
|
} |
|
|
|
request.getSession().setAttribute(AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY, failed); |
|
|
|
request.getSession().setAttribute(AbstractProcessingFilter.SPRING_SECURITY_LAST_EXCEPTION_KEY, failed); |
|
|
|
} |
|
|
|
} |
|
|
|
@ -158,18 +140,6 @@ public abstract class AbstractPreAuthenticatedProcessingFilter implements Filter |
|
|
|
this.authenticationManager = authenticationManager; |
|
|
|
this.authenticationManager = authenticationManager; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Required method, does nothing. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public void init(FilterConfig filterConfig) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Required method, does nothing. |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
public void destroy() { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
protected abstract Object getPreAuthenticatedPrincipal(HttpServletRequest httpRequest); |
|
|
|
protected abstract Object getPreAuthenticatedPrincipal(HttpServletRequest httpRequest); |
|
|
|
|
|
|
|
|
|
|
|
protected abstract Object getPreAuthenticatedCredentials(HttpServletRequest httpRequest); |
|
|
|
protected abstract Object getPreAuthenticatedCredentials(HttpServletRequest httpRequest); |
|
|
|
|