Example usage for org.springframework.security.core.userdetails UsernameNotFoundException UsernameNotFoundException

List of usage examples for org.springframework.security.core.userdetails UsernameNotFoundException UsernameNotFoundException

Introduction

In this page you can find the example usage for org.springframework.security.core.userdetails UsernameNotFoundException UsernameNotFoundException.

Prototype

public UsernameNotFoundException(String msg) 

Source Link

Document

Constructs a UsernameNotFoundException with the specified message.

Usage

From source file:com.create.security.RepositoryUserDetailsService.java

private User getUser(String username) {
    return Optional.of(username).map(userRepository::findByUsername).filter(Objects::nonNull)
            .orElseThrow(() -> new UsernameNotFoundException("User " + username + " does not exists"));
}

From source file:org.jblogcms.core.security.service.UserDetailsServiceImpl.java

/**
   * @see UserDetailsService#loadUserByUsername(String)
   *///from  w  w w .java  2  s  .co  m
@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

    Account account = accountRepository.findAccountByEmail(username);

    if (account == null) {
        throw new UsernameNotFoundException("No user found with username: " + username);
    }

    Set<GrantedAuthority> authorities = new HashSet<GrantedAuthority>();
    SimpleGrantedAuthority authority = new SimpleGrantedAuthority(account.getAccountRole().toString());
    authorities.add(authority);

    AccountDetails userDetails = new AccountDetails(account.getEmail(), account.getPassword(), authorities);
    userDetails.setId(account.getId());
    userDetails.setFirstName(account.getFirstName());
    userDetails.setLastName(account.getLastName());
    userDetails.setSocialSignInProvider(account.getSignInProvider());
    return userDetails;
}

From source file:com.todo.backend.security.UserDetailsServiceImpl.java

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

    log.trace(".loadUserByUsername({})", username);

    final String lowercaseUsername = username.toLowerCase();
    final Optional<User> optionalUser = userRepository.findByUsername(lowercaseUsername);

    if (!optionalUser.isPresent()) {
        throw new UsernameNotFoundException("User " + username + " not found!");
    }/*ww w  .j  a v a2 s  .co  m*/

    final User user = optionalUser.get();
    final List<GrantedAuthority> grantedAuthorities = Collections
            .singletonList(new SimpleGrantedAuthority(user.getRole().name()));
    return new org.springframework.security.core.userdetails.User(lowercaseUsername, null, grantedAuthorities);
}

From source file:com.trenako.web.security.AccountDetailsService.java

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    Account user = repo.findByEmailAddress(username);
    if (user == null) {
        throw new UsernameNotFoundException("Could not find user with username '" + username + "'");
    }/* ww w.  j a v  a2  s  .c  om*/
    return new AccountDetails(user);
}

From source file:org.awesomeagile.webapp.security.AwesomeAgileSocialUserDetailsService.java

@Override
public SocialUserDetails loadUserByUserId(String userId) throws UsernameNotFoundException, DataAccessException {
    User user = userRepository.findOneByPrimaryEmail(userId);
    if (user == null) {
        throw new UsernameNotFoundException(String.format("User with ID %s was not found", userId));
    }/*from w  ww .j ava  2 s  .  c o  m*/
    return new AwesomeAgileSocialUser(user, ImmutableSet.<GrantedAuthority>of());
}

From source file:cz.PA165.vozovyPark.service.impl.UserLoginServiceImpl.java

@Override
@Transactional(readOnly = true)/*from  ww  w.  j av  a  2  s . co  m*/
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    List<cz.PA165.vozovyPark.entities.User> myUserList = userDao.getByUsername(username);
    if (myUserList.size() == 1) {
        cz.PA165.vozovyPark.entities.User myUser = myUserList.get(0);
        List<SimpleGrantedAuthority> auths = new java.util.ArrayList<SimpleGrantedAuthority>();
        if (myUser.getIsAdmin()) {
            auths.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
        } else {
            auths.add(new SimpleGrantedAuthority("ROLE_USER"));
        }
        User userDetails = new User(username, myUser.getPassword(), myUser.getEnabled(), true, true, true,
                auths);
        return userDetails;
    } else {
        throw new UsernameNotFoundException("User with username" + username + " not found");
    }
}

From source file:ch.astina.hesperid.web.services.users.CarpetUserDetailsService.java

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
    User user = userDAO.getUserByName(username);
    if (user == null) {
        throw new UsernameNotFoundException(username);
    }/* ww w . j av a 2s . com*/
    logger.info("Loaded user: " + user.getUsername());
    return user;
}

From source file:com.callcenter.service.UserService.java

@Override
public UserDetails loadUserByUsername(final String userName)
        throws UsernameNotFoundException, DataAccessException {
    final com.callcenter.domain.User user = com.callcenter.domain.User.findUserByName(userName);
    if (user == null)
        throw new UsernameNotFoundException("User with the name: " + userName + " was not found");

    final ArrayList<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();

    for (final String authorityName : user.getAuthorities()) {
        grantedAuthorities.add(new GrantedAuthorityImpl(authorityName));
    }/*from   w  ww . j  ava 2 s .  co m*/
    return new User(user.getName(), user.getPassword(), true, true, true, true, grantedAuthorities);
}

From source file:org.verinice.rest.security.SpringUserDetailsService.java

@Override
public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException {
    Account account = accountService.findByLogin(login);

    if (account == null) {
        throw new UsernameNotFoundException(login);
    }//from  w  w w .ja va2  s . c  o m

    return account;
}

From source file:org.homiefund.api.service.impl.UserServiceImpl.java

@Override
@Transactional(readOnly = true)//from   ww  w .j  ava 2  s .  com
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
    Optional<User> user = userDAO.getByUsername(s);
    if (!user.isPresent()) {
        throw new UsernameNotFoundException(s);
    } else {
        UserDTO result = mapper.map(user.get(), UserDTO.class);
        result.setHomes(homeDAO.getHomes(user.get()).stream().map(h -> {
            HomeDTO hdto = new HomeDTO();
            hdto.setId(h.getId());
            return hdto;
        }).collect(Collectors.toList()));

        return result;
    }
}