ldap.adoc 20 KB

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