I am doing my B.Tech project and everything was going well but now I am stuck in configuring authentication for two different types of users i.e Customers and Service Providers. I am using MongoDB.
I have two different databases for each user. I am trying to create multiple login pages that would authenticate the user from their respective databases. I am using order(1) and order(2) for configuration but only order(1) is working.
This is my configuration code.
@Configuration
@EnableWebSecurity
public class MultiLoginConfig {
@Configuration
@Order(1)
public static class DearHelpUserSecConfig extends WebSecurityConfigurerAdapter{
@Override
@Bean
protected UserDetailsService userDetailsService() {
return new CustomUserDetailsService();
}
@Bean
public static NoOpPasswordEncoder passwordEncoder() {
return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers( "/home").permitAll()
.antMatchers("/hellouser").access("hasRole('USER')")
.and()
.formLogin()
.loginPage("/login1")
.permitAll()
.and()
.logout()
.permitAll()
.logoutUrl("/logout").
logoutSuccessUrl("/home")
.and()
.userDetailsService(userDetailsService());
}
}
@Configuration
@Order(2)
public static class DearHelpSPSecConfig extends WebSecurityConfigurerAdapter{
@Override
@Bean
protected UserDetailsService userDetailsService() {
return new SPUserDetailsService();
}
@Bean
public static NoOpPasswordEncoder passwordEncoder() {
return (NoOpPasswordEncoder) NoOpPasswordEncoder.getInstance();
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers( "/home").permitAll()
.antMatchers("/hellosp").access("hasRole('SP')")
.and()
.formLogin()
.loginPage("/login2")
.permitAll()
.and()
.logout()
.permitAll()
.logoutUrl("/logout").
logoutSuccessUrl("/home")
.and()
.userDetailsService(userDetailsService());
}
}
}
I am implementing Custom UserDetailsService for each user.
Custom implementation for Customers UserDetailsServices is..
public class CustomUserDetailsService implements UserDetailsService {
@Autowired
private MongoTemplate mongoTemplate;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
Query query = new Query();
query.addCriteria(Criteria.where("email").is(email));
DearHelpUsers user =
mongoTemplate.findOne(query, DearHelpUsers.class);
if (user == null) {
throw new UsernameNotFoundException(String.format("email %s not found", email));
}
return new User(user.getEmail(), user.getPassword(),
AuthorityUtils.createAuthorityList(user.getRole()));
}
}
Custom implementation for Service Providers UserDetailsServices is..
public class SPUserDetailsService implements UserDetailsService {
@Autowired
private MongoTemplate mongoTemplate;
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
Query query = new Query();
query.addCriteria(Criteria.where("email").is(email));
ServiceProviders user =
mongoTemplate.findOne(query, ServiceProviders.class);
System.out.println(user);
if (user == null) {
throw new UsernameNotFoundException(String.format("email %s not found", email));
}
return new User(user.getEmail(), user.getPassword(),
AuthorityUtils.createAuthorityList(user.getRole()));
}
}
When I am trying to access Customers page i.e "/hellouser", the login page is popping out and authentication is working fine. But When I am trying to access Service provider page i.e "/hellosp", it is accessible without logging in the user. Authorization for Service providers is not working. I tried changing the order and observed that authorization for only order(1) code is working but order(2) is not working. Where am I doing wrong? Any help would be highly appreciated. Thank you
UserDetailsServicethat distinguishes them internally somehow.