form-authentication.xml 4.1 KB

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