ldap-auth-provider.xml 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  1. <chapter xmlns="http://docbook.org/ns/docbook" version="5.0" xml:id="ldap">
  2. <info>
  3. <title>LDAP Authentication</title>
  4. </info>
  5. <section xml:id="ldap-overview">
  6. <info>
  7. <title>Overview</title>
  8. </info>
  9. <para>LDAP is often used by organizations as a central repository for user information and
  10. as an authentication service. It can also be used to store the role information for
  11. application users.</para>
  12. <para>There are many different scenarios for how an LDAP server may be configured so Spring
  13. Security's LDAP provider is fully configurable. It uses separate strategy interfaces for
  14. authentication and role retrieval and provides default implementations which can be
  15. configured to handle a wide range of situations.</para>
  16. <para>You should be familiar with LDAP before trying to use it with Spring Security. The
  17. following link provides a good introduction to the concepts involved and a guide to
  18. setting up a directory using the free LDAP server OpenLDAP: <uri
  19. xmlns:xlink="http://www.w3.org/1999/xlink"
  20. xlink:href="http://www.zytrax.com/books/ldap/"
  21. >http://www.zytrax.com/books/ldap/</uri>. Some familiarity with the JNDI APIs used
  22. to access LDAP from Java may also be useful. We don't use any third-party LDAP libraries
  23. (Mozilla, JLDAP etc.) in the LDAP provider, but extensive use is made of Spring LDAP, so
  24. some familiarity with that project may be useful if you plan on adding your own
  25. customizations.</para>
  26. </section>
  27. <section>
  28. <info>
  29. <title>Using LDAP with Spring Security</title>
  30. </info>
  31. <para> LDAP authentication in Spring Security can be roughly divided into the following
  32. stages. <orderedlist inheritnum="ignore" continuation="restarts">
  33. <listitem>
  34. <para>Obtaining the unique LDAP <quote>Distinguished Name</quote>, or DN, from
  35. the login name. This will often mean performing a search in the directory,
  36. unless the exact mapping of usernames to DNs is known in advance.</para>
  37. </listitem>
  38. <listitem>
  39. <para>Authenticating the user, either by binding as that user or by performing a
  40. remote <quote>compare</quote> operation of the user's password against the
  41. password attribute in the directory entry for the DN.</para>
  42. </listitem>
  43. <listitem>
  44. <para>Loading the list of authorities for the user.</para>
  45. </listitem>
  46. </orderedlist> The exception is when the LDAP directory is just being used to retrieve
  47. user information and authenticate against it locally. This may not be possible as
  48. directories are often set up with limited read access for attributes such as user
  49. passwords. </para>
  50. <para> We will look at some configuration scenarios below. For full information on available
  51. configuration options, please consult the security namespace schema (information from
  52. which should be available in your XML editor). </para>
  53. </section>
  54. <section xml:id="ldap-server">
  55. <info>
  56. <title>Configuring an LDAP Server</title>
  57. </info>
  58. <para> The first thing you need to do is configure the server against which authentication
  59. should take place. This is done using the <literal>&lt;ldap-server&gt;</literal> element
  60. from the security namespace. This can be configured to point at an external LDAP server,
  61. using the <literal>url</literal> attribute: <programlisting><![CDATA[
  62. <ldap-server url="ldap://springframework.org:389/dc=springframework,dc=org" />
  63. ]]>
  64. </programlisting></para>
  65. <section>
  66. <info>
  67. <title>Using an Embedded Test Server</title>
  68. </info>
  69. <para> The <literal>&lt;ldap-server&gt;</literal> element can also be used to create an
  70. embedded server, which can be very useful for testing and demonstrations. In this
  71. case you use it without the <literal>url</literal> attribute: <programlisting><![CDATA[
  72. <ldap-server root="dc=springframework,dc=org"/>
  73. ]]>
  74. </programlisting> Here we've specified that the root DIT of the directory should be
  75. <quote>dc=springframework,dc=org</quote>, which is the default. Used this way,
  76. the namespace parser will create an embedded Apache Directory server and scan the
  77. classpath for any LDIF files, which it will attempt to load into the server. You can
  78. customize this behaviour using the <literal>ldif</literal> attribute, which defines
  79. an LDIF resource to be loaded: <programlisting><![CDATA[
  80. <ldap-server ldif="classpath:users.ldif" />
  81. ]]></programlisting> This makes it a lot easier to get up and running with LDAP, since it
  82. can be inconvenient to work all the time with an external server. It also insulates
  83. the user from the complex bean configuration needed to wire up an Apache Directory
  84. server. Using plain Spring Beans the configuration would be much more cluttered. You
  85. must have the necessary Apache Directory dependency jars available for your
  86. application to use. These can be obtained from the LDAP sample application. </para>
  87. </section>
  88. <section>
  89. <info>
  90. <title>Using Bind Authentication</title>
  91. </info>
  92. <para> This is the most common LDAP authentication scenario. <programlisting><![CDATA[
  93. <ldap-authentication-provider user-dn-pattern="uid={0},ou=people"/>
  94. ]]></programlisting> This simple example would obtain the DN for the user by
  95. substituting the user login name in the supplied pattern and attempting to bind as
  96. that user with the login password. This is OK if all your users are stored under a
  97. single node in the directory. If instead you wished to configure an LDAP search
  98. filter to locate the user, you could use the following: <programlisting><![CDATA[
  99. <ldap-authentication-provider user-search-filter="(uid={0})"
  100. user-search-base="ou=people"/>
  101. ]]></programlisting> If used with the server definition above, this would
  102. perform a search under the DN <literal>ou=people,dc=springframework,dc=org</literal>
  103. using the value of the <literal>user-search-filter</literal> attribute as a filter.
  104. Again the user login name is substituted for the parameter in the filter name. If
  105. <literal>user-search-base</literal> isn't supplied, the search will be performed
  106. from the root. </para>
  107. </section>
  108. <section>
  109. <info>
  110. <title>Loading Authorities</title>
  111. </info>
  112. <para> How authorities are loaded from groups in the LDAP directory is controlled by the
  113. following attributes. <itemizedlist>
  114. <listitem>
  115. <para>
  116. <literal>group-search-base</literal>. Defines the part of the directory
  117. tree under which group searches should be performed.</para>
  118. </listitem>
  119. <listitem>
  120. <para>
  121. <literal>group-role-attribute</literal>. The attribute which contains
  122. the name of the authority defined by the group entry. Defaults to
  123. <literal>cn</literal>
  124. </para>
  125. </listitem>
  126. <listitem>
  127. <para>
  128. <literal>group-search-filter</literal>. The filter which is used to
  129. search for group membership. The default is
  130. <literal>uniqueMember={0}</literal>, corresponding to the
  131. <literal>groupOfUniqueMembers</literal> LDAP class. In this case,
  132. the substituted parameter is the full distinguished name of the user.
  133. The parameter <literal>{1}</literal> can be used if you want to filter
  134. on the login name.</para>
  135. </listitem>
  136. </itemizedlist> So if we used the following configuration <programlisting><![CDATA[
  137. <ldap-authentication-provider user-dn-pattern="uid={0},ou=people"
  138. group-search-base="ou=groups" />
  139. ]]></programlisting> and authenticated successfully as user <quote>ben</quote>, the subsequent
  140. loading of authorities would perform a search under the directory entry
  141. <literal>ou=groups,dc=springframework,dc=org</literal>, looking for entries
  142. which contain the attribute <literal>uniqueMember</literal> with value
  143. <literal>uid=ben,ou=people,dc=springframework,dc=org</literal>. By default the
  144. authority names will have the prefix <literal>ROLE_</literal> prepended. You can
  145. change this using the <literal>role-prefix</literal> attribute. If you don't want
  146. any prefix, use <literal>role-prefix="none"</literal>. For more information on
  147. loading authorities, see the Javadoc for the
  148. <classname>DefaultLdapAuthoritiesPopulator</classname> class. </para>
  149. </section>
  150. </section>
  151. <section>
  152. <info>
  153. <title>Implementation Classes</title>
  154. </info>
  155. <para>The namespace configuration options we've used above are simple to use and much more
  156. concise than using Spring beans explicitly. There are situations when you may need to
  157. know how to configure Spring Security LDAP directly in your application context. You may
  158. wish to customize the behaviour of some of the classes, for example. If you're happy
  159. using namespace configuration then you can skip this section and the next one. </para>
  160. <para> The main LDAP provider class, <classname>LdapAuthenticationProvider</classname>,
  161. doesn't actually do much itself but delegates the work to two other beans, an
  162. <interfacename>LdapAuthenticator</interfacename> and an
  163. <interfacename>LdapAuthoritiesPopulator</interfacename> which are responsible for
  164. authenticating the user and retrieving the user's set of
  165. <interfacename>GrantedAuthority</interfacename>s respectively.</para>
  166. <section xml:id="ldap-ldap-authenticators">
  167. <info>
  168. <title>LdapAuthenticator Implementations</title>
  169. </info>
  170. <para>The authenticator is also responsible for retrieving any required user attributes.
  171. This is because the permissions on the attributes may depend on the type of
  172. authentication being used. For example, if binding as the user, it may be necessary
  173. to read them with the user's own permissions.</para>
  174. <para>There are currently two authentication strategies supplied with Spring Security: <itemizedlist>
  175. <listitem>
  176. <para>Authentication directly to the LDAP server ("bind"
  177. authentication).</para>
  178. </listitem>
  179. <listitem>
  180. <para>Password comparison, where the password supplied by the user is
  181. compared with the one stored in the repository. This can either be done
  182. by retrieving the value of the password attribute and checking it
  183. locally or by performing an LDAP "compare" operation, where the supplied
  184. password is passed to the server for comparison and the real password
  185. value is never retrieved.</para>
  186. </listitem>
  187. </itemizedlist></para>
  188. <section xml:id="ldap-ldap-authenticators-common">
  189. <info>
  190. <title>Common Functionality</title>
  191. </info>
  192. <para>Before it is possible to authenticate a user (by either strategy), the
  193. distinguished name (DN) has to be obtained from the login name supplied to the
  194. application. This can be done either by simple pattern-matching (by setting the
  195. <property>setUserDnPatterns</property> array property) or by setting the
  196. <property>userSearch</property> property. For the DN pattern-matching
  197. approach, a standard Java pattern format is used, and the login name will be
  198. substituted for the parameter <parameter>{0}</parameter>. The pattern should be
  199. relative to the DN that the configured
  200. <interfacename>SpringSecurityContextSource</interfacename> will bind to (see
  201. the section on <link linkend="ldap-context-source">connecting to the LDAP
  202. server</link> for more information on this). For example, if you are using
  203. an LDAP server with the URL
  204. <literal>ldap://monkeymachine.co.uk/dc=springframework,dc=org</literal>, and
  205. have a pattern <literal>uid={0},ou=greatapes</literal>, then a login name of
  206. "gorilla" will map to a DN
  207. <literal>uid=gorilla,ou=greatapes,dc=springframework,dc=org</literal>. Each
  208. configured DN pattern will be tried in turn until a match is found. For
  209. information on using a search, see the section on <link
  210. linkend="ldap-searchobjects">search objects</link> below. A combination of
  211. the two approaches can also be used - the patterns will be checked first and if
  212. no matching DN is found, the search will be used.</para>
  213. </section>
  214. <section xml:id="ldap-ldap-authenticators-bind">
  215. <info>
  216. <title>BindAuthenticator</title>
  217. </info>
  218. <para>The class <classname>BindAuthenticator</classname> in the package
  219. <filename>org.springframework.security.ldap.authentication</filename>
  220. implements the bind authentication strategy. It simply attempts to bind as the
  221. user.</para>
  222. </section>
  223. <section xml:id="ldap-ldap-authenticators-password">
  224. <info>
  225. <title>PasswordComparisonAuthenticator</title>
  226. </info>
  227. <para>The class <classname>PasswordComparisonAuthenticator</classname> implements
  228. the password comparison authentication strategy.</para>
  229. </section>
  230. <section xml:id="ldap-ldap-authenticators-active-directory">
  231. <info>
  232. <title>Active Directory Authentication</title>
  233. </info>
  234. <para>In addition to standard LDAP authentication (binding with a DN), Active
  235. Directory has its own non-standard syntax for user authentication.</para>
  236. </section>
  237. </section>
  238. <section xml:id="ldap-context-source">
  239. <info>
  240. <title>Connecting to the LDAP Server</title>
  241. </info>
  242. <para>The beans discussed above have to be able to connect to the server. They both have
  243. to be supplied with a <interfacename>SpringSecurityContextSource</interfacename>
  244. which is an extension of Spring LDAP's <interfacename>ContextSource</interfacename>.
  245. Unless you have special requirements, you will usually configure a
  246. <classname>DefaultSpringSecurityContextSource</classname> bean, which can be
  247. configured with the URL of your LDAP server and optionally with the username and
  248. password of a "manager" user which will be used by default when binding to the
  249. server (instead of binding anonymously). For more information read the Javadoc for
  250. this class and for Spring LDAP's <classname>AbstractContextSource</classname>.
  251. </para>
  252. </section>
  253. <section xml:id="ldap-searchobjects">
  254. <info>
  255. <title>LDAP Search Objects</title>
  256. </info>
  257. <para>Often a more complicated strategy than simple DN-matching is required to
  258. locate a user entry in the directory. This can be encapsulated in an
  259. <interfacename>LdapUserSearch</interfacename> instance which can be supplied to
  260. the authenticator implementations, for example, to allow them to locate a user. The
  261. supplied implementation is <classname>FilterBasedLdapUserSearch</classname>.</para>
  262. <section xml:id="ldap-searchobjects-filter">
  263. <info>
  264. <title xml:id="ldap-searchobjects-filter-based">
  265. <classname>FilterBasedLdapUserSearch</classname>
  266. </title>
  267. </info>
  268. <para>This bean uses an LDAP filter to match the user object in the directory. The
  269. process is explained in the Javadoc for the corresponding search method on the
  270. <link xmlns:xlink="http://www.w3.org/1999/xlink"
  271. xlink:href="http://java.sun.com/j2se/1.4.2/docs/api/javax/naming/directory/DirContext.html#search(javax.naming.Name,%20java.lang.String,%20java.lang.Object[],%20javax.naming.directory.SearchControls)"
  272. >JDK DirContext class</link>. As explained there, the search filter can be
  273. supplied with parameters. For this class, the only valid parameter is
  274. <parameter>{0}</parameter> which will be replaced with the user's login
  275. name.</para>
  276. </section>
  277. </section>
  278. <section xml:id="ldap-authorities">
  279. <title>LdapAuthoritiesPopulator</title>
  280. <para> After authenticating the user successfully, the
  281. <classname>LdapAuthenticationProvider</classname> will attempt to load a set of
  282. authorities for the user by calling the configured
  283. <interfacename>LdapAuthoritiesPopulator</interfacename> bean. The
  284. <classname>DefaultLdapAuthoritiesPopulator</classname> is an implementation
  285. which will load the authorities by searching the directory for groups of which the
  286. user is a member (typically these will be <literal>groupOfNames</literal> or
  287. <literal>groupOfUniqueNames</literal> entries in the directory). Consult the
  288. Javadoc for this class for more details on how it works. </para>
  289. <para>If you want to use LDAP only for authentication, but load the authorities from a
  290. difference source (such as a database) then you can provide your own implementation
  291. of this interface and inject that instead.</para>
  292. </section>
  293. <section xml:id="ldap-bean-config">
  294. <info>
  295. <title>Spring Bean Configuration</title>
  296. </info>
  297. <para>A typical configuration, using some of the beans we've discussed here, might look
  298. like this: <programlisting><![CDATA[
  299. <bean id="contextSource"
  300. class="org.springframework.security.ldap.DefaultSpringSecurityContextSource">
  301. <constructor-arg value="ldap://monkeymachine:389/dc=springframework,dc=org"/>
  302. <property name="userDn" value="cn=manager,dc=springframework,dc=org"/>
  303. <property name="password" value="password"/>
  304. </bean>
  305. <bean id="ldapAuthProvider"
  306. class="org.springframework.security.ldap.authentication.LdapAuthenticationProvider">
  307. <constructor-arg>
  308. <bean class="org.springframework.security.ldap.authentication.BindAuthenticator">
  309. <constructor-arg ref="contextSource"/>
  310. <property name="userDnPatterns">
  311. <list><value>uid={0},ou=people</value></list>
  312. </property>
  313. </bean>
  314. </constructor-arg>
  315. <constructor-arg>
  316. <bean
  317. class="org.springframework.security.ldap.userdetails.DefaultLdapAuthoritiesPopulator">
  318. <constructor-arg ref="contextSource"/>
  319. <constructor-arg value="ou=groups"/>
  320. <property name="groupRoleAttribute" value="ou"/>
  321. </bean>
  322. </constructor-arg>
  323. </bean>]]>
  324. </programlisting> This would set up the provider to access an LDAP server
  325. with URL <literal>ldap://monkeymachine:389/dc=springframework,dc=org</literal>.
  326. Authentication will be performed by attempting to bind with the DN
  327. <literal>uid=&lt;user-login-name&gt;,ou=people,dc=springframework,dc=org</literal>.
  328. After successful authentication, roles will be assigned to the user by searching
  329. under the DN <literal>ou=groups,dc=springframework,dc=org</literal> with the default
  330. filter <literal>(member=&lt;user's-DN&gt;)</literal>. The role name will be taken
  331. from the <quote>ou</quote> attribute of each match.</para>
  332. <para>To configure a user search object, which uses the filter
  333. <literal>(uid=&lt;user-login-name&gt;)</literal> for use instead of the
  334. DN-pattern (or in addition to it), you would configure the following bean <programlisting><![CDATA[
  335. <bean id="userSearch"
  336. class="org.springframework.security.ldap.search.FilterBasedLdapUserSearch">
  337. <constructor-arg index="0" value=""/>
  338. <constructor-arg index="1" value="(uid={0})"/>
  339. <constructor-arg index="2" ref="contextSource" />
  340. </bean> ]]>
  341. </programlisting> and use it by setting the
  342. <classname>BindAuthenticator</classname> bean's <property>userSearch</property>
  343. property. The authenticator would then call the search object to obtain the correct
  344. user's DN before attempting to bind as this user.</para>
  345. </section>
  346. <section xml:id="ldap-custom-user-details">
  347. <title>LDAP Attributes and Customized UserDetails</title>
  348. <para> The net result of an authentication using
  349. <classname>LdapAuthenticationProvider</classname> is the same as a normal Spring
  350. Security authentication using the standard
  351. <interfacename>UserDetailsService</interfacename> interface. A
  352. <interfacename>UserDetails</interfacename> object is created and stored in the
  353. returned <interfacename>Authentication</interfacename> object. As with using a
  354. <interfacename>UserDetailsService</interfacename>, a common requirement is to be
  355. able to customize this implementation and add extra properties. When using LDAP,
  356. these will normally be attributes from the user entry. The creation of the
  357. <interfacename>UserDetails</interfacename> object is controlled by the
  358. provider's <interfacename>UserDetailsContextMapper</interfacename> strategy, which
  359. is responsible for mapping user objects to and from LDAP context data: <programlisting><![CDATA[
  360. public interface UserDetailsContextMapper {
  361. UserDetails mapUserFromContext(DirContextOperations ctx, String username,
  362. Collection<GrantedAuthority> authorities);
  363. void mapUserToContext(UserDetails user, DirContextAdapter ctx);
  364. }]]>
  365. </programlisting> Only the first method is relevant for authentication. If you
  366. provide an implementation of this interface and inject it into the
  367. <classname>LdapAuthenticationProvider</classname>, you have control over exactly how
  368. the UserDetails object is created. The first parameter is an instance of Spring
  369. LDAP's <interfacename>DirContextOperations</interfacename> which gives you access to
  370. the LDAP attributes which were loaded during authentication. The
  371. <literal>username</literal> parameter is the name used to authenticate and the final
  372. parameter is the collection of authorities loaded for the user by the configured
  373. <interfacename>LdapAuthoritiesPopulator</interfacename>. </para>
  374. <para> The way the context data is loaded varies slightly depending on the type of
  375. authentication you are using. With the <classname>BindAuthenticator</classname>, the
  376. context returned from the bind operation will be used to read the attributes,
  377. otherwise the data will be read using the standard context obtained from the
  378. configured <interfacename>ContextSource</interfacename> (when a search is configured
  379. to locate the user, this will be the data returned by the search object). </para>
  380. </section>
  381. </section>
  382. </chapter>