@Override
    protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken usernamePasswordAuthenticationToken) throws AuthenticationException {
        String password = (String)usernamePasswordAuthenticationToken.getCredentials();

        if (!StringUtils.hasText(password)) {
            logger.warn("Username {}: no password provided", username);
            throw new BadCredentialsException("Please enter password");
        }

        User foundUser = userService.getUserByName(username);

        if (foundUser == null) {
            logger.warn("Username {}: no password provided", username);
            throw new UsernameNotFoundException("Invalid login");
        }

        if (!encoder.matches(password, foundUser.getPassword())){
            logger.warn("Password is incorrect!!");
            throw new BadCredentialsException("wrong password");
        }
        final List<GrantedAuthority> authorities;

        if (foundUser.getRole().compareTo(User.ROLES.ROLE_USER) == 0){
            authorities = AuthorityUtils.createAuthorityList(foundUser.getRole().toString());
        }else {
            authorities = AuthorityUtils.NO_AUTHORITIES;
        }
        return new org.springframework.security.core.userdetails.User(username, password,
                true,
                true,
                true,
                true,
                authorities);
    }