First step you need have domain model User to store users and then create Repositoy and Service layer that find user by username from database and then create UserDetailService like this
@Component("userDetailsService")
public class CustomUserDetailsService implements org.springframework.security.core.userdetails.UserDetailsService {
@Autowired
private IUserService userService;
private final Logger log = LoggerFactory.getLogger(CustomUserDetailsService.class);
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
User userEntity = userService.findByUsername(username);
if (userEntity != null) {
log.debug("------------==" + userEntity.getUsername());
log.debug("------------==" + userEntity.getPassword());
} else if (userEntity == null)
throw new UsernameNotFoundException("user not found");
return userEntity;
}
}
and the set UserDetailService in Security Configuration like this
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Inject
@Qualifier("userDetailsService")
private UserDetailsService userDetailsService;
@Inject
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth
.userDetailsService(userDetailsService);
}
@Override
public void configure(WebSecurity web) throws Exception {
}
}