session-mgmt.xml 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <chapter xmlns="http://docbook.org/ns/docbook" version="5.0" xml:id="session-mgmt"
  2. xmlns:xlink="http://www.w3.org/1999/xlink">
  3. <info>
  4. <title>Session Management</title>
  5. </info>
  6. <para>HTTP session related functonality is handled by a combination of the
  7. <classname>SessionManagementFilter</classname> and the
  8. <interfacename>SessionAuthenticationStrategy</interfacename> interface, which the filter
  9. delegates to. Typical usage includes session-fixation protection attack prevention,
  10. detection of session timeouts and restrictions on how many sessions an authenticated user
  11. may have open concurrently.</para>
  12. <section>
  13. <title>SessionManagementFilter</title>
  14. <para>The <classname>SessionManagementFilter</classname> checks the contents of the
  15. <interfacename>SecurityContextRepository</interfacename> against the current contents of
  16. the <classname>SecurityContextHolder</classname> to determine whether a user has been
  17. authenticated during the current request, typically by a non-interactive authentication
  18. mechanism, such as pre-authentication or remember-me <footnote>
  19. <para>Authentication by mechanisms which perform a redirect after authenticating (such
  20. as form-login) will not be detected by
  21. <classname>SessionManagementFilter</classname>, as the filter will not be invoked
  22. during the authenticating request. Session-management functionality has to be
  23. handled separately in these cases. </para>
  24. </footnote>. If the repository contains a security context, the filter does nothing. If
  25. it doesn't, and the thread-local <interfacename>SecurityContext</interfacename> contains
  26. a (non-anonymous) <interfacename>Authentication</interfacename> object, the filter
  27. assumes they have been authenticated by a previous filter in the stack. It will then
  28. invoke the configured
  29. <interfacename>SessionAuthenticationStrategy</interfacename>.</para>
  30. <para>If the user is not currently authenticated, the filter will check whether an invalid
  31. session ID has been requested (because of a timeout, for example) and will redirect to
  32. the configured <literal>invalidSessionUrl</literal> if set. The easiest way to configure
  33. this is through the namespace, <link xlink:href="#ns-session-mgmt">as described
  34. earlier</link>.</para>
  35. </section>
  36. <section>
  37. <title><interfacename>SessionAuthenticationStrategy</interfacename></title>
  38. <para> <interfacename>SessionAuthenticationStrategy</interfacename> is used by both
  39. <classname>SessionManagementFilter</classname> and
  40. <classname>AbstractAuthenticationProcessingFilter</classname>, so if you are using a
  41. customized form-login class, for example, you will need to inject it into both of these.
  42. In this case, a typical configuration, combining the namespace and custom beans might
  43. look like this:<programlisting><![CDATA[
  44. <http>
  45. <custom-filter position="FORM_LOGIN_FILTER" ref="myAuthFilter" />
  46. <session-management session-authentication-strategy-ref="sas"/>
  47. </http>
  48. <beans:bean id="myAuthFilter" class=
  49. "org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
  50. <beans:property name="sessionAuthenticationStrategy" ref="sas" />
  51. ...
  52. </beans:bean>
  53. <beans:bean id="sas" class=
  54. "org.springframework.security.web.authentication.session.SessionFixationProtectionStrategy">
  55. <beans:property name="sessionRegistry" ref="sessionRegistry" />
  56. <beans:property name="maximumSessions" value="1" />
  57. </beans:bean>
  58. ]]>
  59. </programlisting></para>
  60. </section>
  61. <section xml:id="concurrent-sessions">
  62. <title>Concurrency Control</title>
  63. <para>Spring Security is able to prevent a principal from concurrently authenticating to the
  64. same application more than a specified number of times. Many ISVs take advantage of this
  65. to enforce licensing, whilst network administrators like this feature because it helps
  66. prevent people from sharing login names. You can, for example, stop user
  67. <quote>Batman</quote> from logging onto the web application from two different sessions.
  68. You can either expire their previous login or you can report an error when they try to
  69. log in again, preventing the second login. Note that if you are using the second
  70. approach, a user who has not explicitly logged out (but who has just closed their
  71. browser, for example) will not be able to log in again until their original session
  72. expires.</para>
  73. <para>Concurrency control is supported by the namespace, so please check the earlier
  74. namespace chapter for the simplest configuration. Sometimes you need to customize things
  75. though. </para>
  76. <para>The implementation uses a specialized version of
  77. <interfacename>SessionAuthenticationStrategy</interfacename>, called
  78. <classname>ConcurrentSessionControlStrategy</classname>. <note>
  79. <para>Previously the concurrent authentication check was made by the
  80. <classname>ProviderManager</classname>, which could be injected with a
  81. <literal>ConcurrentSessionController</literal>. The latter would check if the user
  82. was attempting to exceed the number of permitted sessions. However, this approach
  83. required that an HTTP session be created in advance, which is undesirable. In Spring
  84. Security 3, the user is first authenticated by the
  85. <interfacename>AuthenticationManager</interfacename> and once they are successfully
  86. authenticated, a session is created and the check is made whether they are allowed
  87. to have another session open.</para>
  88. </note></para>
  89. <para>To use concurrent session support, you'll need to add the following to
  90. <literal>web.xml</literal>: <programlisting><![CDATA[
  91. <listener>
  92. <listener-class>
  93. org.springframework.security.web.session.HttpSessionEventPublisher
  94. </listener-class>
  95. </listener> ]]>
  96. </programlisting></para>
  97. <para>In addition, you will need to add the <literal>ConcurrentSessionFilter</literal> to
  98. your <classname>FilterChainProxy</classname>. The
  99. <classname>ConcurrentSessionFilter</classname> requires two properties,
  100. <literal>sessionRegistry</literal>, which generally points to an instance of
  101. <classname>SessionRegistryImpl</classname>, and <literal>expiredUrl</literal>, which
  102. points to the page to display when a session has expired. A configuration using the
  103. namespace to create the <classname>FilterChainProxy</classname> and other default beans
  104. might look like this: <programlisting><![CDATA[
  105. <http>
  106. <custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />
  107. <custom-filter position="FORM_LOGIN_FILTER" ref="myAuthFilter" />
  108. <session-management session-authentication-strategy-ref="sas"/>
  109. </http>
  110. <beans:bean id="concurrencyFilter"
  111. class="org.springframework.security.web.session.ConcurrentSessionFilter">
  112. <beans:property name="sessionRegistry" ref="sessionRegistry" />
  113. <beans:property name="expiredUrl" value="/session-expired.htm" />
  114. </beans:bean>
  115. <beans:bean id="myAuthFilter" class=
  116. "org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
  117. <beans:property name="sessionAuthenticationStrategy" ref="sas" />
  118. <beans:property name="authenticationManager" ref="authenticationManager" />
  119. </beans:bean>
  120. <beans:bean id="sas" class=
  121. "org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">
  122. <beans:constructor-arg name="sessionRegistry" ref="sessionRegistry" />
  123. <beans:property name="maximumSessions" value="1" />
  124. </beans:bean>
  125. <beans:bean id="sessionRegistry"
  126. class="org.springframework.security.core.session.SessionRegistryImpl" />
  127. ]]>
  128. </programlisting></para>
  129. <para>Adding the listener to <filename>web.xml</filename> causes an
  130. <literal>ApplicationEvent</literal> to be published to the Spring
  131. <literal>ApplicationContext</literal> every time a <literal>HttpSession</literal>
  132. commences or terminates. This is critical, as it allows the
  133. <classname>SessionRegistryImpl</classname> to be notified when a session ends. Without
  134. it, a user will never be able to log back in again once they have exceeded their session
  135. allowance, even if they log out of another session or it times out.</para>
  136. </section>
  137. </chapter>