channel-security.xml 9.6 KB

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