session-mgmt.xml 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. ]]></programlisting>
  59. Note that the use of the default, <classname>SessionFixationProtectionStrategy</classname>
  60. may cause issues if you are storing beans in the session which implement
  61. <interfacename>HttpSessionBindingListener</interfacename>, including Spring session-scoped
  62. beans. See the Javadoc for this class for more information.
  63. </para>
  64. </section>
  65. <section xml:id="concurrent-sessions">
  66. <title>Concurrency Control</title>
  67. <para>Spring Security is able to prevent a principal from concurrently authenticating to the
  68. same application more than a specified number of times. Many ISVs take advantage of this
  69. to enforce licensing, whilst network administrators like this feature because it helps
  70. prevent people from sharing login names. You can, for example, stop user
  71. <quote>Batman</quote> from logging onto the web application from two different sessions.
  72. You can either expire their previous login or you can report an error when they try to
  73. log in again, preventing the second login. Note that if you are using the second
  74. approach, a user who has not explicitly logged out (but who has just closed their
  75. browser, for example) will not be able to log in again until their original session
  76. expires.</para>
  77. <para>Concurrency control is supported by the namespace, so please check the earlier
  78. namespace chapter for the simplest configuration. Sometimes you need to customize things
  79. though. </para>
  80. <para>The implementation uses a specialized version of
  81. <interfacename>SessionAuthenticationStrategy</interfacename>, called
  82. <classname>ConcurrentSessionControlStrategy</classname>. <note>
  83. <para>Previously the concurrent authentication check was made by the
  84. <classname>ProviderManager</classname>, which could be injected with a
  85. <literal>ConcurrentSessionController</literal>. The latter would check if the user
  86. was attempting to exceed the number of permitted sessions. However, this approach
  87. required that an HTTP session be created in advance, which is undesirable. In Spring
  88. Security 3, the user is first authenticated by the
  89. <interfacename>AuthenticationManager</interfacename> and once they are successfully
  90. authenticated, a session is created and the check is made whether they are allowed
  91. to have another session open.</para>
  92. </note></para>
  93. <para>To use concurrent session support, you'll need to add the following to
  94. <literal>web.xml</literal>: <programlisting><![CDATA[
  95. <listener>
  96. <listener-class>
  97. org.springframework.security.web.session.HttpSessionEventPublisher
  98. </listener-class>
  99. </listener> ]]>
  100. </programlisting></para>
  101. <para>In addition, you will need to add the <literal>ConcurrentSessionFilter</literal> to
  102. your <classname>FilterChainProxy</classname>. The
  103. <classname>ConcurrentSessionFilter</classname> requires two properties,
  104. <literal>sessionRegistry</literal>, which generally points to an instance of
  105. <classname>SessionRegistryImpl</classname>, and <literal>expiredUrl</literal>, which
  106. points to the page to display when a session has expired. A configuration using the
  107. namespace to create the <classname>FilterChainProxy</classname> and other default beans
  108. might look like this: <programlisting><![CDATA[
  109. <http>
  110. <custom-filter position="CONCURRENT_SESSION_FILTER" ref="concurrencyFilter" />
  111. <custom-filter position="FORM_LOGIN_FILTER" ref="myAuthFilter" />
  112. <session-management session-authentication-strategy-ref="sas"/>
  113. </http>
  114. <beans:bean id="concurrencyFilter"
  115. class="org.springframework.security.web.session.ConcurrentSessionFilter">
  116. <beans:property name="sessionRegistry" ref="sessionRegistry" />
  117. <beans:property name="expiredUrl" value="/session-expired.htm" />
  118. </beans:bean>
  119. <beans:bean id="myAuthFilter" class=
  120. "org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter">
  121. <beans:property name="sessionAuthenticationStrategy" ref="sas" />
  122. <beans:property name="authenticationManager" ref="authenticationManager" />
  123. </beans:bean>
  124. <beans:bean id="sas" class=
  125. "org.springframework.security.web.authentication.session.ConcurrentSessionControlStrategy">
  126. <beans:constructor-arg name="sessionRegistry" ref="sessionRegistry" />
  127. <beans:property name="maximumSessions" value="1" />
  128. </beans:bean>
  129. <beans:bean id="sessionRegistry"
  130. class="org.springframework.security.core.session.SessionRegistryImpl" />
  131. ]]>
  132. </programlisting></para>
  133. <para>Adding the listener to <filename>web.xml</filename> causes an
  134. <literal>ApplicationEvent</literal> to be published to the Spring
  135. <literal>ApplicationContext</literal> every time a <literal>HttpSession</literal>
  136. commences or terminates. This is critical, as it allows the
  137. <classname>SessionRegistryImpl</classname> to be notified when a session ends. Without
  138. it, a user will never be able to log back in again once they have exceeded their session
  139. allowance, even if they log out of another session or it times out.</para>
  140. <section xml:id="list-authenticated-principals">
  141. <title>Querying the <interfacename>SessionRegistry</interfacename> for currently authenticated
  142. users and their sessions</title>
  143. <para>
  144. Setting up concurrency-control, either through the namespace or using plain beans has the
  145. useful side effect of providing you with a reference to the <interfacename>SessionRegistry</interfacename>
  146. which you can use directly within your application, so even if you don't want to restrict the
  147. number of sessions a user may have, it may be worth setting up the infrastructure anyway. You can
  148. set the <literal>maximumSession</literal> property to -1 to allow unlimited sessions. If
  149. you're using the namespace, you can set an alias for the internally-created
  150. <interfacename>SessionRegistry</interfacename> using the <literal>session-registry-alias</literal>
  151. attribute, providing a reference which you can inject into your own beans.</para>
  152. <para>
  153. The <methodname>getAllPrincipals()</methodname>
  154. method supplies you with a list of the currently authenticated users. You can list a user's
  155. sessions by calling the <methodname>getAllSessions(Object principal, boolean includeExpiredSessions)</methodname> method,
  156. which returns a list of <classname>SessionInformation</classname> objects. You can also
  157. expire a user's session by calling <methodname>expireNow()</methodname> on a
  158. <methodname>SessionInformation</methodname> instance. When the user returns to the application, they
  159. will be prevented from proceeding. You may find these methods useful in an administration
  160. application, for example. Have a look at the Javadoc for more information.
  161. </para>
  162. </section>
  163. </section>
  164. </chapter>