channel-security.xml 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <chapter xmlns="http://docbook.org/ns/docbook" version="5.0" xml:id="channel-security"
  2. xmlns:xlink="http://www.w3.org/1999/xlink">
  3. <info>
  4. <title>Channel Security</title>
  5. </info>
  6. <section xml:id="channel-security-overview">
  7. <info>
  8. <title>Overview</title>
  9. </info>
  10. <para>In addition to coordinating the authentication and authorization requirements of your
  11. application, Spring Security is also able to ensure unauthenticated web requests have
  12. certain properties. These properties may include being of a particular transport type,
  13. having a particular <literal>HttpSession</literal> attribute set and so on. The most
  14. common requirement is for your web requests to be received using a particular transport
  15. protocol, such as HTTPS.</para>
  16. <para>An important issue in considering transport security is that of session hijacking.
  17. Your web container manages a <literal>HttpSession</literal> by reference to a
  18. <literal>jsessionid</literal> that is sent to user agents either via a cookie or URL
  19. rewriting. If the <literal>jsessionid</literal> is ever sent over HTTP, there is a
  20. possibility that session identifier can be intercepted and used to impersonate the user
  21. after they complete the authentication process. This is because most web containers
  22. maintain the same session identifier for a given user, even after they switch from HTTP
  23. to HTTPS pages.</para>
  24. <para>If session hijacking is considered too significant a risk for your particular
  25. application, the only option is to use HTTPS for every request. This means the
  26. <literal>jsessionid</literal> is never sent across an insecure channel. You will
  27. need to ensure your <literal>web.xml</literal>-defined
  28. <literal>&lt;welcome-file&gt;</literal> points to an HTTPS location, and the
  29. application never directs the user to an HTTP location. Spring Security provides a
  30. solution to assist with the latter.</para>
  31. </section>
  32. <section xml:id="channel-security-config">
  33. <info>
  34. <title>Configuration</title>
  35. </info>
  36. <para>Channel security is supported by the <link xlink:href="#ns-requires-channel">security
  37. namespace</link> by means of the <literal>requires-channel</literal> attribute on
  38. the <literal>&lt;intercept-url&gt;</literal> element and this is the simplest (and
  39. recommended approach).</para>
  40. <para>To configure channel security explicitly, you would define the following the filter in
  41. your application context: <programlisting><![CDATA[
  42. <bean id="channelProcessingFilter"
  43. class="org.springframework.security.web.access.channel.ChannelProcessingFilter">
  44. <property name="channelDecisionManager" ref="channelDecisionManager"/>
  45. <property name="securityMetadataSource">
  46. <security:filter-security-metadata-source path-type="regex">
  47. <security:intercept-url pattern="\A/secure/.*\Z"
  48. access="REQUIRES_SECURE_CHANNEL"/>
  49. <security:intercept-url pattern="\A/acegilogin.jsp.*\Z"
  50. access="REQUIRES_SECURE_CHANNEL"/>
  51. <security:intercept-url pattern="\A/j_spring_security_check.*\Z"
  52. access="REQUIRES_SECURE_CHANNEL"/>
  53. <security:intercept-url pattern="\A/.*\Z" access="ANY_CHANNEL"/>
  54. </security:filter-security-metadata-source>
  55. </property>
  56. </bean>
  57. <bean id="channelDecisionManager"
  58. class="org.springframework.security.access.channel.ChannelDecisionManagerImpl">
  59. <property name="channelProcessors">
  60. <list>
  61. <ref bean="secureChannelProcessor"/>
  62. <ref bean="insecureChannelProcessor"/>
  63. </list>
  64. </property>
  65. </bean>
  66. <bean id="secureChannelProcessor"
  67. class="org.springframework.security.access.channel.SecureChannelProcessor"/>
  68. <bean id="insecureChannelProcessor"
  69. class="org.springframework.security.access.channel.InsecureChannelProcessor"/>]]>
  70. </programlisting>
  71. Like <classname>FilterSecurityInterceptor</classname>, Apache Ant style paths are also
  72. supported by the <literal>ChannelProcessingFilter</literal>.</para>
  73. <para>The <literal>ChannelProcessingFilter</literal> operates by filtering all web requests
  74. and determining the configuration attributes that apply. It then delegates to the
  75. <literal>ChannelDecisionManager</literal>. The default implementation,
  76. <literal>ChannelDecisionManagerImpl</literal>, should suffice in most cases. It
  77. simply delegates to the list of configured <literal>ChannelProcessor</literal>
  78. instances. The attribute <literal>ANY_CHANNEL</literal> can be used to override this
  79. behaviour and skip a particular URL. Otherwise, a <literal>ChannelProcessor</literal>
  80. will review the request, and if it is unhappy with the request (e.g. if it was received
  81. across the incorrect transport protocol), it will perform a redirect, throw an exception
  82. or take whatever other action is appropriate.</para>
  83. <para>Included with Spring Security are two concrete <literal>ChannelProcessor</literal>
  84. implementations: <literal>SecureChannelProcessor</literal> ensures requests with a
  85. configuration attribute of <literal>REQUIRES_SECURE_CHANNEL</literal> are received over
  86. HTTPS, whilst <literal>InsecureChannelProcessor</literal> ensures requests with a
  87. configuration attribute of <literal>REQUIRES_INSECURE_CHANNEL</literal> are received
  88. over HTTP. Both implementations delegate to a <literal>ChannelEntryPoint</literal> if
  89. the required transport protocol is not used. The two
  90. <literal>ChannelEntryPoint</literal> implementations included with Spring Security
  91. simply redirect the request to HTTP and HTTPS as appropriate. Appropriate defaults are
  92. assigned to the <literal>ChannelProcessor</literal> implementations for the
  93. configuration attribute keywords they respond to and the
  94. <interfacename>ChannelEntryPoint</interfacename> they delegate to, although you have
  95. the ability to override these using the application context.</para>
  96. <para>Note that the redirections are absolute (eg
  97. <literal>http://www.company.com:8080/app/page</literal>), not relative (eg
  98. <literal>/app/page</literal>). During testing it was discovered that Internet
  99. Explorer 6 Service Pack 1 has a bug whereby it does not respond correctly to a
  100. redirection instruction which also changes the port to use. Accordingly, absolute URLs
  101. are used in conjunction with bug detection logic in the
  102. <classname>PortResolverImpl</classname> that is wired up by default to many Spring
  103. Security beans. Please refer to the JavaDocs for <classname>PortResolverImpl</classname>
  104. for further details.</para>
  105. <para>You should note that using a secure channel is recommended if usernames and passwords
  106. are to be kept secure during the login process. If you do decide to use
  107. <classname>ChannelProcessingFilter</classname> with form-based login, please ensure
  108. that your login page is set to <literal>REQUIRES_SECURE_CHANNEL</literal>, and that the
  109. <literal>LoginUrlAuthenticationEntryPoint.forceHttps</literal> property is
  110. <literal>true</literal>.</para>
  111. </section>
  112. <section xml:id="channel-security-conclusion">
  113. <info>
  114. <title>Conclusion</title>
  115. </info>
  116. <para>Once configured, using the channel security filter is very easy. Simply request pages
  117. without regard to the protocol (ie HTTP or HTTPS) or port (eg 80, 8080, 443, 8443 etc).
  118. Obviously you'll still need a way of making the initial request (probably via the
  119. <literal>web.xml</literal>
  120. <literal>&lt;welcome-file&gt;</literal> or a well-known home page URL), but once this is
  121. done the filter will perform redirects as defined by your application context.</para>
  122. <para>You can also add your own <literal>ChannelProcessor</literal> implementations to the
  123. <literal>ChannelDecisionManagerImpl</literal>. For example, you might set a
  124. <literal>HttpSession</literal> attribute when a human user is detected via a "enter
  125. the contents of this graphic" procedure. Your <literal>ChannelProcessor</literal> would
  126. respond to say <literal>REQUIRES_HUMAN_USER</literal> configuration attributes and
  127. redirect to an appropriate entry point to start the human user validation process if the
  128. <literal>HttpSession</literal> attribute is not currently set.</para>
  129. <para>To decide whether a security check belongs in a <literal>ChannelProcessor</literal> or
  130. an <interfacename>AccessDecisionVoter</interfacename>, remember that the former is
  131. designed to handle unauthenticated requests, whilst the latter is designed to handle
  132. authenticated requests. The latter therefore has access to the granted authorities of
  133. the authenticated principal. In addition, problems detected by a
  134. <literal>ChannelProcessor</literal> will generally cause an HTTP/HTTPS redirection
  135. so its requirements can be met, whilst problems detected by an
  136. <interfacename>AccessDecisionVoter</interfacename> will ultimately result in an
  137. <literal>AccessDeniedException</literal> (depending on the governing
  138. <interfacename>AccessDecisionManager</interfacename>).</para>
  139. </section>
  140. </chapter>