2
0

form-authentication.xml 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. <chapter xmlns="http://docbook.org/ns/docbook" version="5.0" xml:id="form">
  2. <info><title>Form Authentication Mechanism</title></info>
  3. <section xml:id="form-overview">
  4. <info><title>Overview</title></info>
  5. <para>HTTP Form Authentication involves using the
  6. <literal>UsernamePasswordAuthenticationProcessingFilter</literal> to process a login
  7. form. This is the most common way for an application to authenticate end
  8. users. Form-based authentication is entirely compatible with the DAO, LDAP
  9. and JAAS authentication providers.</para>
  10. <para>This is also the mechanism used by the &lt;form-login&gt; element from the namespace
  11. and it's recommended that you use that unless you have specific customization requirements.
  12. </para>
  13. </section>
  14. <section xml:id="form-config">
  15. <info><title>Configuration</title></info>
  16. <para>The login form simply contains <literal>j_username</literal> and
  17. <literal>j_password</literal> input fields, and posts to a URL that is
  18. monitored by the filter (by default
  19. <literal>/j_spring_security_check</literal>). You should add an
  20. <literal>UsernamePasswordAuthenticationProcessingFilter</literal> to your application context:
  21. <programlisting><![CDATA[
  22. <bean id="authenticationProcessingFilter" class=
  23. "org.springframework.security.web.authentication.UsernamePasswordAuthenticationProcessingFilter">
  24. <property name="authenticationManager" ref="authenticationManager"/>
  25. <property name="filterProcessesUrl" value="/j_spring_security_check"/>
  26. </bean> ]]>
  27. </programlisting></para>
  28. <para>
  29. The configured <interfacename>AuthenticationManager</interfacename>
  30. processes each authentication request. The destination following a successful authentication
  31. or an authentication failure is controlled by the <interfacename>AuthenticationSuccessHandler</interfacename>
  32. and <interfacename>AuthenticationFailureHandler</interfacename> interfaces, respectively.
  33. The filter has properties which allow you to set these
  34. <footnote><para>In versions prior to 3.0, the application flow at this point had evolved to a stage
  35. was controlled by a mix of properties on this class and strategy plugins. The
  36. decision was made for 3.0 to refactor the code to make these two strategies entirely responsible.
  37. </para></footnote>.
  38. Some standard implementations are supplied for these such as
  39. <classname>SimpleUrlAuthenticationSuccessHandler</classname>,
  40. <classname>SavedRequestAwareAuthenticationSuccessHandler</classname>,
  41. <classname>SimpleUrlAuthenticationFailureHandler</classname> and
  42. <classname>ExceptionMappingAuthenticationFailureHandler</classname>. Have a look at the Javadoc
  43. for these classes to see how they work.
  44. </para>
  45. <para>If authentication is successful, the resulting
  46. <interfacename>Authentication</interfacename> object will be placed into the
  47. <classname>SecurityContextHolder</classname>.
  48. The configured AuthenticationSuccessHandler will then be called to either redirect or forward
  49. the user to the approprate destination. By default a <classname>SavedRequestAwareAuthenticationSuccessHandler</classname>
  50. is used, which means that the user will be redirected to the original destination they requested before they were asked to
  51. login.
  52. <note>
  53. <para>
  54. The <classname>ExceptionTranslationFilter</classname> caches the original request a user makes.
  55. When the user authenticates, the request handler makes use of this cached request to obtain the original
  56. URL and redirect to it. The original request is then rebuilt and used as an alternative.
  57. </para>
  58. </note>
  59. If authentication fails, the configured <interfacename>AuthenticationFailureHandler</interfacename> will be invoked.
  60. </para>
  61. </section>
  62. </chapter>