2014-01-29 9 views
3

У меня есть весенний mvc webapp с использованием весенней безопасности, и одна вещь, которую я хотел бы делать каждый раз, когда пользователь входит в систему, регистрирует количество одновременных пользователей в системе.Весенняя сессия сеанса безопасности безопасности

Для этого я дал моей сессии реестре псевдоним, а затем я autowire в класс и сказать ...

List<Object> principals = sessionRegistry.getAllPrincipals(); 
MDC.put(MDCKeyConstants.CONCURRENT_USER_COUNT, principals.size()); 

но principals.size подходит к 0. т. е. список участников пуст. Я пропустил что-то еще, что мне нужно настроить?

Извините за длинный пост, но я ставлю свою весеннюю конфигурации безопасности здесь, чтобы попытаться получить некоторую помощь с вопросом .. спасибо заранее ...

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

    <!-- 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-registry-alias="sessionRegistry"/> 

    </session-management> 

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

    <!-- logout --> 
    <logout logout-success-url="/logout.htm" invalidate-session="false" 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> 

<!-- =============================== --> 
<!--  AUTHENTICATION BEANS  --> 
<!-- =============================== --> 

<beans:bean id="authenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider"> 
    <beans:property name="userDetailsService" ref="userDetailsDao" /> 
    <beans:property name="passwordEncoder" ref="encoder" /> 
</beans:bean> 

<beans:bean id="twoFactorAuthenticationFilter" class="com.mycompany.reporting.security.TwoFactorAuthenticationFilter"> 
    <beans:property name="authenticationManager" ref="authenticationManager" /> 
    <beans:property name="authenticationFailureHandler" ref="failureHandler" /> 
    <beans:property name="authenticationSuccessHandler" ref="successHandler" />   
    <beans:property name="filterProcessesUrl" value="/j_spring_security_check" /> 
    <beans:property name="postOnly" value="true" /> 
</beans:bean> 

<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.mycompany.reporting.security.CustomSavedRequestAwareAuthenticationSuccessHandler"> 
    <beans:property name="defaultTargetUrl" value="/home.htm" /> 
</beans:bean> 

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

<authentication-manager alias="authenticationManager"> 
    <authentication-provider ref="authenticationProvider"></authentication-provider> 
</authentication-manager> 
+0

Вы упомянули прослушивание HttpSessionEventPublisher в вашей веб-среде. Xml –

+0

да у меня есть. org.springframework.security.web.session.HttpSessionEventPublisher – Richie

+0

попробуйте указать в spring-security.xml –

ответ

1

Попробуйте это. Это сработало для меня.

в <http></http>,

<session-management session-authentication-strategy-ref="sas" invalid-session-url="/invalid-session" />

И объявите бобы следующим образом:

<beans:bean id="sessionRegistry" class="org.springframework.security.core.session.SessionRegistryImpl"/>

<beans:bean id="sas" class="org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy"> <beans:constructor-arg name="sessionRegistry" ref="sessionRegistry" /> <beans:property name="maximumSessions" value="1" /> </beans:bean>

И не забудьте добавить org.springframework.security.web. session.HttpSessionEventPublisher для прослушивания в Интернете. конфигурации.

+0

спасибо Hardik. Я действительно нашел этот ответ некоторое время назад, но я могу подтвердить, что вы указали именно то, что я исправил эту проблему. – Richie

+0

Имя класса может быть изменено на org.springframework.security.web.authentication.session.ConcurrentSessionControlAuthenticationStrategy – Stephane

+0

это не работает для меня !! – M2E67

Смежные вопросы