ldap.adoc 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. [[servlet-authentication-ldap]]
  2. = LDAP Authentication
  3. LDAP is often used by organizations as a central repository for user information and as an authentication service.
  4. It can also be used to store the role information for application users.
  5. Spring Security's LDAP based authentication is used by Spring Security when it is configured to xref:servlet/authentication/passwords/index.adoc#servlet-authentication-unpwd-input[accept a username/password] for authentication.
  6. However, despite leveraging a username/password for authentication it does not integrate using `UserDetailsService` because in <<servlet-authentication-ldap-bind,bind authentication>> the LDAP server does not return the password so the application cannot perform validation of the password.
  7. There are many different scenarios for how an LDAP server may be configured so Spring Security's LDAP provider is fully configurable.
  8. It uses separate strategy interfaces for authentication and role retrieval and provides default implementations which can be configured to handle a wide range of situations.
  9. [[servlet-authentication-ldap-required-dependencies]]
  10. == Required Dependencies
  11. To get started, add the `spring-security-ldap` dependency to your project.
  12. When using Spring Boot, add the following dependencies:
  13. .Spring Security LDAP Dependencies
  14. [tabs]
  15. ======
  16. Maven::
  17. +
  18. [source,xml,role="primary"]
  19. ----
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-data-ldap</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.security</groupId>
  26. <artifactId>spring-security-ldap</artifactId>
  27. </dependency>
  28. ----
  29. Gradle::
  30. +
  31. [source,groovy,role="secondary"]
  32. ----
  33. depenendencies {
  34. implementation "org.springframework.boot:spring-boot-starter-data-ldap"
  35. implementation "org.springframework.security:spring-security-ldap"
  36. }
  37. ----
  38. ======
  39. [[servlet-authentication-ldap-prerequisites]]
  40. == Prerequisites
  41. You should be familiar with LDAP before trying to use it with Spring Security.
  42. The following link provides a good introduction to the concepts involved and a guide to setting up a directory using the free LDAP server OpenLDAP: https://www.zytrax.com/books/ldap/.
  43. Some familiarity with the JNDI APIs used to access LDAP from Java may also be useful.
  44. We don't use any third-party LDAP libraries (Mozilla, JLDAP etc.) in the LDAP provider, but extensive use is made of Spring LDAP, so some familiarity with that project may be useful if you plan on adding your own customizations.
  45. When using LDAP authentication, it is important to ensure that you configure LDAP connection pooling properly.
  46. If you are unfamiliar with how to do this, you can refer to the https://docs.oracle.com/javase/jndi/tutorial/ldap/connect/config.html[Java LDAP documentation].
  47. // FIXME:
  48. // ldap server
  49. // embedded (both java and xml)
  50. // external
  51. // authentication
  52. // bind
  53. // password
  54. // roles
  55. // search, etc (other APIs)
  56. [[servlet-authentication-ldap-embedded]]
  57. == Setting up an Embedded LDAP Server
  58. The first thing you will need to do is to ensure that you have an LDAP Server to point your configuration to.
  59. For simplicity, it is often best to start with an embedded LDAP Server.
  60. Spring Security supports using either:
  61. * <<servlet-authentication-ldap-unboundid>>
  62. * <<servlet-authentication-ldap-apacheds>>
  63. In the samples below, we expose the following as `users.ldif` as a classpath resource to initialize the embedded LDAP server with the users `user` and `admin` both of which have a password of `password`.
  64. .users.ldif
  65. [source,ldif]
  66. ----
  67. dn: ou=groups,dc=springframework,dc=org
  68. objectclass: top
  69. objectclass: organizationalUnit
  70. ou: groups
  71. dn: ou=people,dc=springframework,dc=org
  72. objectclass: top
  73. objectclass: organizationalUnit
  74. ou: people
  75. dn: uid=admin,ou=people,dc=springframework,dc=org
  76. objectclass: top
  77. objectclass: person
  78. objectclass: organizationalPerson
  79. objectclass: inetOrgPerson
  80. cn: Rod Johnson
  81. sn: Johnson
  82. uid: admin
  83. userPassword: password
  84. dn: uid=user,ou=people,dc=springframework,dc=org
  85. objectclass: top
  86. objectclass: person
  87. objectclass: organizationalPerson
  88. objectclass: inetOrgPerson
  89. cn: Dianne Emu
  90. sn: Emu
  91. uid: user
  92. userPassword: password
  93. dn: cn=user,ou=groups,dc=springframework,dc=org
  94. objectclass: top
  95. objectclass: groupOfNames
  96. cn: user
  97. member: uid=admin,ou=people,dc=springframework,dc=org
  98. member: uid=user,ou=people,dc=springframework,dc=org
  99. dn: cn=admin,ou=groups,dc=springframework,dc=org
  100. objectclass: top
  101. objectclass: groupOfNames
  102. cn: admin
  103. member: uid=admin,ou=people,dc=springframework,dc=org
  104. ----
  105. [[servlet-authentication-ldap-unboundid]]
  106. === Embedded UnboundID Server
  107. If you wish to use https://ldap.com/unboundid-ldap-sdk-for-java/[UnboundID], then specify the following dependencies:
  108. .UnboundID Dependencies
  109. [tabs]
  110. ======
  111. Maven::
  112. +
  113. [source,xml,role="primary",subs="verbatim,attributes"]
  114. ----
  115. <dependency>
  116. <groupId>com.unboundid</groupId>
  117. <artifactId>unboundid-ldapsdk</artifactId>
  118. <version>{unboundid-ldapsdk-version}</version>
  119. <scope>runtime</scope>
  120. </dependency>
  121. ----
  122. Gradle::
  123. +
  124. [source,groovy,role="secondary",subs="verbatim,attributes"]
  125. ----
  126. depenendencies {
  127. runtimeOnly "com.unboundid:unboundid-ldapsdk:{unboundid-ldapsdk-version}"
  128. }
  129. ----
  130. ======
  131. You can then configure the Embedded LDAP Server using an `EmbeddedLdapServerContextSourceFactoryBean`.
  132. This will instruct Spring Security to start an in-memory LDAP server.
  133. .Embedded LDAP Server Configuration
  134. [tabs]
  135. ======
  136. Java::
  137. +
  138. [source,java,role="primary"]
  139. ----
  140. @Bean
  141. public EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
  142. return EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer();
  143. }
  144. ----
  145. Kotlin::
  146. +
  147. [source,kotlin,role="secondary"]
  148. ----
  149. @Bean
  150. fun contextSourceFactoryBean(): EmbeddedLdapServerContextSourceFactoryBean {
  151. return EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer()
  152. }
  153. ----
  154. ======
  155. Alternatively, you can manually configure the Embedded LDAP Server.
  156. If you choose this approach, you will be responsible for managing the lifecycle of the Embedded LDAP Server.
  157. .Explicit Embedded LDAP Server Configuration
  158. [tabs]
  159. ======
  160. Java::
  161. +
  162. [source,java,role="primary"]
  163. ----
  164. @Bean
  165. UnboundIdContainer ldapContainer() {
  166. return new UnboundIdContainer("dc=springframework,dc=org",
  167. "classpath:users.ldif");
  168. }
  169. ----
  170. XML::
  171. +
  172. [source,xml,role="secondary"]
  173. ----
  174. <b:bean class="org.springframework.security.ldap.server.UnboundIdContainer"
  175. c:defaultPartitionSuffix="dc=springframework,dc=org"
  176. c:ldif="classpath:users.ldif"/>
  177. ----
  178. Kotlin::
  179. +
  180. [source,kotlin,role="secondary"]
  181. ----
  182. @Bean
  183. fun ldapContainer(): UnboundIdContainer {
  184. return UnboundIdContainer("dc=springframework,dc=org","classpath:users.ldif")
  185. }
  186. ----
  187. ======
  188. [[servlet-authentication-ldap-apacheds]]
  189. === Embedded ApacheDS Server
  190. [NOTE]
  191. ====
  192. Spring Security uses ApacheDS 1.x which is no longer maintained.
  193. Unfortunately, ApacheDS 2.x has only released milestone versions with no stable release.
  194. Once a stable release of ApacheDS 2.x is available, we will consider updating.
  195. ====
  196. If you wish to use https://directory.apache.org/apacheds/[Apache DS], then specify the following dependencies:
  197. .ApacheDS Dependencies
  198. [tabs]
  199. ======
  200. Maven::
  201. +
  202. [source,xml,role="primary",subs="+attributes"]
  203. ----
  204. <dependency>
  205. <groupId>org.apache.directory.server</groupId>
  206. <artifactId>apacheds-core</artifactId>
  207. <version>{apacheds-core-version}</version>
  208. <scope>runtime</scope>
  209. </dependency>
  210. <dependency>
  211. <groupId>org.apache.directory.server</groupId>
  212. <artifactId>apacheds-server-jndi</artifactId>
  213. <version>{apacheds-core-version}</version>
  214. <scope>runtime</scope>
  215. </dependency>
  216. ----
  217. Gradle::
  218. +
  219. [source,groovy,role="secondary",subs="+attributes"]
  220. ----
  221. depenendencies {
  222. runtimeOnly "org.apache.directory.server:apacheds-core:{apacheds-core-version}"
  223. runtimeOnly "org.apache.directory.server:apacheds-server-jndi:{apacheds-core-version}"
  224. }
  225. ----
  226. ======
  227. You can then configure the Embedded LDAP Server
  228. .Embedded LDAP Server Configuration
  229. [tabs]
  230. ======
  231. Java::
  232. +
  233. [source,java,role="primary"]
  234. ----
  235. @Bean
  236. ApacheDSContainer ldapContainer() {
  237. return new ApacheDSContainer("dc=springframework,dc=org",
  238. "classpath:users.ldif");
  239. }
  240. ----
  241. XML::
  242. +
  243. [source,xml,role="secondary"]
  244. ----
  245. <b:bean class="org.springframework.security.ldap.server.ApacheDSContainer"
  246. c:defaultPartitionSuffix="dc=springframework,dc=org"
  247. c:ldif="classpath:users.ldif"/>
  248. ----
  249. Kotlin::
  250. +
  251. [source,kotlin,role="secondary"]
  252. ----
  253. @Bean
  254. fun ldapContainer(): ApacheDSContainer {
  255. return ApacheDSContainer("dc=springframework,dc=org", "classpath:users.ldif")
  256. }
  257. ----
  258. ======
  259. [[servlet-authentication-ldap-contextsource]]
  260. == LDAP ContextSource
  261. Once you have an LDAP Server to point your configuration to, you need configure Spring Security to point to an LDAP server that should be used to authenticate users.
  262. This is done by creating an LDAP `ContextSource`, which is the equivalent of a JDBC `DataSource`.
  263. If you have already configured an `EmbeddedLdapServerContextSourceFactoryBean`, Spring Security will create an LDAP `ContextSource` that points to the embedded LDAP server.
  264. .LDAP Context Source with Embedded LDAP Server
  265. [tabs]
  266. ======
  267. Java::
  268. +
  269. [source,java,role="primary"]
  270. ----
  271. @Bean
  272. public EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean() {
  273. EmbeddedLdapServerContextSourceFactoryBean contextSourceFactoryBean =
  274. EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer();
  275. contextSourceFactoryBean.setPort(0);
  276. return contextSourceFactoryBean;
  277. }
  278. ----
  279. Kotlin::
  280. +
  281. [source,kotlin,role="secondary"]
  282. ----
  283. @Bean
  284. fun contextSourceFactoryBean(): EmbeddedLdapServerContextSourceFactoryBean {
  285. val contextSourceFactoryBean = EmbeddedLdapServerContextSourceFactoryBean.fromEmbeddedLdapServer()
  286. contextSourceFactoryBean.setPort(0)
  287. return contextSourceFactoryBean
  288. }
  289. ----
  290. ======
  291. Alternatively, you can explicitly configure the LDAP `ContextSource` to connect to the supplied LDAP server.
  292. .LDAP Context Source
  293. [tabs]
  294. ======
  295. Java::
  296. +
  297. [source,java,role="primary"]
  298. ----
  299. ContextSource contextSource(UnboundIdContainer container) {
  300. return new DefaultSpringSecurityContextSource("ldap://localhost:53389/dc=springframework,dc=org");
  301. }
  302. ----
  303. XML::
  304. +
  305. [source,xml,role="secondary"]
  306. ----
  307. <ldap-server
  308. url="ldap://localhost:53389/dc=springframework,dc=org" />
  309. ----
  310. Kotlin::
  311. +
  312. [source,kotlin,role="secondary"]
  313. ----
  314. fun contextSource(container: UnboundIdContainer): ContextSource {
  315. return DefaultSpringSecurityContextSource("ldap://localhost:53389/dc=springframework,dc=org")
  316. }
  317. ----
  318. ======
  319. [[servlet-authentication-ldap-authentication]]
  320. == Authentication
  321. Spring Security's LDAP support does not use the xref:servlet/authentication/passwords/user-details-service.adoc#servlet-authentication-userdetailsservice[UserDetailsService] because LDAP bind authentication does not allow clients to read the password or even a hashed version of the password.
  322. This means there is no way a password to be read and then authenticated by Spring Security.
  323. For this reason, LDAP support is implemented using the `LdapAuthenticator` interface.
  324. The `LdapAuthenticator` is also responsible for retrieving any required user attributes.
  325. This is because the permissions on the attributes may depend on the type of authentication being used.
  326. For example, if binding as the user, it may be necessary to read them with the user's own permissions.
  327. There are two `LdapAuthenticator` implementations supplied with Spring Security:
  328. * <<servlet-authentication-ldap-bind>>
  329. * <<servlet-authentication-ldap-pwd>>
  330. [[servlet-authentication-ldap-bind]]
  331. == Using Bind Authentication
  332. https://ldap.com/the-ldap-bind-operation/[Bind Authentication] is the most common mechanism for authenticating users with LDAP.
  333. In bind authentication the users credentials (i.e. username/password) are submitted to the LDAP server which authenticates them.
  334. The advantage to using bind authentication is that the user's secrets (i.e. password) do not need to be exposed to clients which helps to protect them from leaking.
  335. An example of bind authentication configuration can be found below.
  336. .Bind Authentication
  337. [tabs]
  338. ======
  339. Java::
  340. +
  341. [source,java,role="primary",attrs="-attributes"]
  342. ----
  343. @Bean
  344. AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
  345. LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
  346. factory.setUserDnPatterns("uid={0},ou=people");
  347. return factory.createAuthenticationManager();
  348. }
  349. ----
  350. XML::
  351. +
  352. [source,xml,role="secondary",attrs="-attributes"]
  353. ----
  354. <ldap-authentication-provider
  355. user-dn-pattern="uid={0},ou=people"/>
  356. ----
  357. Kotlin::
  358. +
  359. [source,kotlin,role="secondary",attrs="-attributes"]
  360. ----
  361. @Bean
  362. fun authenticationManager(contextSource: BaseLdapPathContextSource): AuthenticationManager {
  363. val factory = LdapBindAuthenticationManagerFactory(contextSource)
  364. factory.setUserDnPatterns("uid={0},ou=people")
  365. return factory.createAuthenticationManager()
  366. }
  367. ----
  368. ======
  369. This simple example would obtain the DN for the user by substituting the user login name in the supplied pattern and attempting to bind as that user with the login password.
  370. This is OK if all your users are stored under a single node in the directory.
  371. If instead you wished to configure an LDAP search filter to locate the user, you could use the following:
  372. .Bind Authentication with Search Filter
  373. [tabs]
  374. ======
  375. Java::
  376. +
  377. [source,java,role="primary",attrs="-attributes"]
  378. ----
  379. @Bean
  380. AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
  381. LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
  382. factory.setUserSearchFilter("(uid={0})");
  383. factory.setUserSearchBase("ou=people");
  384. return factory.createAuthenticationManager();
  385. }
  386. ----
  387. XML::
  388. +
  389. [source,xml,role="secondary",attrs="-attributes"]
  390. ----
  391. <ldap-authentication-provider
  392. user-search-filter="(uid={0})"
  393. user-search-base="ou=people"/>
  394. ----
  395. Kotlin::
  396. +
  397. [source,kotlin,role="secondary",attrs="-attributes"]
  398. ----
  399. @Bean
  400. fun authenticationManager(contextSource: BaseLdapPathContextSource): AuthenticationManager {
  401. val factory = LdapBindAuthenticationManagerFactory(contextSource)
  402. factory.setUserSearchFilter("(uid={0})")
  403. factory.setUserSearchBase("ou=people")
  404. return factory.createAuthenticationManager()
  405. }
  406. ----
  407. ======
  408. If used with the `ContextSource` <<servlet-authentication-ldap-contextsource,definition above>>, this would perform a search under the DN `ou=people,dc=springframework,dc=org` using `+(uid={0})+` as a filter.
  409. Again the user login name is substituted for the parameter in the filter name, so it will search for an entry with the `uid` attribute equal to the user name.
  410. If a user search base isn't supplied, the search will be performed from the root.
  411. [[servlet-authentication-ldap-pwd]]
  412. == Using Password Authentication
  413. Password comparison is when the password supplied by the user is compared with the one stored in the repository.
  414. This can either be done by retrieving the value of the password attribute and checking it locally or by performing an LDAP "compare" operation, where the supplied password is passed to the server for comparison and the real password value is never retrieved.
  415. An LDAP compare cannot be done when the password is properly hashed with a random salt.
  416. .Minimal Password Compare Configuration
  417. [tabs]
  418. ======
  419. Java::
  420. +
  421. [source,java,role="primary"]
  422. ----
  423. @Bean
  424. AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
  425. LdapPasswordComparisonAuthenticationManagerFactory factory = new LdapPasswordComparisonAuthenticationManagerFactory(
  426. contextSource, NoOpPasswordEncoder.getInstance());
  427. factory.setUserDnPatterns("uid={0},ou=people");
  428. return factory.createAuthenticationManager();
  429. }
  430. ----
  431. XML::
  432. +
  433. [source,xml,role="secondary",attrs="-attributes"]
  434. ----
  435. <ldap-authentication-provider
  436. user-dn-pattern="uid={0},ou=people">
  437. <password-compare />
  438. </ldap-authentication-provider>
  439. ----
  440. Kotlin::
  441. +
  442. [source,kotlin,role="secondary"]
  443. ----
  444. @Bean
  445. fun authenticationManager(contextSource: BaseLdapPathContextSource?): AuthenticationManager? {
  446. val factory = LdapPasswordComparisonAuthenticationManagerFactory(
  447. contextSource, NoOpPasswordEncoder.getInstance()
  448. )
  449. factory.setUserDnPatterns("uid={0},ou=people")
  450. return factory.createAuthenticationManager()
  451. }
  452. ----
  453. ======
  454. A more advanced configuration with some customizations can be found below.
  455. .Password Compare Configuration
  456. [tabs]
  457. ======
  458. Java::
  459. +
  460. [source,java,role="primary"]
  461. ----
  462. @Bean
  463. AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource) {
  464. LdapPasswordComparisonAuthenticationManagerFactory factory = new LdapPasswordComparisonAuthenticationManagerFactory(
  465. contextSource, new BCryptPasswordEncoder());
  466. factory.setUserDnPatterns("uid={0},ou=people");
  467. factory.setPasswordAttribute("pwd"); // <1>
  468. return factory.createAuthenticationManager();
  469. }
  470. ----
  471. XML::
  472. +
  473. [source,xml,role="secondary",attrs="-attributes"]
  474. ----
  475. <ldap-authentication-provider
  476. user-dn-pattern="uid={0},ou=people">
  477. <password-compare password-attribute="pwd"> <!--1-->
  478. <password-encoder ref="passwordEncoder" /> <!--2-->
  479. </password-compare>
  480. </ldap-authentication-provider>
  481. <b:bean id="passwordEncoder"
  482. class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />
  483. ----
  484. Kotlin::
  485. +
  486. [source,kotlin,role="secondary"]
  487. ----
  488. @Bean
  489. fun authenticationManager(contextSource: BaseLdapPathContextSource): AuthenticationManager {
  490. val factory = LdapPasswordComparisonAuthenticationManagerFactory(
  491. contextSource, BCryptPasswordEncoder()
  492. )
  493. factory.setUserDnPatterns("uid={0},ou=people")
  494. factory.setPasswordAttribute("pwd") // <1>
  495. return factory.createAuthenticationManager()
  496. }
  497. ----
  498. ======
  499. <1> Specify the password attribute as `pwd`
  500. == LdapAuthoritiesPopulator
  501. Spring Security's `LdapAuthoritiesPopulator` is used to determine what authorites are returned for the user.
  502. .LdapAuthoritiesPopulator Configuration
  503. [tabs]
  504. ======
  505. Java::
  506. +
  507. [source,java,role="primary",attrs="-attributes"]
  508. ----
  509. @Bean
  510. LdapAuthoritiesPopulator authorities(BaseLdapPathContextSource contextSource) {
  511. String groupSearchBase = "";
  512. DefaultLdapAuthoritiesPopulator authorities =
  513. new DefaultLdapAuthoritiesPopulator(contextSource, groupSearchBase);
  514. authorities.setGroupSearchFilter("member={0}");
  515. return authorities;
  516. }
  517. @Bean
  518. AuthenticationManager authenticationManager(BaseLdapPathContextSource contextSource, LdapAuthoritiesPopulator authorities) {
  519. LdapBindAuthenticationManagerFactory factory = new LdapBindAuthenticationManagerFactory(contextSource);
  520. factory.setUserDnPatterns("uid={0},ou=people");
  521. factory.setLdapAuthoritiesPopulator(authorities);
  522. return factory.createAuthenticationManager();
  523. }
  524. ----
  525. XML::
  526. +
  527. [source,xml,role="secondary",attrs="-attributes"]
  528. ----
  529. <ldap-authentication-provider
  530. user-dn-pattern="uid={0},ou=people"
  531. group-search-filter="member={0}"/>
  532. ----
  533. Kotlin::
  534. +
  535. [source,kotlin,role="secondary",attrs="-attributes"]
  536. ----
  537. @Bean
  538. fun authorities(contextSource: BaseLdapPathContextSource): LdapAuthoritiesPopulator {
  539. val groupSearchBase = ""
  540. val authorities = DefaultLdapAuthoritiesPopulator(contextSource, groupSearchBase)
  541. authorities.setGroupSearchFilter("member={0}")
  542. return authorities
  543. }
  544. @Bean
  545. fun authenticationManager(
  546. contextSource: BaseLdapPathContextSource,
  547. authorities: LdapAuthoritiesPopulator): AuthenticationManager {
  548. val factory = LdapBindAuthenticationManagerFactory(contextSource)
  549. factory.setUserDnPatterns("uid={0},ou=people")
  550. factory.setLdapAuthoritiesPopulator(authorities)
  551. return factory.createAuthenticationManager()
  552. }
  553. ----
  554. ======
  555. == Active Directory
  556. Active Directory supports its own non-standard authentication options, and the normal usage pattern doesn't fit too cleanly with the standard `LdapAuthenticationProvider`.
  557. Typically authentication is performed using the domain username (in the form `user@domain`), rather than using an LDAP distinguished name.
  558. To make this easier, Spring Security has an authentication provider which is customized for a typical Active Directory setup.
  559. Configuring `ActiveDirectoryLdapAuthenticationProvider` is quite straightforward.
  560. You just need to supply the domain name and an LDAP URL supplying the address of the server footnote:[It is also possible to obtain the server's IP address using a DNS lookup.
  561. This is not currently supported, but hopefully will be in a future version.].
  562. An example configuration can be seen below:
  563. .Example Active Directory Configuration
  564. [tabs]
  565. ======
  566. Java::
  567. +
  568. [source,java,role="primary"]
  569. ----
  570. @Bean
  571. ActiveDirectoryLdapAuthenticationProvider authenticationProvider() {
  572. return new ActiveDirectoryLdapAuthenticationProvider("example.com", "ldap://company.example.com/");
  573. }
  574. ----
  575. XML::
  576. +
  577. [source,xml,role="secondary"]
  578. ----
  579. <bean id="authenticationProvider"
  580. class="org.springframework.security.ldap.authentication.ad.ActiveDirectoryLdapAuthenticationProvider">
  581. <constructor-arg value="example.com" />
  582. <constructor-arg value="ldap://company.example.com/" />
  583. </bean>
  584. ----
  585. Kotlin::
  586. +
  587. [source,kotlin,role="secondary"]
  588. ----
  589. @Bean
  590. fun authenticationProvider(): ActiveDirectoryLdapAuthenticationProvider {
  591. return ActiveDirectoryLdapAuthenticationProvider("example.com", "ldap://company.example.com/")
  592. }
  593. ----
  594. ======