Ошибка нулевого указателя сеанса Spring Security в onExpiredSessionDetected - PullRequest
0 голосов
/ 16 апреля 2020

Я добавил в свой веб-приложение реестр сеансов Spring Security для регистрации и ведения информации о сеансах.

Я хочу ограничить количество возможных сеансов для пользователя только 2. Я считаю, что так, как я его настроил как только кто-то регистрируется в третий раз, истекает срок действия наименее использованного сеанса (исключение ifMaximumExceeded равно false).

Я использую следующую конфигурацию в моем applicationContext. xml:

<beans:bean id="concurrencyFilter" class="org.springframework.security.web.session.ConcurrentSessionFilter"> 
    <beans:constructor-arg name="sessionRegistry" ref="sessionRegistry" /> 
    <beans:constructor-arg name="expiredUrl" value="/login" /> 
</beans:bean>

<beans:bean id="compositeSessionAuthenticationStrategy"
    class="org.springframework.security.web.authentication.session.CompositeSessionAuthenticationStrategy">
    <beans:constructor-arg>
        <beans:list>
            <beans:ref bean="registerSessionAuthenticationStrategy"/>
            <beans:ref bean="concurrentSessionControlStrategy"/>
        </beans:list>
    </beans:constructor-arg>
</beans:bean>

<beans:bean id="registerSessionAuthenticationStrategy"
  class="org.springframework.security.web.authentication.session.RegisterSessionAuthenticationStrategy">
    <beans:constructor-arg ref="sessionRegistry"/>
</beans:bean>

<beans:bean id="concurrentSessionControlStrategy"
  class="org.springframework.security.web.authentication.session.ConcurrentSessionControlAuthenticationStrategy">
    <beans:constructor-arg ref="sessionRegistry"/>
    <beans:property name="exceptionIfMaximumExceeded" value="false"/>
    <beans:property name="maximumSessions" value="2"/>
</beans:bean>

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

Но при нулевом указателе выдается osswsConcurrentSessionFilter method onExpiredSessionDetected , когда кто-то входит в систему в третий раз. В моей конфигурации чего-то не хватает, чтобы это предотвратить?

logback-test 2020-04-16 09:08:42,758 [https-jsse-nio-8443-exec-10] [39mDEBUG[0;39m o.s.s.w.s.ConcurrentSessionFilter - Requested session ID EBB664413F11D4510D7C780D982818BF has expired.
logback-test 2020-04-16 09:08:42,758 [https-jsse-nio-8443-exec-10] [39mDEBUG[0;39m o.s.s.w.a.l.SecurityContextLogoutHandler - Invalidating session: EBB664413F11D4510D7C780D982818BF
logback-test 2020-04-16 09:08:42,759 [https-jsse-nio-8443-exec-10] [39mDEBUG[0;39m o.s.s.w.s.HttpSessionEventPublisher - Publishing event: org.springframework.security.web.session.HttpSessionDestroyedEvent[source=org.apache.catalina.session.StandardSessionFacade@46615274]
logback-test 2020-04-16 09:08:42,759 [https-jsse-nio-8443-exec-10] [39mDEBUG[0;39m o.s.s.c.s.SessionRegistryImpl - Removing session EBB664413F11D4510D7C780D982818BF from principal's set of registered sessions
logback-test 2020-04-16 09:08:42,759 [https-jsse-nio-8443-exec-10] [39mDEBUG[0;39m o.s.s.w.c.HttpSessionSecurityContextRepository - SecurityContext is empty or contents are anonymous - context will not be stored in HttpSession.
logback-test 2020-04-16 09:08:42,759 [https-jsse-nio-8443-exec-10] [39mDEBUG[0;39m o.s.s.w.c.SecurityContextPersistenceFilter - SecurityContextHolder now cleared, as request processing completed
Apr 16, 2020 9:08:42 AM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [MyProfileServlet] in context with path [] threw exception
java.lang.NullPointerException
    at org.springframework.security.web.session.ConcurrentSessionFilter$1.onExpiredSessionDetected(ConcurrentSessionFilter.java:107)
    at org.springframework.security.web.session.ConcurrentSessionFilter.doFilter(ConcurrentSessionFilter.java:145)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)

1 Ответ

0 голосов
/ 19 апреля 2020

Если кто-нибудь столкнется с этой проблемой, я найду решение в разделе Управление параллелизмом документации Spring на https://docs.spring.io/spring-security/site/docs/current/reference/html5/#concurrent -сессии .

В частности, Мне нужно было добавить аргумент sessionInformationExpiredStrategy (создание экземпляра SimpleRedirectSessionInformationExpiredStrategy) в мой concurrentSessionFilter , заменив аргумент expiredUrl , который Я был там.

Не знаю, откуда взялся аргумент expiredUrl , как это было в прежнем xml, с которым я работал. Возможно, он работал с предыдущей версией Spring Security.

<beans:bean id="redirectSessionInformationExpiredStrategy"
  class="org.springframework.security.web.session.SimpleRedirectSessionInformationExpiredStrategy">
    <beans:constructor-arg name="invalidSessionUrl" value="/login.jsp" />
</beans:bean>

<beans:bean id="concurrencyFilter" class="org.springframework.security.web.session.ConcurrentSessionFilter"> 
    <beans:constructor-arg name="sessionRegistry" ref="sessionRegistry" /> 
    <beans:constructor-arg name="sessionInformationExpiredStrategy" ref="redirectSessionInformationExpiredStrategy" />
</beans:bean>
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...