jaas-auth-provider.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. <chapter xmlns="http://docbook.org/ns/docbook" xmlns:xlink="http://www.w3.org/1999/xlink" version="5.0" xml:id="jaas">
  2. <info>
  3. <title>Java Authentication and Authorization Service (JAAS) Provider</title>
  4. </info>
  5. <section xml:aid="jaas-overview">
  6. <info>
  7. <title>Overview</title>
  8. </info>
  9. <para>Spring Security provides a package able to delegate authentication requests to the
  10. Java Authentication and Authorization Service (JAAS). This package is discussed in
  11. detail below.</para>
  12. </section>
  13. <section xml:id="jaas-abstractjaasauthenticationprovider">
  14. <info>
  15. <title>AbstractJaasAuthenticationProvider</title>
  16. </info>
  17. <para>The <classname>AbstractJaasAuthenticationProvider</classname> is the basis for the
  18. provided JAAS <interfacename>AuthenticationProvider</interfacename> implementations. Subclasses
  19. must implement a method that creates the <classname>LoginContext</classname>. The
  20. <classname>AbstractJaasAuthenticationProvider</classname> has a number of dependencies that can
  21. be injected into it that are discussed below.</para>
  22. <section xml:id="jaas-callbackhandler">
  23. <info>
  24. <title xml:id="jaas-callback-handler">JAAS CallbackHandler</title>
  25. </info>
  26. <para>Most JAAS <literal>LoginModule</literal>s require a callback of some sort. These
  27. callbacks are usually used to obtain the username and password from the user.</para>
  28. <para>In a Spring Security deployment, Spring Security is responsible for this user
  29. interaction (via the authentication mechanism). Thus, by the time the authentication
  30. request is delegated through to JAAS, Spring Security's authentication mechanism
  31. will already have fully-populated an <interfacename>Authentication</interfacename>
  32. object containing all the information required by the JAAS
  33. <literal>LoginModule</literal>.</para>
  34. <para>Therefore, the JAAS package for Spring Security provides two default callback
  35. handlers, <literal>JaasNameCallbackHandler</literal> and
  36. <literal>JaasPasswordCallbackHandler</literal>. Each of these callback handlers
  37. implement <literal>JaasAuthenticationCallbackHandler</literal>. In most cases these
  38. callback handlers can simply be used without understanding the internal
  39. mechanics.</para>
  40. <para>For those needing full control over the callback behavior, internally
  41. <classname>AbstractJaasAuthenticationProvider</classname> wraps these
  42. <literal>JaasAuthenticationCallbackHandler</literal>s with an
  43. <literal>InternalCallbackHandler</literal>. The
  44. <literal>InternalCallbackHandler</literal> is the class that actually implements
  45. JAAS’ normal <literal>CallbackHandler</literal> interface. Any time that the JAAS
  46. <literal>LoginModule</literal> is used, it is passed a list of application context
  47. configured <literal>InternalCallbackHandler</literal>s. If the
  48. <literal>LoginModule</literal> requests a callback against the
  49. <literal>InternalCallbackHandler</literal>s, the callback is in-turn passed to the
  50. <literal>JaasAuthenticationCallbackHandler</literal>s being wrapped.</para>
  51. </section>
  52. <section xml:id="jaas-authoritygranter">
  53. <info>
  54. <title xml:id="jaas-authority-granter">JAAS AuthorityGranter</title>
  55. </info>
  56. <para>JAAS works with principals. Even "roles" are represented as principals in JAAS.
  57. Spring Security, on the other hand, works with
  58. <interfacename>Authentication</interfacename> objects. Each
  59. <interfacename>Authentication</interfacename> object contains a single principal,
  60. and multiple <interfacename>GrantedAuthority</interfacename>s. To facilitate
  61. mapping between these different concepts, Spring Security's JAAS package includes an
  62. <literal>AuthorityGranter</literal> interface.</para>
  63. <para>An <literal>AuthorityGranter</literal> is responsible for inspecting a JAAS
  64. principal and returning a set of <literal>String</literal>s, representing the
  65. authorities assigned to the principal. For each returned authority string, the
  66. <classname>AbstractJaasAuthenticationProvider</classname> creates a
  67. <classname>JaasGrantedAuthority</classname> (which implements Spring Security’s
  68. <interfacename>GrantedAuthority</interfacename> interface) containing the authority
  69. string and the JAAS principal that the
  70. <interfacename>AuthorityGranter</interfacename> was passed. The
  71. <classname>AbstractJaasAuthenticationProvider</classname> obtains the JAAS principals by
  72. firstly successfully authenticating the user’s credentials using the JAAS
  73. <literal>LoginModule</literal>, and then accessing the
  74. <literal>LoginContext</literal> it returns. A call to
  75. <literal>LoginContext.getSubject().getPrincipals()</literal> is made, with each
  76. resulting principal passed to each <interfacename>AuthorityGranter</interfacename>
  77. defined against the
  78. <literal>AbstractJaasAuthenticationProvider.setAuthorityGranters(List)</literal>
  79. property.</para>
  80. <para>Spring Security does not include any production
  81. <interfacename>AuthorityGranter</interfacename>s given that every JAAS principal has
  82. an implementation-specific meaning. However, there is a
  83. <literal>TestAuthorityGranter</literal> in the unit tests that demonstrates a simple
  84. <literal>AuthorityGranter</literal> implementation.</para>
  85. </section>
  86. </section>
  87. <section xml:id="jaas-defaultjaasauthenticationprovider">
  88. <info>
  89. <title>DefaultJaasAuthenticationProvider</title>
  90. </info>
  91. <para>The <classname>DefaultJaasAuthenticationProvider</classname> allows a JAAS
  92. <classname>Configuration</classname> object to be injected into it as a dependency. It then
  93. creates a <classname>LoginContext</classname> using the injected JAAS <classname>Configuration</classname>.
  94. This means that <classname>DefaultJaasAuthenticationProvider</classname> is not bound any particular implementation
  95. of <classname>Configuration</classname> as <classname>JaasAuthenticationProvider</classname> is.</para>
  96. <section xml:id="jaas-inmemoryconfiguration">
  97. <info>
  98. <title>InMemoryConfiguration</title>
  99. </info>
  100. <para>In order to make it easy to inject a <classname>Configuration</classname> into
  101. <classname>DefaultJaasAuthenticationProvider</classname>, a default in memory
  102. implementation named <classname>InMemoryConfiguration</classname> is provided. The
  103. implementation constructor accepts a <interfacename>Map</interfacename> where each key represents a
  104. login configuration name and the value represents an <classname>Array</classname> of
  105. <classname>AppConfigurationEntry</classname>s.
  106. <classname>InMemoryConfiguration</classname> also supports a default
  107. <classname>Array</classname> of <classname>AppConfigurationEntry</classname> objects that
  108. will be used if no mapping is found within the provided <interfacename>Map</interfacename>. For
  109. details, refer to the class level javadoc of <classname>InMemoryConfiguration</classname>.</para>
  110. </section>
  111. <section xml:id="jaas-djap-config">
  112. <info>
  113. <title>DefaultJaasAuthenticationProvider Example Configuration</title>
  114. </info>
  115. <para>While the Spring configuration for <classname>InMemoryConfiguration</classname> can be
  116. more verbose than the standarad JAAS configuration files, using it in conjuction with
  117. <classname>DefaultJaasAuthenticationProvider</classname> is more flexible than
  118. <classname>JaasAuthenticationProvider</classname> since it not dependant on the default
  119. <classname>Configuration</classname> implementation.</para>
  120. <para>An example configuration of <classname>DefaultJaasAuthenticationProvider</classname> using
  121. <classname>InMemoryConfiguration</classname> is provided below. Note that custom implementations of
  122. <classname>Configuration</classname> can easily be injected into
  123. <classname>DefaultJaasAuthenticationProvider</classname> as well.</para>
  124. <programlisting language="xml"><![CDATA[
  125. <bean id="jaasAuthProvider"
  126. class="org.springframework.security.authentication.jaas.DefaultJaasAuthenticationProvider">
  127. <property name="configuration">
  128. <bean class="org.springframework.security.authentication.jaas.memory.InMemoryConfiguration">
  129. <constructor-arg>
  130. <map>
  131. <!--
  132. SPRINGSECURITY is the default loginContextName
  133. for AbstractJaasAuthenticationProvider
  134. -->
  135. <entry key="SPRINGSECURITY">
  136. <array>
  137. <bean class="javax.security.auth.login.AppConfigurationEntry">
  138. <constructor-arg value="sample.SampleLoginModule" />
  139. <constructor-arg>
  140. <util:constant static-field=
  141. "javax.security.auth.login.AppConfigurationEntry$LoginModuleControlFlag.REQUIRED"/>
  142. </constructor-arg>
  143. <constructor-arg>
  144. <map></map>
  145. </constructor-arg>
  146. </bean>
  147. </array>
  148. </entry>
  149. </map>
  150. </constructor-arg>
  151. </bean>
  152. </property>
  153. <property name="authorityGranters">
  154. <list>
  155. <!-- You will need to write your own implementation of AuthorityGranter -->
  156. <bean class="org.springframework.security.authentication.jaas.TestAuthorityGranter"/>
  157. </list>
  158. </property>
  159. </bean>
  160. ]]></programlisting>
  161. </section>
  162. </section>
  163. <section xml:id="jaas-jaasauthenticationprovider">
  164. <info>
  165. <title>JaasAuthenticationProvider</title>
  166. </info>
  167. <para>The <classname>JaasAuthenticationProvider</classname> assumes the default <classname>Configuration</classname> is an instance of
  168. <link xlink:href="http://download.oracle.com/javase/1.4.2/docs/guide/security/jaas/spec/com/sun/security/auth/login/ConfigFile.html">
  169. ConfigFile</link>. This assumption is made in order to attempt to update the <classname>Configuration</classname>. The
  170. <classname>JaasAuthenticationProvider</classname> then uses the default <classname>Configuration</classname> to create the
  171. <classname>LoginContext</classname>.</para>
  172. <para>Let’s assume we have a JAAS login configuration file,
  173. <literal>/WEB-INF/login.conf</literal>, with the following contents:
  174. <programlisting language="txt">
  175. JAASTest {
  176. sample.SampleLoginModule required;
  177. };</programlisting></para>
  178. <para>Like all Spring Security beans, the <classname>JaasAuthenticationProvider</classname>
  179. is configured via the application context. The following definitions would correspond to
  180. the above JAAS login configuration file: <programlisting language="xml"><![CDATA[
  181. <bean id="jaasAuthenticationProvider"
  182. class="org.springframework.security.authentication.jaas.JaasAuthenticationProvider">
  183. <property name="loginConfig" value="/WEB-INF/login.conf"/>
  184. <property name="loginContextName" value="JAASTest"/>
  185. <property name="callbackHandlers">
  186. <list>
  187. <bean
  188. class="org.springframework.security.authentication.jaas.JaasNameCallbackHandler"/>
  189. <bean
  190. class="org.springframework.security.authentication.jaas.JaasPasswordCallbackHandler"/>
  191. </list>
  192. </property>
  193. <property name="authorityGranters">
  194. <list>
  195. <bean class="org.springframework.security.authentication.jaas.TestAuthorityGranter"/>
  196. </list>
  197. </property>
  198. </bean>
  199. ]]></programlisting></para>
  200. </section>
  201. <section xml:id="jaas-apiprovision">
  202. <info>
  203. <title xml:id="jaas-api-provision">Running as a Subject</title>
  204. </info>
  205. <para>If configured, the <classname>JaasApiIntegrationFilter</classname> will attempt to
  206. run as the <literal>Subject</literal> on the
  207. <classname>JaasAuthenticationToken</classname>. This means that the
  208. <literal>Subject</literal> can be accessed using:
  209. <programlisting language="java"><![CDATA[
  210. Subject subject = Subject.getSubject(AccessController.getContext());
  211. ]]></programlisting>
  212. This integration can easily be configured using the
  213. <link xlink:href="#nsa-http-jaas-api-provision">jaas-api-provision</link> attribute. This
  214. feature is useful when integrating with legacy or external API's that rely on the
  215. JAAS Subject being populated.</para>
  216. </section>
  217. </chapter>