2

Spring Boot + Spring Security with Multiple Login Forms and User Types

Application Requirement

I’m working on some personal project that handles 4 different user types (e.g., students, instructors, registrar employees, security personnel). Each user type is stored in a separate user table. With these, I require four (4) login forms.

Software Requirement

For this blog, the following are used.

  1. IntelliJ IDEA (optional)
  2. JDK 8
  3. Spring Initizr (http://start.spring.io/)
  4. Spring Boot  1.4.2.RELEASE
    1. Thymeleaf
    2. Spring Security

The codes

Login Form

Using Thymeleaf, we have this studentLogin.html file. Now notice line # 17. We specify the type of user using a hidden field.

For other user types, you may need to specify different user types in a similar way.

Custom Filter

We need a filter to get the additional parameter from the HttpServletRequest and set it as a HttpSession attribute.

Then include this new filter in the filter chain by modifying your security configuration Java file.

UserDetailService Implementation

You would then retrieve that parameter using RequestContextHolder.

Karl San Gabriel

Karl San Gabriel

Java and Enterprise Technologies Expert