session-mgmt.xml 7.9 KB

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