在CustomLogoutSuccessHandler中,身份验证对象始终为null

时间:2016-01-27 12:17:43

标签: java spring spring-security

我总是在自定义logoutSuccesshandler中为Authentication对象获取null,我不确定是什么问题。 :(

这是spring-security文件:

    <sec:http auto-config="true" entry-point-ref="ssoProcessingFilterEntryPoint"
        access-decision-manager-ref="affirmativeBased">
        <sec:intercept-url pattern="/afterAuthn/**" access="${spring.security.role}" />
        <sec:intercept-url pattern="/tenancy/**" access="${spring.security.role}" />
        <!-- Add permissions to specific URLS - i.e. IAM.User.Read for /resources/** -->
        <sec:intercept-url pattern="/**"
            access="${default.permission}" />
        <sec:logout invalidate-session="true" delete-cookies="true"
            success-handler-ref="customLogoutSuccessHandler" />
        <sec:custom-filter ref="ssoAuthenticationFilter"
            position="PRE_AUTH_FILTER" />
        <sec:session-management>
            <sec:concurrency-control max-sessions="1" />
        </sec:session-management>
    </sec:http>
..
<bean id="customLogoutSuccessHandler"
        class="usermgmt.service.CustomLogoutSuccessHandler">
         <property name="defaultLogoutSuccessUrl" value="${service.provider.logout.success.url}"/>
    </bean>

我的自定义类处理程序:

public class CustomLogoutSuccessHandler implements LogoutSuccessHandler {
@Override
    public void onLogoutSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication)
            throws IOException, ServletException {
        if (authentication == null) {
            System.out.println("NULL");
        } else {
            System.out.println("NOT NULL");
        }
}}

在此处,身份验证始终为NULL,Spring安全性版本为3.1.0.RELEASE

1 个答案:

答案 0 :(得分:0)

这可能是因为您在Spring Security中配置了SecurityContextLogoutHandler。 SecurityContextLogoutHandler清除安全上下文,从而清除身份验证对象。在SecurityContextLogoutHandler之后调用您的自定义处理程序。为了保留身份验证对象,您将必须创建自定义SecurityContextLogoutHandler并在其中具有必要的逻辑。

在本课程中,您有2个选择:

  1. 您可以将身份验证对象放在可以在您的自定义注销成功处理程序中检索到的request属性中
  2. 或在您的自定义SecurityContextLogoutHandler中计算URL并将其设置为请求属性在自定义注销成功处理程序中将其检索并设置为默认目标URL