Spring安全会话的行为不符合预期

时间:2014-01-28 05:41:20

标签: spring-mvc spring-security

我刚刚按照http://www.mkyong.com/servlet/a-simple-httpsessionlistener-example-active-sessions-counter/在我的spring mvc webapp中实现了SessionCounterListener。

我看到一些我没想到的行为,并且有两个问题。

问题1。 当我在服务器重启后第一次点击我的login.jsp时,甚至在我登录之前会话计数器为1(不是预期的)。

例如,当我进入登录页面时...... sessionCreated - 在计数器中添加一个会话:1

然后当我按下退出按钮时,会话计数减少一个(这很好)但是会话计数增加1之后立即(不是预期的)。

例如,当我点击退出按钮时...... sessionDestroyed - 从计数器中扣除一个会话:0 sessionCreated - 在计数器中添加一个会话:1

就好像每次进入登录页面时,计数都会增加1.我希望只有在成功登录后才会增加计数。

有人可以帮我理解这里发生的事情吗?

问题2。 当我在我的应用程序中以其他用户身份登录而第一个用户仍然登录时,我没有得到新的会话计数器。即我不认为它没有为新用户创建新会话。

我再次需要帮助才能理解。

这是我的春季安全设置......

<http pattern="/login.htm" security="none"/>
<http use-expressions="true" auto-config="false" entry-point-ref="loginUrlAuthenticationEntryPoint">    
    <!-- custom filters -->
    <custom-filter position="FORM_LOGIN_FILTER" ref="twoFactorAuthenticationFilter" />
    <custom-filter ref="securityLoggingFilter" after="SECURITY_CONTEXT_FILTER"/>

    <!-- session management --> 
    <session-management 
        invalid-session-url="/sessionExpired.htm" 
        session-authentication-error-url="/alreadyLoggedIn.htm">

        <concurrency-control 
            max-sessions="1" 
            expired-url="/sessionExpiredDuplicateLogin.htm" 
            error-if-maximum-exceeded="false" />
    </session-management>   

    <!-- error handlers -->
    <access-denied-handler error-page="/accessDenied.htm"/>             

    <!-- logout --> 
    <logout logout-success-url="/logout.htm" invalidate-session="true" delete-cookies="JSESSIONID" />   

    <!-- authorize pages -->    
    <intercept-url pattern="/home.htm" access="isAuthenticated()" />
    <intercept-url pattern="/shortsAndOvers.htm" access="isAuthenticated()" />
    <intercept-url pattern="/shortsAndOversDaily.htm" access="isAuthenticated()" />
    <intercept-url pattern="/birtpage.htm" access="isAuthenticated()" />
    <intercept-url pattern="/reports/show.htm" access="isAuthenticated()" />    

</http>

<beans:bean id="loginUrlAuthenticationEntryPoint" class="org.springframework.security.web.authentication.LoginUrlAuthenticationEntryPoint">
    <beans:property name="loginFormUrl" value="/login.htm" />
</beans:bean>

<beans:bean id="successHandler" class="com.me.reporting.security.CustomSavedRequestAwareAuthenticationSuccessHandler">
    <beans:property name="defaultTargetUrl" value="/home.htm" />
</beans:bean>

<beans:bean id="failureHandler" class="com.me.reporting.security.CustomSimpleUrlAuthenticationFailureHandler">
    <beans:property name="defaultFailureUrl" value="/loginfailed.htm" />
</beans:bean>  

1 个答案:

答案 0 :(得分:1)

你可能正在考虑不同背景下的会话。

在点击你的应用程序时查看firebug中的JSESSION cookie,也许它会给你一些答案:)

每次JSESSIONID更改时都可能会调用HttpSessionListener,因此:

  1. 你进入login.jsp页面 - 创建会话,JSESSIONID更改,调用sessionCreated。你是登出:调用sessionDestroyed,会话失效(这里不确定,只是猜测),计数器减少。但登出后会发生什么?您可能被重定向到/logout.htm并创建了新会话。