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:ch.wisv.areafiftylan.security.authentication.AuthenticationServiceImpl.java

@Override
public String createNewAuthToken(String username, String password) {
    User user = userService.getUserByUsername(username)
            .orElseThrow(() -> new UsernameNotFoundException(username));

    if (correctCredentials(user, password)) {
        // Delete the old Token
        authenticationTokenRepository.findByUserUsername(username)
                .ifPresent(t -> authenticationTokenRepository.delete(t));

        return authenticationTokenRepository.save(new AuthenticationToken(user)).getToken();
    }//w w w.j  a va  2 s  .c  o m

    throw new AuthenticationCredentialsNotFoundException("Incorrect credentials");
}

From source file:com.wiiyaya.provider.main.service.impl.UserDetailsServiceImpl.java

@Override
@Transactional(readOnly = true)/*  ww w . ja  va2 s .  co m*/
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    BaseUser user = baseUserDao.findByUsername(username);
    if (user == null) {
        throw new UsernameNotFoundException(username + " dose not exists!");
    }
    List<AuthorityDto> privileges = new ArrayList<>();
    List<Long> privilegesId = new ArrayList<>();
    for (Role role : user.getRoles()) {
        for (Privilege privilege : role.getPrivileges()) {
            AuthorityDto auth = new AuthorityDto();
            auth.setPrivilege(privilege.getCode());
            privileges.add(auth);
            privilegesId.add(privilege.getId());
        }
    }
    CurrUserDto currUserDto = new CurrUserDto();
    currUserDto.setId(user.getId());
    currUserDto.setUsername(user.getUsername());
    currUserDto.setPassword(user.getPassword());
    currUserDto.setAuthorities(privileges);
    currUserDto.setAuthoritieIds(privilegesId);
    currUserDto.setEnabled(user.isEnabled());
    currUserDto.setAccountNonExpired(user.isAccountNonExpired());
    currUserDto.setAccountNonLocked(user.isAccountNonLocked());
    currUserDto.setCredentialsNonExpired(user.isCredentialsNonExpired());
    return currUserDto;
}

From source file:com.capital.dragon.Security.UserRepositoryUserDetailsService.java

@Override
public UserDetails loadUserByUsername(String login) throws UsernameNotFoundException {
    User user = userRepo.findByLogin(login);
    if (user == null) {
        throw new UsernameNotFoundException("Could not find user " + login);
    }// w  w  w .j  av a  2  s . co m

    return new CustomUserDetails(user);
}

From source file:com.excilys.ebi.bank.web.security.CustomUserDetailsService.java

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

    User user = userService.findByLoginFetchRoles(username);

    if (user == null) {
        throw new UsernameNotFoundException(username + " : cet utilisateur n'existe pas.");
    }/*from  www.j a  v  a2  s.  com*/

    return createUserDetails(user);
}

From source file:cz.muni.fi.pa165.legomanager.services.impl.UserDetailsServiceImpl.java

@Override
public UserDetails loadUserByUsername(String string) throws UsernameNotFoundException {
    Object o = userDao.findUserByAccountName(string);
    if (o == null) {
        throw new UsernameNotFoundException("User not found {username= " + string + "}");
    }//from  w  w w .  ja v a2  s. co  m

    final UserTO userTO = mapper.map(o, UserTO.class);
    UserDetails details = new UserDetails() {

        @Override
        public Collection<? extends GrantedAuthority> getAuthorities() {
            List<SimpleGrantedAuthority> auths = new ArrayList<>();
            auths.add(new SimpleGrantedAuthority(userTO.getRole()));
            return auths;
        }

        @Override
        public String getPassword() {
            return userTO.getPassword();
        }

        @Override
        public String getUsername() {
            return userTO.getAccountName();
        }

        @Override
        public boolean isAccountNonExpired() {
            return true;
        }

        @Override
        public boolean isAccountNonLocked() {
            return true;
        }

        @Override
        public boolean isCredentialsNonExpired() {
            return true;
        }

        @Override
        public boolean isEnabled() {
            return true;
        }
    };
    return details;
}

From source file:org.test.skeleton.core.security.UserRepositoryUserDetailsService.java

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    User user = userDao.findByEmail(username);
    if (user == null) {
        throw new UsernameNotFoundException("Could not find user " + username);
    }//from www. ja va 2 s.  co m
    return new CustomUserDetails(user);
}

From source file:sample.security.UserRepositoryUserDetailsService.java

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    User user = this.userRepository.findByEmail(username);
    if (user == null) {
        throw new UsernameNotFoundException("Could not find user " + username);
    }/*w w  w .j  a  va2 s.c  o m*/
    return new CustomUserDetails(user);
}

From source file:fr.wirth.web.service.SecurityService.java

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

    Utilisateur utilisateur = dao.loadByUsername(username);

    if (utilisateur == null) {
        throw new UsernameNotFoundException("No such user: " + username);
    } else if (utilisateur.getRoles().isEmpty()) {
        throw new UsernameNotFoundException("User " + username + " has no authorities");
    }/* ww w.ja v a  2  s . c  om*/

    return new User(utilisateur.getUsername(), utilisateur.getPassword(), utilisateur.isIsEnabled(),
            utilisateur.isAccountNonExpired(), utilisateur.isCredentialsNonExpired(),
            utilisateur.isAccountNonLocked(), toGrantedAutorithy(utilisateur.getRoles()));

}

From source file:de.thm.arsnova.security.DbUserDetailsService.java

@Override
public UserDetails loadUserByUsername(String username) {
    LOGGER.debug("Load user: " + username);
    DbUser dbUser = dao.getUser(username);
    if (null == dbUser) {
        throw new UsernameNotFoundException("User does not exist.");
    }/* w  w w  .  j  a  va 2  s. c  o  m*/

    final List<GrantedAuthority> grantedAuthorities = new ArrayList<GrantedAuthority>();
    grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_USER"));
    grantedAuthorities.add(new SimpleGrantedAuthority("ROLE_DB_USER"));

    return new User(username, dbUser.getPassword(), null == dbUser.getActivationKey(), true, true, true,
            grantedAuthorities);
}

From source file:com.elearning.rest1.config.security.web.UserRepositoryUserDetailsService.java

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

    User user = userRepository.findByEmail(email).orElseThrow(() -> new UsernameNotFoundException(email));

    return new CustomUserDetails(user);
}