jaas-auth-provider.xml 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <chapter xmlns="http://docbook.org/ns/docbook" version="5.0" xml:id="jaas">
  2. <info><title>Java Authentication and Authorization Service (JAAS) Provider</title></info>
  3. <section xml:id="jaas-overview">
  4. <info><title>Overview</title></info>
  5. <para>Spring Security provides a package able to delegate
  6. authentication requests to the Java Authentication and Authorization
  7. Service (JAAS). This package is discussed in detail below.</para>
  8. <para>Central to JAAS operation are login configuration files. To
  9. learn more about JAAS login configuration files, consult the JAAS
  10. reference documentation available from Sun Microsystems. We expect you
  11. to have a basic understanding of JAAS and its login configuration file
  12. syntax in order to understand this section.</para>
  13. </section>
  14. <section xml:id="jaas-config">
  15. <info><title>Configuration</title></info>
  16. <para>The <literal>JaasAuthenticationProvider</literal> attempts to
  17. authenticate a user’s principal and credentials through JAAS.</para>
  18. <para>Let’s assume we have a JAAS login configuration file,
  19. <literal>/WEB-INF/login.conf</literal>, with the following
  20. contents:
  21. <programlisting>
  22. JAASTest {
  23. sample.SampleLoginModule required;
  24. };</programlisting></para>
  25. <para>Like all Spring Security beans, the
  26. <classname>JaasAuthenticationProvider</classname> is configured via the
  27. application context. The following definitions would correspond to the
  28. above JAAS login configuration file:
  29. <programlisting><![CDATA[
  30. <bean id="jaasAuthenticationProvider"
  31. class="org.springframework.security.authentication.jaas.JaasAuthenticationProvider">
  32. <property name="loginConfig" value="/WEB-INF/login.conf"/>
  33. <property name="loginContextName" value="JAASTest"/>
  34. <property name="callbackHandlers">
  35. <list>
  36. <bean
  37. class="org.springframework.security.authentication.jaas.JaasNameCallbackHandler"/>
  38. <bean
  39. class="org.springframework.security.authentication.jaas.JaasPasswordCallbackHandler"/>
  40. </list>
  41. </property>
  42. <property name="authorityGranters">
  43. <list>
  44. <bean class="org.springframework.security.authentication.jaas.TestAuthorityGranter"/>
  45. </list>
  46. </property>
  47. </bean>
  48. ]]></programlisting></para>
  49. <para>The <literal>CallbackHandler</literal>s and
  50. <interfacename>AuthorityGranter</interfacename>s are discussed below.</para>
  51. <section xml:id="jaas-callbackhandler">
  52. <info><title xml:id="jaas-callback-handler">JAAS CallbackHandler</title></info>
  53. <para>Most JAAS <literal>LoginModule</literal>s require a callback
  54. of some sort. These callbacks are usually used to obtain the
  55. username and password from the user.</para>
  56. <para>In a Spring Security deployment, Spring Security is
  57. responsible for this user interaction (via the authentication
  58. mechanism). Thus, by the time the authentication request is
  59. delegated through to JAAS, Spring Security's authentication
  60. mechanism will already have fully-populated an
  61. <interfacename>Authentication</interfacename> object containing all the
  62. information required by the JAAS
  63. <literal>LoginModule</literal>.</para>
  64. <para>Therefore, the JAAS package for Spring Security provides two
  65. default callback handlers,
  66. <literal>JaasNameCallbackHandler</literal> and
  67. <literal>JaasPasswordCallbackHandler</literal>. Each of these
  68. callback handlers implement
  69. <literal>JaasAuthenticationCallbackHandler</literal>. In most cases
  70. these callback handlers can simply be used without understanding the
  71. internal mechanics.</para>
  72. <para>For those needing full control over the callback behavior,
  73. internally <literal>JaasAutheticationProvider</literal> wraps these
  74. <literal>JaasAuthenticationCallbackHandler</literal>s with an
  75. <literal>InternalCallbackHandler</literal>. The
  76. <literal>InternalCallbackHandler</literal> is the class that
  77. actually implements JAAS’ normal <literal>CallbackHandler</literal>
  78. interface. Any time that the JAAS <literal>LoginModule</literal> is
  79. used, it is passed a list of application context configured
  80. <literal>InternalCallbackHandler</literal>s. If the
  81. <literal>LoginModule</literal> requests a callback against the
  82. <literal>InternalCallbackHandler</literal>s, the callback is in-turn
  83. passed to the <literal>JaasAuthenticationCallbackHandler</literal>s
  84. being wrapped.</para>
  85. </section>
  86. <section xml:id="jaas-authoritygranter">
  87. <info><title xml:id="jaas-authority-granter">JAAS AuthorityGranter</title></info>
  88. <para>JAAS works with principals. Even "roles" are represented as
  89. principals in JAAS. Spring Security, on the other hand, works with
  90. <interfacename>Authentication</interfacename> objects. Each
  91. <interfacename>Authentication</interfacename> object contains a single
  92. principal, and multiple <interfacename>GrantedAuthority</interfacename>[]s. To
  93. facilitate mapping between these different concepts, Spring
  94. Security's JAAS package includes an
  95. <literal>AuthorityGranter</literal> interface.</para>
  96. <para>An <literal>AuthorityGranter</literal> is responsible for
  97. inspecting a JAAS principal and returning a set of
  98. <literal>String</literal>s, representing the authorities assigned to the principal.
  99. For each returned authority string, the
  100. <classname>JaasAuthenticationProvider</classname> creates a
  101. <classname>JaasGrantedAuthority</classname> (which implements Spring
  102. Security’s <interfacename>GrantedAuthority</interfacename> interface) containing
  103. the authority string and the JAAS principal that the
  104. <interfacename>AuthorityGranter</interfacename> was passed. The
  105. <classname>JaasAuthenticationProvider</classname> obtains the JAAS
  106. principals by firstly successfully authenticating the user’s
  107. credentials using the JAAS <literal>LoginModule</literal>, and then
  108. accessing the <literal>LoginContext</literal> it returns. A call to
  109. <literal>LoginContext.getSubject().getPrincipals()</literal> is
  110. made, with each resulting principal passed to each
  111. <interfacename>AuthorityGranter</interfacename> defined against the
  112. <literal>JaasAuthenticationProvider.setAuthorityGranters(List)</literal>
  113. property.</para>
  114. <para>Spring Security does not include any production
  115. <interfacename>AuthorityGranter</interfacename>s given that every JAAS principal
  116. has an implementation-specific meaning. However, there is a
  117. <literal>TestAuthorityGranter</literal> in the unit tests that
  118. demonstrates a simple <literal>AuthorityGranter</literal>
  119. implementation.</para>
  120. </section>
  121. </section>
  122. </chapter>