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.musicrecital.dao.hibernate.UserDaoHibernate.java

/**
 * {@inheritDoc}/*from www. jav a  2  s  . com*/
*/
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    List users = getSession().createCriteria(User.class).add(Restrictions.eq("username", username)).list();
    if (users == null || users.isEmpty()) {
        throw new UsernameNotFoundException("user '" + username + "' not found...");
    } else {
        return (UserDetails) users.get(0);
    }
}

From source file:org.apigw.authserver.svc.impl.CertifiedClientDetailsServiceImpl.java

@Override
@Transactional(propagation = Propagation.SUPPORTS)
public CertifiedClient loadClientByX509Cert(String issuerDN, String subjectDN) {
    log.trace("loadClientByX509Cert(issuerDN:{}, subjectDN:{})", issuerDN, subjectDN);
    CertifiedClient certifiedClient = certifiedClientRepository.findClientByIssuerDnAndSubjectDnEager(issuerDN,
            subjectDN);/*from   ww w . j av a 2s . c o  m*/
    if (certifiedClient == null) {
        log.warn("No client with issuer DN {} and subject DN {} found", issuerDN, subjectDN);
        throw new UsernameNotFoundException("No client for the provided certificate found");
    }
    log.trace("returning CertifiedClient from loadClientByX509Cert");
    return certifiedClient;
}

From source file:miage.ecom.web.security.AdminAuthenticationProvider.java

@Override
protected UserDetails retrieveUser(String login, UsernamePasswordAuthenticationToken upat)
        throws AuthenticationException {

    Admin admin = null;// ww w  .ja va2 s  . c  om
    UserDetails user = null;
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    authorities.add(new GrantedAuthorityImpl("ROLE_ADMIN"));

    if (login != null) {
        admin = adminFacade.findByLogin(login);
    }

    if (admin == null) {
        throw new UsernameNotFoundException("Nom d'utilisateur ou mot de passe incorrect");
    }

    user = new User(admin.getLogin(), admin.getPassword(), true, true, true, true, authorities);

    return user;
}

From source file:miage.ecom.web.security.UserAuthenticationProvider.java

@Override
protected UserDetails retrieveUser(String login, UsernamePasswordAuthenticationToken upat)
        throws AuthenticationException {

    Customer customer = customerFacade.findByLogin(login);
    UserDetails user;//  w w w. j a  v  a  2 s.co m
    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    authorities.add(new GrantedAuthorityImpl("ROLE_USER"));

    if (customer == null) {
        throw new UsernameNotFoundException("Nom d'utilisateur ou mot de passe incorrect");
    }

    user = new User(customer.getLogin(), customer.getPassword(), true, true, true, true, authorities);

    return user;
}

From source file:org.socialsignin.springsocial.security.userdetails.SpringSocialSecurityUserDetailsService.java

/**
 * Uses a <code>SignUpService</code> implementation to check if a local user account for this username is available 
 * and if so, bases the user's authentication on the set of connections the user currently has to
 * 3rd party providers.  Allows provider-specific roles to be set for each user - uses a <code>UsersConnectionRepository</code>
 * to obtain list of connections the user has and a <code>SpringSocialSecurityAuthenticationFactory</code>
 * to obtain an authentication based on those connections.
 * /*from  w w  w. j a  v  a  2 s .c  o m*/
 */
@Override
public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
    ConnectionRepository connectionRepository = usersConnectionRepository.createConnectionRepository(userName);
    SpringSocialProfile springSocialProfile = signUpService.getUserProfile(userName);
    List<Connection<?>> allConnections = getConnections(connectionRepository, userName);
    if (allConnections.size() > 0) {

        Authentication authentication = authenticationFactory.createAuthenticationForAllConnections(userName,
                springSocialProfile.getPassword(), allConnections);
        return new User(userName, authentication.getCredentials().toString(), true, true, true, true,
                authentication.getAuthorities());

    } else {
        throw new UsernameNotFoundException(userName);
    }

}

From source file:com.github.zxkane.config.SecurityTestConfiguration.java

@Bean
public UserDetailsService userDetailsService() {
    return new UserDetailsService() {
        @Override//from www.  jav a  2s . co m
        public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
            switch (username) {
            case "staffUsername":
                return staffUser();
            }
            throw new UsernameNotFoundException(username + " was not found.");
        }
    };
}

From source file:org.cloudfoundry.identity.uaa.authentication.manager.AuthzAuthenticationManagerTests.java

@Test
public void missingUserPublishesNotFoundEvent() {
    when(db.retrieveUserByName(eq("aguess"))).thenThrow(new UsernameNotFoundException("mocked"));
    try {/*from  www. ja  va2s .  co m*/
        mgr.authenticate(createAuthRequest("aguess", "password"));
        fail();
    } catch (BadCredentialsException expected) {
    }

    verify(publisher).publishEvent(isA(UserNotFoundEvent.class));
}

From source file:com.healthcit.cacure.businessdelegates.UserManager.java

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
    userService.setAuthType(Constants.DB_AUTH_VALUE);
    UserCredentials user = findByName(username);
    if (user == null) {
        throw new UsernameNotFoundException("Username not found");
    }//from  ww w . j  av a 2  s . co  m

    //local inner class
    class GrantedAuthorityImpl implements GrantedAuthority {
        private static final long serialVersionUID = -4708051153956036063L;
        private final String role;

        public GrantedAuthorityImpl(String role) {
            this.role = role;
        }

        @Override
        public String getAuthority() {
            return role;
        }

        @SuppressWarnings("unused")
        public int compareTo(Object o) {
            if (o instanceof GrantedAuthority) {
                return role.compareTo(((GrantedAuthority) o).getAuthority());
            }
            return -1;
        }
    }

    //getting user Roles
    List<Role> roles = userManagerDao.getUserRoles(user);
    List<GrantedAuthority> grantedAuthorityList = new ArrayList<GrantedAuthority>();
    if (roles != null && !roles.isEmpty()) {
        for (Role role : roles) {
            grantedAuthorityList.add(new GrantedAuthorityImpl(role.getName()));
        }
    }
    UserDetails res = new User(user.getUserName(), user.getPassword(), true, true, true, true,
            grantedAuthorityList);

    return res;
}

From source file:org.openwms.core.uaa.SecurityContextUserServiceImpl.java

/**
 * {@inheritDoc}/*from   w w w  . j  av a 2 s .  c om*/
 * 
 * @param username
 *            User's username to search for
 * @return A wrapper object
 * @throws UsernameNotFoundException
 *             in case the User was not found or the password was not valid
 */
@Transactional(readOnly = true)
@Override
public UserDetails loadUserByUsername(String username) {
    UserDetails ud = userCache.getUserFromCache(username);
    if (null == ud) {
        if (systemUsername.equals(username)) {
            User user = userService.createSystemUser();
            ud = new SystemUserWrapper(user);
            ((SystemUserWrapper) ud).setPassword(enc.encode(user.getPassword()));
        } else {
            try {
                ud = new UserWrapper(userService.findByUsername(username).get());
            } catch (Exception ex) {
                LOGGER.error(ex.getMessage(), ex);
                throw new UsernameNotFoundException(String.format("User with username %s not found", username));
            }
        }
        userCache.putUserInCache(ud);
    }
    return ud;
}

From source file:net.kamhon.ieagle.function.user.service.impl.UserDetailsServiceImpl.java

public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
    User user = userDetailsDao.getByUsername(username);

    if (user == null) {
        throw new UsernameNotFoundException("Username " + username + " not found");
    }//from   w  w  w.ja  v  a  2  s.c o  m

    List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>();
    authorities.add(new ExtGrantedAuthority("ROLE_USER"));

    List<UserDetailsAccessEx> accessExs = userDetailsAccessExDao.findAccessEx(user.getUserId());
    for (UserDetailsAccessEx accessEx : accessExs) {
        authorities.add(new ExtGrantedAuthority(accessEx.getId().getAccessExCode()));
    }

    user.setAuthorities(authorities.toArray(new ExtGrantedAuthority[] {}));

    return user;
}