To configure multiple entry points, you must use DelegatingAuthenticationEntryPoint . This means that you can have several authentication methods. The following is sample code:
DBUser entry point:
public class DBUserAuthencticationEntryPoint extends BasicAuthenticationEntryPoint { @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { super.commence(request, response, authException); } }
LDAP Entry Point:
public class LDAPAuthencticationEntryPoint extends BasicAuthenticationEntryPoint { @Override public void commence(HttpServletRequest request, HttpServletResponse response, AuthenticationException authException) throws IOException, ServletException { super.commence(request, response, authException); } }
Then you need to create a RequestMatcher to select the correct entry point (based on the header / area name):
DBUser Query Connector:
RequestMatcher dbUserMatcher = new RequestMatcher() { @Override public boolean matches(HttpServletRequest request) {
Requset negotiator for LDAP user:
RequestMatcher ldapMatcher = new RequestMatcher() { @Override public boolean matches(HttpServletRequest request) {
Now we need to add these matches and entry points to the DelegatingAuthenticationEntryPoint . At run time, DelegatingAuthenticationEntryPoint selects an entry point and performs authentication based on a connector that returns true .
DBUserAuthencticationEntryPoint dbUserEntryPoint = new DBUserAuthencticationEntryPoint(); LDAPAuthencticationEntryPoint ldapEntryPoint = new LDAPAuthencticationEntryPoint(); LinkedHashMap<RequestMatcher,AuthenticationEntryPoint> entryPoints = new LinkedHashMap<RequestMatcher,AuthenticationEntryPoint>(); entryPoints.put(ldapMatcher, ldapEntryPoint); entryPoints.put(dbUserMatcher, dbUserEntryPoint); DelegatingAuthenticationEntryPoint delegatingAuthenticationEntryPoint = new DelegatingAuthenticationEntryPoint(entryPoints);
Now map the DelegatingAuthenticationEntryPoint to the HttpSecurity in the configure() method:
@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { @Override protected void configure(HttpSecurity http) throws Exception { http. authorizeRequests(). regexMatchers("/login.*").permitAll(). regexMatchers("/api.*").fullyAuthenticated(). and(). formLogin().loginPage("/login"). and(). exceptionHandling().authenticationEntryPoint(delegatingAuthenticationEntryPoint); } }
Configure Provider Manager:
@Bean public AuthenticationManager authenticationManager() { return new ProviderManager(Arrays.asList(provider1, provider2); }