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.qcadoo.security.internal.password.PasswordReminderServiceImpl.java

private Entity getUserEntity(final String userName) {
    Entity userEntity = securityService.getUserEntity(userName);
    if (userEntity == null) {
        throw new UsernameNotFoundException("Username " + userName + " not found");
    }//from  w  w  w . ja v a2s . com
    return userEntity;
}

From source file:org.energyos.espi.common.service.impl.RetailCustomerServiceImpl.java

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    try {/* w  ww  .  j a v a 2s.  c o m*/
        return retailCustomerRepository.findByUsername(username);
    } catch (EmptyResultDataAccessException x) {
        throw new UsernameNotFoundException("Unable to find user");
    }
}

From source file:mx.edu.um.mateo.general.service.UserDetailsServiceImpl.java

@Override
public UserDetails loadUserDetails(OpenIDAuthenticationToken token) throws UsernameNotFoundException {
    log.debug("loadUserDetails: {}", token);
    String username = token.getIdentityUrl();
    String email = "";
    Usuario usuario = usuarioDao.obtienePorOpenId(username);
    log.debug("Usuario encontrado : {}", usuario);
    if (usuario == null) {
        log.debug("Buscando atributo email");
        List<OpenIDAttribute> attrs = token.getAttributes();
        for (OpenIDAttribute attr : attrs) {
            log.debug("Attr: {}", attr.getName());
            if (attr.getName().equals("email")) {
                email = attr.getValues().get(0);
            }//from   w w  w  .java 2s.c  om
        }
        log.debug("Buscando por email {}", email);
        usuario = usuarioDao.obtienePorCorreo(email);
        if (usuario == null) {
            throw new UsernameNotFoundException("No se encontro al usuario " + username);
        }
        usuario.setOpenId(username);
        usuarioDao.actualiza(usuario);
    }
    log.debug("Regresando usuario: {}", usuario);
    return (UserDetails) usuario;
}

From source file:com.jiwhiz.domain.account.impl.UserAccountServiceImpl.java

@Override
public UserAccount loadUserByUserId(String userId) throws UsernameNotFoundException {
    UserAccount account = accountRepository.findOne(userId);
    if (account == null) {
        throw new UsernameNotFoundException("Cannot find user by userId " + userId);
    }//from  w ww . ja v a 2  s .  c o  m
    return account;
}

From source file:eu.cloud4soa.frontend.commons.server.security.C4sSubjectImpl.java

private Authentication getAuthentication() {
    if (securityContext.getAuthentication() instanceof RememberMeAuthenticationToken) {

        // replace the remember-me authentication token with a C4sUserAuthentication
        String username = securityContext.getAuthentication().getName();
        logger.debug("Logging in user '{}' with remember me.", username);

        List<User> users = userRepository.findBy("username", username);
        if (users.isEmpty())
            throw new UsernameNotFoundException("User '" + username + "' not found.");

        User user = users.get(0);/*from w  ww .j ava 2 s  .  c om*/

        Collection<GrantedAuthority> authorities;
        if (SUPER_USER.equals(username)) {
            authorities = AuthorityUtils.createAuthorityList(USER_TYPE_DEVELOPER, USER_TYPE_PROVIDER,
                    UserModel.USER_ROLE_ADMINISTRATOR);
        } else if ("developer".equals(user.getUsertype().getName())) {
            authorities = AuthorityUtils.createAuthorityList(USER_TYPE_DEVELOPER);
        } else if ("paasprovider".equals(user.getUsertype().getName())) {
            authorities = AuthorityUtils.createAuthorityList(USER_TYPE_PROVIDER);
        } else
            authorities = Collections.emptyList();

        securityContext.setAuthentication(
                new C4sUserAuthentication(authorities, securityContext.getAuthentication(), user.getUriID()));

        securityContext.getAuthentication().setAuthenticated(true);

    }

    return securityContext.getAuthentication();

}

From source file:org.pentaho.custom.authentication.CustomUserDetailsService.java

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
    final boolean ACCOUNT_NON_EXPIRED = true;
    final boolean CREDS_NON_EXPIRED = true;
    final boolean ACCOUNT_NON_LOCKED = true;

    // Retrieve the user from the custom authentication system
    IUser user;/*  w  ww .  java  2 s  . c  om*/
    try {
        user = userRoleDao.getUser(getUserNameUtils().getPrincipleName(username));
    } catch (UncategorizedUserRoleDaoException e) {
        throw new UserDetailsException("Unable to get the user role dao"); //$NON-NLS-1$
    }

    // If the user is null, throw a NameNotFoundException
    if (user == null) {
        throw new UsernameNotFoundException(
                "Username [ " + getUserNameUtils().getPrincipleName(username) + "] not found"); //$NON-NLS-1$
    } else {
        // convert IUser to a UserDetails instance
        int authsSize = user.getRoles() != null ? user.getRoles().size() : 0;
        GrantedAuthority[] auths = new GrantedAuthority[authsSize];
        int i = 0;
        for (IRole role : user.getRoles()) {
            auths[i++] = new SimpleGrantedAuthority(role.getName());
        }

        List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(Arrays.asList(auths));

        if (authorities.size() == 0) {
            throw new UsernameNotFoundException(
                    "User [ " + getUserNameUtils().getPrincipleName(username) + "] does not have any role"); //$NON-NLS-1$
        }

        // Add default role to all authenticating users
        if (defaultRole != null && !authorities.contains(defaultRole)) {
            authorities.add(defaultRole);
        }

        GrantedAuthority[] arrayAuths = authorities.toArray(new GrantedAuthority[authorities.size()]);

        return new User(user.getUsername(), user.getPassword(), user.isEnabled(), ACCOUNT_NON_EXPIRED,
                CREDS_NON_EXPIRED, ACCOUNT_NON_LOCKED, Arrays.asList(arrayAuths));
    }
}

From source file:de.kaiserpfalzEdv.office.ui.web.security.KPOfficeAuthenticationProvider.java

@Override
protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication)
        throws AuthenticationException {
    KPOfficeUserDetail result;//from   w  w w . j  ava2 s. c o  m

    try {
        OfficeLoginTicket ticket = service.login(username, (String) authentication.getCredentials());

        result = new KPOfficeUserDetail(ticket);
    } catch (InvalidLoginException e) {
        throw new UsernameNotFoundException("Username '" + username + "' not found.");
    } catch (NoSuchAccountException e) {
        throw new BadCredentialsException("Wrong password for '" + username + "'.");
    }

    LOG.info("Created: {}", result);
    return result;
}

From source file:io.github.azige.bbs.service.AccountService.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String username = authentication.getName();
    String password = (String) authentication.getCredentials();

    Account account = accountRepository.findByAccountName(username);
    if (account == null) {
        throw new UsernameNotFoundException("User name not found");
    }/*from  w ww.j  a  v  a2 s. c o m*/
    password = encryptPassword(password, account.getSalt());
    if (password.equals(account.getPassword())) {
        return new UsernamePasswordAuthenticationToken(account.getProfile(), password, Account.AUTHORITYS);
    }
    return authentication;
}

From source file:com.hillert.botanic.service.DefaultUserDetailsService.java

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    for (UserDetails details : this.details) {
        if (details.getUsername().equalsIgnoreCase(username)) {
            return details;
        }/* w  w w  .java2  s  .co  m*/
    }
    throw new UsernameNotFoundException("No user found for username " + username);
}

From source file:org.callistasoftware.netcare.core.spi.impl.UserDetailsServiceImpl.java

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

    getLog().info("Lookup user {}", username);

    /*/*w ww .j  a  va 2  s.  c o m*/
     * Username will be civic registration number
     * for patients and hsa id for care givers
     */
    final PatientEntity patient = this.patientRepository.findByCivicRegistrationNumber(username);
    if (patient == null) {
        getLog().debug("Could not find any patients matching {}. Trying with care givers...", username);

        final CareActorEntity ca = this.careActorRepository.findByHsaId(username);
        if (ca == null) {
            getLog().debug("Could not find any care giver matching {}", username);
        } else {

            getLog().debug("Found care actor with {} roles", ca.getRoles().size());

            return CareActorBaseViewImpl.newFromEntity(ca);
        }
    } else {
        getLog().debug("Patient found.");
        return PatientBaseViewImpl.newFromEntity(patient);
    }

    throw new UsernameNotFoundException("Please check your credentials");

}