I was recently working on a project where there was a need to create custom web services that would connect to Alfresco and other back end systems. For a variety of reasons, it was decided to use Spring Boot as the framework for implementing these web services. One of the requirements was to use HTTP basic authentication when calling the web services and authenticate the user against Active Directory (AD) making sure that the user was also a member of specific group(s). This seems like a very simple requirement and my first thought was to use Spring Security with annotations to do this.

The key to implementing this was to create a new ActiveDirectoryLdapAuthenticationProvider when configuring the AuthenticationManagerBuilder and applying that with the correct settings. This is done by creating a class to extend WebSecurityConfigurerAdapter and overriding the configure(AuthenticationManagerBuilder auth) method. The setSearchFilter() method is used to change the LDAP query from the default to whatever is set in the application.properties.

The default user search filter is:

(&(objectClass=user)(userPrincipalName={0})

The user search filter can be changed as needed. It is an LDAP query that replaces the userPrincipalName substitution {0} with username@domain. In this case, a check was added to see if the user is also a member of a specific group using a “memberof” attribute. This may be different depending on how you have set up Active Directory. Use a tool like Apache Directory Studio to test your query.

(&(objectClass=user)(userPrincipalName={0})(memberof=CN=ServiceAccounts,OU=alfresco,DC=mycompany,DC=com))

A final change made in the overriding method was to set erase credentials to false ( “auth.eraseCredentials(false);” ) in order to make the password available for later use in the web service. The user name and password were needed to make web services calls to Alfresco. By default, the password is cleared.

The below code snippet shows how the user name and password were obtained. This is used within the service that makes calls to Alfresco.

Complete class used to configure Spring Security for the project.

Using Spring Security makes it very easy to add Active Directory support with HTTP basic authentication.

To learn more, or for help with your ECM system, contact us today.

Pin It on Pinterest

Sharing is caring

Share this post with your friends!