@Service("clientUserDetailsService")
public class ClientUserDetailsService implements UserDetailsService {
@Autowired
private UserRepository users;
@Override
public UserDetails loadUserByUsername(String username)
throws UsernameNotFoundException {
Optional<ClientUser> optionalUser = users.findByUsername(username);
if (!optionalUser.isPresent()) {
throw new UsernameNotFoundException("invalid username or password");
}
return new ClientUserDetails(optionalUser.get());
}
}
@Configuration
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Autowired
@Qualifier("clientUserDetailsService")
private UserDetailsService userDetailsService;
@Override
protected void configure(AuthenticationManagerBuilder auth)
throws Exception {
auth.userDetailsService(userDetailsService);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests().antMatchers("/", "/index.html").permitAll().anyRequest().authenticated().and()
.formLogin().and()
.logout().permitAll().and()
.csrf().disable();
}
}