【问题标题】:Custom Spring Security Logout Filter自定义 Spring Security 注销过滤器
【发布时间】:2016-01-13 03:02:51
【问题描述】:

我需要在我的 spring security 3.0.5 Web 应用程序中取消对用户的身份验证(终止他们的会话),然后将重定向发送到另一个站点以通知他们注销。这在春季是否可行?如果可以,执行这些任务的一般方法是什么?谢谢!

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");

}

}

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)

【问题讨论】:

    标签: spring spring-security


    【解决方案1】:

    实际上,标记的“正确答案”是关于设置自定义logout success-handler,而不是LogoutFilter,正如所定义的那样。

    所以,如果有人想创建一个自定义的注销过滤器,这里有一个 sn-p:

    <bean id="securityContextLogoutHandler" class="org.springframework.security.web.authentication.logout.SecurityContextLogoutHandler"/>
    <bean id="logoutFilter" class="org.springframework.security.web.authentication.logout.LogoutFilter">
        <property name="filterProcessesUrl" value="/logout"/>
        <constructor-arg index="0" value="/"/>
        <constructor-arg index="1">
            <list>
                <ref bean="securityContextLogoutHandler"/>
                <!--ref bean="myLogoutHandler"/-->
            </list>
        </constructor-arg>
    </bean>
    

    这是一个默认过滤器类,带有一个默认的预定义处理程序(这个无效会话)。 如果您确实需要自定义注销过滤器,那么您应该更改此标准行为(子类化此或使用相同的接口编写您自己的)。 也别忘了注册:

        <security:http>
    ....
            <custom-filter position="LOGOUT_FILTER" ref="logoutFilter"/>
        </security:http>
    

    更新: 在阅读了一些 spring 代码后,我发现还有一个默认的注销处理程序 - RememberMeServices,使用接口 AbstractRememberMeServices implements LogoutHandler 定义。因此,如果您正在使用 RememberMeServices 并想编写一个包含 RememberMe 支持的自定义过滤器,您还需要在注销处理程序列表中添加对您的 RememberMeServices 的引用。

    【讨论】:

      【解决方案2】:

      继承 SimpleUrlLogoutSuccessHandler 并覆盖 onLogoutSuccess() 以进行重定向。

      配置注销成功处理程序,如:

      <http>
        ...
        <logout success-handler-ref="myLogoutSuccessHandler"/>
      </http>
      

      【讨论】:

      • 关于为什么我会在重定向中获得非法状态的任何想法?
      • 这通常意味着在重定向之前发送了一些输出。
      • IllegalStateException 是由两次调用重定向引起的。 response.sendRedirect("https://secure05.pilot.principal.com/shared/members/corp/LogoutServlet");response.sendRedirect("/dreamcatcher/auth/login.html");
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-23
      • 2014-07-28
      • 2013-07-22
      • 1970-01-01
      • 2014-10-24
      • 2011-07-23
      • 2018-05-25
      相关资源
      最近更新 更多