title: Custom Spring Security Logout Filter
body: <p>I need to de-authenticate a user (kill their session) within my spring security 3.0.5 web app and then send a redirect to another site to notify them of the logout.  Is this possible within spring and if so what is the general approach to performing these tasks?  Thanks!</p>
  
  <pre><code>import org.springframework.security.core.Authentication;
  import org.springframework.security.web.authentication.logout.SimpleUrlLogoutSuccessHandler;
  
  import com.dc.api.model.Users;
  
  public class DCSimpleUrlLogoutSuccessHandler extends SimpleUrlLogoutSuccessHandler{
  
      public void onLogoutSuccess(javax.servlet.http.HttpServletRequest request, 
              javax.servlet.http.HttpServletResponse response,
              Authentication authentication)
       throws java.io.IOException,
              javax.servlet.ServletException{
              Users user=null;
              Object principal = authentication.getPrincipal();
              if (principal instanceof Users) {
                  user = (Users) principal;
                  if(user.getType().equals(TEST)){
                      response.sendRedirect("LogoutServlet");
                  }
              }
              response.sendRedirect("login.html");
  
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>java.lang.IllegalStateException
      org.apache.catalina.connector.ResponseFacade.sendRedirect(ResponseFacade.java:463)
      javax.servlet.http.HttpServletResponseWrapper.sendRedirect(HttpServletResponseWrapper.java:138)
      org.springframework.security.web.context.SaveContextOnUpdateOrErrorResponseWrapper.sendRedirect(SaveContextOnUpdateOrErrorResponseWrapper.java:74)
      com.dc.api.service.impl.DCSimpleUrlLogoutSuccessHandler.onLogoutSuccess(DCSimpleUrlLogoutSuccessHandler.java:24)
      org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:100)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
      org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:79)
      org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:380)
      org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:169)
      org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:237)
      org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:167)
  </code></pre>