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:org.openmrs.contrib.metadatarepository.dao.hibernate.UserDaoHibernate.java

/** 
 * {@inheritDoc}/*from  w  w  w .  j  av a2 s  . co m*/
*/
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    List users = getHibernateTemplate().find("from User where username=?", username);
    if (users == null || users.isEmpty()) {
        throw new UsernameNotFoundException("user '" + username + "' not found...");
    } else {
        return (UserDetails) users.get(0);
    }
}

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

@Override
public UserDetails lookupPrincipal(BankIdAuthenticationResult auth) throws UsernameNotFoundException {
    getLog().debug(/*from   www . j  a v  a  2 s . c  o  m*/
            "The authentication result indicates that the user is a patient. Check for the user in patient repository");
    final PatientEntity patient = this.pRepo
            .findByCivicRegistrationNumber(EntityUtil.formatCrn(auth.getCivicRegistrationNumber()));
    if (patient == null) {
        getLog().debug("Could not find any patients matching {}. Trying with care givers...",
                auth.getCivicRegistrationNumber());
    } else {
        return PatientBaseViewImpl.newFromEntity(patient);
    }

    throw new UsernameNotFoundException(
            "User " + auth.getCivicRegistrationNumber() + " could not be found in the system.");
}

From source file:cn.edu.zjnu.acm.judge.service.UserDetailService.java

public UserModel getUserModel(String userId) {
    User user = userMapper.findOne(userId);
    if (user == null) {
        throw new UsernameNotFoundException(userId);
    }//from  ww  w.j  ava  2 s  . com
    int role = 0;
    for (String rightstr : userRoleMapper.findAllByUserId(user.getId())) {
        if (rightstr != null) {
            switch (rightstr.toLowerCase()) {
            case "administrator":
                role = 2;
                break;
            case "source_browser":
                role = Math.max(role, 1);
                break;
            case "news_publisher":
                // TODO
                break;
            default:
                break;
            }
        }
    }

    return new UserModel(user, ROLES.get(role));
}

From source file:com.klm.workshop.security.CustomUserDetailsService.java

/**
 * Return a spring security user, based on the given email.
 * //from   w  ww  . ja  v a2 s.  c  o  m
 * @param email The email of the user we need to return
 * @return Spring security user, based on the given email
 * @throws UsernameNotFoundException If user could not be found
 */
@Override
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
    User user = userDAO.findByEmail(email);

    if (user != null) {
        user = patchUserWithRequestData(user);
    }

    if (user != null) {
        List<GrantedAuthority> roles = new ArrayList();
        roles.add(new SimpleGrantedAuthority(user.getRole().toString()));
        return new CustomUserDetails(user, roles);
    } else {
        throw new UsernameNotFoundException("No user with email '" + email + "' found!");
    }
}

From source file:com.persistent.cloudninja.web.security.CloudNinjaUserDetailsService.java

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

    Member member = null;// www  .  j  a v  a 2  s.c  o m
    User user = null;

    String[] userNameIdArr = username.split("!");
    if (userNameIdArr.length == 2) {
        String memberId = userNameIdArr[0];
        String tenantId = userNameIdArr[1];

        try {
            member = manageUsersDao.getMemberFromDb(tenantId, memberId);
        } catch (SystemException exception) {
            LOGGER.error(exception);
        }

        if (null != member) {
            // get user authorities
            String[] roleArray = member.getRole().split(",");
            List<GrantedAuthority> userAuthorities = new ArrayList<GrantedAuthority>();
            for (int i = 0; i < roleArray.length; i++) {
                userAuthorities.add(new GrantedAuthorityImpl(ROLE_PREFIX + roleArray[i]));
            }
            // Create user
            user = new User(username, member.getPassword(), true, true, true, true, userAuthorities);
            currentUser.set(user);

        } else {
            throw new UsernameNotFoundException("User id " + memberId + " not found!");
        }
    }
    return user;
}

From source file:com.seajas.search.utilities.spring.security.service.ExtendedUserDetailsService.java

/**
 * Retrieve the UserDetails object by its username.
 * /*from  www .  j av  a2s  .  c  o  m*/
 * @param username
 * @return UserDetails
 * @throws UsernameNotFoundException
 * @throws DataAccessException
 */
@Override
public UserDetails loadUserByUsername(final String username)
        throws UsernameNotFoundException, DataAccessException {
    User user;

    if (username.equals(USERNAME_ADMIN)) {
        user = new User(-1, USERNAME_ADMIN, System.getProperty(PROPERTY_ADMIN_PASSWORD), "Administrator", true);

        if (logger != null && logger.isDebugEnabled())
            logger.debug("Administrator login details requested.");
        else if (internalLogger.isDebugEnabled())
            internalLogger.debug("Administrator login details requested.");
    } else
        user = userDAO.findUserByUsername(username);

    if (user == null)
        throw new UsernameNotFoundException("Could not find the given username.");

    return new ExtendedInternalUserDetails(user);
}

From source file:py.una.pol.karaku.services.server.KarakuWSAuthenticationProvider.java

@Override
protected UserDetails retrieveUser(String username, UsernamePasswordAuthenticationToken authentication) {

    if (userService.checkAuthenthicationByUID(username, authentication.getCredentials().toString())) {

        KarakuUser user = new KarakuUser();
        user.setUserName(username);/*from   w  w  w.  j  a  v  a2  s.com*/
        return user;
    }
    throw new UsernameNotFoundException(username);
}

From source file:com.wandrell.example.swss.service.security.DefaultUserDetailsService.java

@Override
public final UserDetails loadUserByUsername(final String username) throws UsernameNotFoundException {
    final UserDetails user; // Details for the user
    final Collection<SimpleGrantedAuthority> authorities; // Privileges of
                                                          // the user

    checkNotNull(username, "Received a null pointer as username");

    if ("myUser".equalsIgnoreCase(username)) {
        // User for password-based security
        authorities = new ArrayList<SimpleGrantedAuthority>();
        authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));

        user = new User(username, "myPassword", authorities);
    } else {/*w  ww .j a v a 2 s  .co  m*/
        // User not found
        LOGGER.debug(String.format("User for username %s not found", username));

        throw new UsernameNotFoundException(String.format("Invalid username '%s'", username));
    }

    LOGGER.debug(String.format("Found user for username %s", username));

    return user;
}

From source file:com.lixiaocong.security.DaoBasedUserDetailsService.java

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
            .getRequest();/*  w w w. ja va2 s  .c  o  m*/
    if (request.getRequestURI().equals("/signin")) {
        String postcode = request.getParameter("imagecode");
        if (!codeService.check(postcode, request.getSession()))
            throw new UsernameNotFoundException("image code error");
    }
    com.lixiaocong.entity.User user = userRepository.findByUsername(username);
    if (user == null) {
        log.info("user not exists");
        throw new UsernameNotFoundException("user not exists");
    }
    Set<GrantedAuthority> authorities = new HashSet<>();

    authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
    if (user.isAdmin())
        authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));

    return new DaoBasedUserDetails(user.getId(), user.getUsername(), user.getPassword(), authorities);
}