jaas-auth-provider.xml 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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 class="org.springframework.security.authentication.jaas.JaasNameCallbackHandler"/>
  37. <bean class="org.springframework.security.authentication.jaas.JaasPasswordCallbackHandler"/>
  38. </list>
  39. </property>
  40. <property name="authorityGranters">
  41. <list>
  42. <bean class="org.springframework.security.authentication.jaas.TestAuthorityGranter"/>
  43. </list>
  44. </property>
  45. </bean>
  46. ]]></programlisting></para>
  47. <para>The <literal>CallbackHandler</literal>s and
  48. <interfacename>AuthorityGranter</interfacename>s are discussed below.</para>
  49. <section xml:id="jaas-callbackhandler">
  50. <info><title xml:id="jaas-callback-handler">JAAS CallbackHandler</title></info>
  51. <para>Most JAAS <literal>LoginModule</literal>s require a callback
  52. of some sort. These callbacks are usually used to obtain the
  53. username and password from the user.</para>
  54. <para>In a Spring Security deployment, Spring Security is
  55. responsible for this user interaction (via the authentication
  56. mechanism). Thus, by the time the authentication request is
  57. delegated through to JAAS, Spring Security's authentication
  58. mechanism will already have fully-populated an
  59. <interfacename>Authentication</interfacename> object containing all the
  60. information required by the JAAS
  61. <literal>LoginModule</literal>.</para>
  62. <para>Therefore, the JAAS package for Spring Security provides two
  63. default callback handlers,
  64. <literal>JaasNameCallbackHandler</literal> and
  65. <literal>JaasPasswordCallbackHandler</literal>. Each of these
  66. callback handlers implement
  67. <literal>JaasAuthenticationCallbackHandler</literal>. In most cases
  68. these callback handlers can simply be used without understanding the
  69. internal mechanics.</para>
  70. <para>For those needing full control over the callback behavior,
  71. internally <literal>JaasAutheticationProvider</literal> wraps these
  72. <literal>JaasAuthenticationCallbackHandler</literal>s with an
  73. <literal>InternalCallbackHandler</literal>. The
  74. <literal>InternalCallbackHandler</literal> is the class that
  75. actually implements JAAS’ normal <literal>CallbackHandler</literal>
  76. interface. Any time that the JAAS <literal>LoginModule</literal> is
  77. used, it is passed a list of application context configured
  78. <literal>InternalCallbackHandler</literal>s. If the
  79. <literal>LoginModule</literal> requests a callback against the
  80. <literal>InternalCallbackHandler</literal>s, the callback is in-turn
  81. passed to the <literal>JaasAuthenticationCallbackHandler</literal>s
  82. being wrapped.</para>
  83. </section>
  84. <section xml:id="jaas-authoritygranter">
  85. <info><title xml:id="jaas-authority-granter">JAAS AuthorityGranter</title></info>
  86. <para>JAAS works with principals. Even "roles" are represented as
  87. principals in JAAS. Spring Security, on the other hand, works with
  88. <interfacename>Authentication</interfacename> objects. Each
  89. <interfacename>Authentication</interfacename> object contains a single
  90. principal, and multiple <interfacename>GrantedAuthority</interfacename>[]s. To
  91. facilitate mapping between these different concepts, Spring
  92. Security's JAAS package includes an
  93. <literal>AuthorityGranter</literal> interface.</para>
  94. <para>An <literal>AuthorityGranter</literal> is responsible for
  95. inspecting a JAAS principal and returning a set of
  96. <literal>String</literal>s, representing the authorities assigned to the principal.
  97. For each returned authority string, the
  98. <classname>JaasAuthenticationProvider</classname> creates a
  99. <classname>JaasGrantedAuthority</classname> (which implements Spring
  100. Security’s <interfacename>GrantedAuthority</interfacename> interface) containing
  101. the authority string and the JAAS principal that the
  102. <interfacename>AuthorityGranter</interfacename> was passed. The
  103. <classname>JaasAuthenticationProvider</classname> obtains the JAAS
  104. principals by firstly successfully authenticating the user’s
  105. credentials using the JAAS <literal>LoginModule</literal>, and then
  106. accessing the <literal>LoginContext</literal> it returns. A call to
  107. <literal>LoginContext.getSubject().getPrincipals()</literal> is
  108. made, with each resulting principal passed to each
  109. <interfacename>AuthorityGranter</interfacename> defined against the
  110. <literal>JaasAuthenticationProvider.setAuthorityGranters(List)</literal>
  111. property.</para>
  112. <para>Spring Security does not include any production
  113. <interfacename>AuthorityGranter</interfacename>s given that every JAAS principal
  114. has an implementation-specific meaning. However, there is a
  115. <literal>TestAuthorityGranter</literal> in the unit tests that
  116. demonstrates a simple <literal>AuthorityGranter</literal>
  117. implementation.</para>
  118. </section>
  119. </section>
  120. </chapter>