Spring Security Custom Authentication Provider

I recently had the requirement for user authentication in a spring boot application as follows:

Check if the user exist in local database user table
-> if the user exist, then authenticate it to external windows AD server.

In Spring Security, an Authentication request is processed by an AuthenticationProvider and a fully authenticated object with full credentials is returned. Some standard and common implementation of Authentication Provider are DaoAuthenticationProvider.

Here, we’ll define a custom Authentication Provider to achieve our authentication requirement.

Suppose the environment are as follows:

  • Local db details :Postgresql 13.1, host- server1, port 5432, database — mydb1, users table — myuser
  • Windows AD server details: domain — example.examplegroup.co.in, IP Address — 192.168.1.25

Steps:

  1. Add following dependencies to pom.xml file

2. Modify application.properties files

3. Add domain class for user — Myuser.class

4. Add Repo interface for user — MyuserRepo.class

5. Add service class for user — MyuserService.class

6. Create the custom authentication provider

Inside Custom Authentication provider first we will check whether user is present in myuser table and then will use ActiveDirectoryLdapAuthenticationProvider to perform windows AD authentication.

CustomAuthenticationProvider must override two methods

  • Authentication authenticate(Authentication authentication)
  • boolean supports(Class<?> authentication)

7. Define WebSecurityConfig class in which our custom authentication builder will be utilized.

Resources :

https://www.baeldung.com/spring-security-authentication-provider

--

--

System Administrator and Full stack web developer.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store