Search code examples
javaldapjaas

Set userProvider dynamically at runtime for JAAS logins to LDAP


I have a J2SE application that is setup correctly to handle user authentication with an LDAP server. However, the URL is hard-coded in the config file.

e.g. userProvider="ldaps://host.domain.com:3269/DC=domain,DC=com"

I would like to move toward dynamically finding an LDAP server by querying DNS SRV records.

Once I find an LDAP server, how do I set the JAAS userProvider property dynamically at runtime without specifying it in the config file? All the examples I see only show the URL coming from the config file. The JavaDocs aren't too clear on this question.


Solution

  • Thanks to EJP, I got on the right track. Fortunately, System variables can be interpolated in the config file. (Reference)

    Therefore, I can do something like this in the config file:

    userProvider="ldaps://${ldap.host}/DC=domain,DC=com"
    

    In my Java code, I set the system property "ldap.host":

    System.setProperty("ldap.host","host.domain.com:3269");