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.ar.dev.tierra.api.config.security.CustomUserDetailsService.java

@Override
public UserDetails loadUserByUsername(final String login) {

    Calendar cal = Calendar.getInstance();
    Date date = cal.getTime();/*from  w  ww .j  a  va2 s  . c  o m*/
    String lowercaseLogin = login.toLowerCase();

    Usuarios userFromDatabase;

    userFromDatabase = userRepository.findUsuarioByUsername(lowercaseLogin);

    if (userFromDatabase == null) {
        throw new UsernameNotFoundException("User " + lowercaseLogin + " was not found in the database");
    } else if (!userFromDatabase.isEstado() == true) {
        throw new UserNotActivatedException("User " + lowercaseLogin + " is not activated");
    }
    userFromDatabase.setUltimaConexion(date);
    userRepository.updateUsuario(userFromDatabase);
    Collection<GrantedAuthority> grantedAuthorities = new ArrayList<>();
    String authority;
    switch (userFromDatabase.getRoles().getIdRol()) {
    case 1:
        authority = "ROLE_ADMIN";
        break;
    case 2:
        authority = "ROLE_VENDEDOR";
        break;
    case 3:
        authority = "ROLE_CAJERO";
        break;
    case 4:
        authority = "ROLE_CONTADOR";
        break;
    case 5:
        authority = "ROLE_REPOSITOR";
        break;
    case 6:
        authority = "ROLE_ENCARGADO/VENDEDOR";
        break;
    default:
        authority = "ROLE_NONE";
        break;
    }
    GrantedAuthority grantedAuthority = new SimpleGrantedAuthority(authority);
    grantedAuthorities.add(grantedAuthority);
    return new User(String.valueOf(userFromDatabase.getUsername()), userFromDatabase.getPassword(),
            grantedAuthorities);
}

From source file:org.createnet.raptor.auth.service.RaptorUserDetailsService.java

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

    User user = userRepository.findByEmail(username);

    if (user == null) {
        user = userRepository.findByUsername(username);
    }//from ww  w . jav  a 2 s .  c o  m

    if (user == null) {
        throw new UsernameNotFoundException("User not found!");
    }

    return new RaptorUserDetails(user);
}

From source file:com.jeanchampemont.notedown.security.NoteDownUserDetailsService.java

@Transactional(readOnly = true)
@Override//from  w w  w.ja  v a2s  .com
public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
    User user = userRepository.findByEmailIgnoreCase(s);
    if (user == null) {
        throw new UsernameNotFoundException("not found");
    }
    return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), true,
            true, true, true, Collections.emptyList());
}

From source file:com.sinespera.services.CustomUserDetailsService.java

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    Usuario user = usuarioRepository.findByUsuario(username);
    if (user == null) {
        throw new UsernameNotFoundException(String.format("User %s does not exist!", username));
    }//from  w ww . j a  v a 2  s.co m
    return new UserRepositoryUserDetails(user);
}

From source file:com.epam.reportportal.auth.DatabaseUserDetailsService.java

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    User userEntity = userRepository.findOne(username.toLowerCase());
    if (null == userEntity) {
        throw new UsernameNotFoundException("Username '" + username + "' not found");
    }/*from  w w  w  .  j av a  2s . c o m*/

    String login = userEntity.getLogin();
    String password = userEntity.getPassword() == null ? "" : userEntity.getPassword();

    return new org.springframework.security.core.userdetails.User(login, password, true, true, true, true,
            AuthUtils.AS_AUTHORITIES.apply(userEntity.getRole()));
}

From source file:sequrity.LoginService.java

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

    Korisnik user = null;//w  w  w .j a  va2  s  . c  o  m
    try {
        //username je email u ovom slucaju
        user = loadUserByEmail(username);

    } catch (Exception ex) {
        Logger.getLogger(LoginService.class.getName()).log(Level.SEVERE, null, ex);
    }

    if (user == null)
        throw new UsernameNotFoundException("Ne postoji registrovani korisnik sa email adresom " + username);

    List<SimpleGrantedAuthority> authorities;
    if (user instanceof Student)
        authorities = Arrays.asList(new SimpleGrantedAuthority(ROLE_STUDENT));
    else
        authorities = Arrays.asList(new SimpleGrantedAuthority(ROLE_PROFESOR));

    return new User(username, user.getPassword(), authorities);
}

From source file:com.gsr.myschool.server.security.StatelessAdminDetailService.java

@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
    AdminUser user = adminUserRepos.findByEmail(email);
    if (user == null || user.getStatus() == UserStatus.INACTIVE) {
        throw new UsernameNotFoundException("Bad credentials");
    } else {//from ww w .  j a va2s  .  co m
        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
        authorities.add(new SimpleGrantedAuthority(user.getAuthority().name()));
        return new User(user.getEmail(), user.getPassword(), user.getStatus() == UserStatus.ACTIVE, true, true,
                true, authorities);
    }
}

From source file:scratch.cucumber.example.security.spring.RepositoryUserDetailsService.java

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

    final User user = userRepository.findByUsername(username);

    if (user != null) {
        return new scratch.cucumber.example.security.spring.UserDetails(user);
    }/*from w w w  .java  2s  .  c  om*/

    throw new UsernameNotFoundException("No user found for username: " + username);
}

From source file:ca.qhrtech.security.BGLAuthenticationConfiguration.java

private UserDetailsService userDetailsService() {
    return (String username) -> {
        BGLUser user = userRepository.findByUsername(username);
        if (user != null) {
            return new User(user.getUsername(), user.getPassword(), true, true, true, true,
                    AuthorityUtils.createAuthorityList("USER"));
        } else {//  ww w  . j  ava 2s  .c o  m
            throw new UsernameNotFoundException("Could not find the user: " + username);
        }
    };
}

From source file:io.spring.springoverflow.service.SpringOverflowUserDetailsService.java

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    User user = userRepository.findByDisplayName(username);

    if (user != null) {
        return new org.springframework.security.core.userdetails.User(username, "password",
                Collections.singletonList(new SimpleGrantedAuthority("ROLE_AUTHENTICATED")));
    } else {/*from   w  ww . j  a v  a2s . co  m*/
        throw new UsernameNotFoundException("Unable to find " + username);
    }
}