Search in sources :

Example 61 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project dhis2-core by dhis2.

the class DefaultClientDetailsUserDetailsService method loadUserByUsername.

public UserDetails loadUserByUsername(String username) {
    ClientDetails clientDetails;
    try {
        clientDetails = clientDetailsService.loadClientByClientId(username);
    } catch (NoSuchClientException e) {
        throw new UsernameNotFoundException(e.getMessage(), e);
    }
    String clientSecret = clientDetails.getClientSecret();
    if (clientSecret == null || clientSecret.trim().length() == 0) {
        clientSecret = emptyPassword;
    }
    return new User(username, clientSecret, clientDetails.getAuthorities());
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) ClientDetails(org.springframework.security.oauth2.provider.ClientDetails) User(org.springframework.security.core.userdetails.User) NoSuchClientException(org.springframework.security.oauth2.provider.NoSuchClientException)

Example 62 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project dhis2-core by dhis2.

the class DefaultLdapUserDetailsService method loadUserByUsername.

// -------------------------------------------------------------------------
// UserDetailsService implementation
// -------------------------------------------------------------------------
@Override
@Transactional(readOnly = true)
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
    User user = userService.getUserByUsername(username);
    if (!user.isExternalAuth() || !user.hasLdapId()) {
        throw new UsernameNotFoundException("Wrong type of user, is not LDAP user.");
    }
    boolean enabled = !user.isDisabled();
    boolean credentialsNonExpired = userService.userNonExpired(user);
    boolean accountNonLocked = !securityService.isLocked(user.getUsername());
    boolean accountNonExpired = !userService.isAccountExpired(user);
    if (ObjectUtils.anyIsFalse(enabled, credentialsNonExpired, accountNonLocked, accountNonExpired)) {
        log.info(String.format("Login attempt for disabled/locked user: '%s', enabled: %b, account non-expired: %b, user non-expired: %b, account non-locked: %b", username, enabled, accountNonExpired, credentialsNonExpired, accountNonLocked));
    }
    return new org.springframework.security.core.userdetails.User(user.getUsername(), "EXTERNAL_LDAP_" + CodeGenerator.generateCode(10), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, SecurityUtils.getGrantedAuthorities(user));
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(org.hisp.dhis.user.User) Transactional(org.springframework.transaction.annotation.Transactional)

Example 63 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project midpoint by Evolveum.

the class MidpointPrincipalContextMapper method mapUserFromContext.

@Override
public UserDetails mapUserFromContext(DirContextOperations ctx, String username, Collection<? extends GrantedAuthority> authorities) {
    String userNameEffective = username;
    Class<? extends FocusType> focusType = UserType.class;
    try {
        if (ctx instanceof LdapDirContextAdapter && ((LdapDirContextAdapter) ctx).getNamingAttr() != null) {
            userNameEffective = resolveLdapName(ctx, username, ((LdapDirContextAdapter) ctx).getNamingAttr());
            focusType = ((LdapDirContextAdapter) ctx).getFocusType();
        }
        return principalManager.getPrincipal(userNameEffective, focusType);
    } catch (ObjectNotFoundException e) {
        throw new UsernameNotFoundException("UserProfileServiceImpl.unknownUser", e);
    } catch (SchemaException | CommunicationException | ConfigurationException | SecurityViolationException | ExpressionEvaluationException | NamingException e) {
        throw new SystemException(e.getMessage(), e);
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) NamingException(javax.naming.NamingException) UserType(com.evolveum.midpoint.xml.ns._public.common.common_3.UserType)

Example 64 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project incubator-atlas by apache.

the class UserDaoTest method testUserDaowithInValidLogin.

@Test
public void testUserDaowithInValidLogin() {
    boolean hadException = false;
    Properties userLogins = new Properties();
    userLogins.put("admin", "ADMIN::admin123");
    userLogins.put("test", "DATA_STEWARD::test123");
    UserDao user = new UserDao();
    user.setUserLogins(userLogins);
    try {
        User userBean = user.loadUserByUsername("xyz");
    } catch (UsernameNotFoundException uex) {
        hadException = true;
    }
    assertTrue(hadException);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(org.apache.atlas.web.model.User) UserDao(org.apache.atlas.web.dao.UserDao) Properties(java.util.Properties) Test(org.testng.annotations.Test)

Example 65 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project incubator-atlas by apache.

the class FileAuthenticationTest method testInValidUsernameLogin.

@Test
public void testInValidUsernameLogin() {
    when(authentication.getName()).thenReturn("wrongUserName");
    when(authentication.getCredentials()).thenReturn("wrongpassword");
    try {
        Authentication auth = authProvider.authenticate(authentication);
        LOG.debug(" {}", auth);
    } catch (UsernameNotFoundException uExp) {
        assertTrue(uExp.getMessage().contains("Username not found."));
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) Authentication(org.springframework.security.core.Authentication) Test(org.testng.annotations.Test)

Aggregations

UsernameNotFoundException (org.springframework.security.core.userdetails.UsernameNotFoundException)132 GrantedAuthority (org.springframework.security.core.GrantedAuthority)40 SimpleGrantedAuthority (org.springframework.security.core.authority.SimpleGrantedAuthority)39 UserDetails (org.springframework.security.core.userdetails.UserDetails)36 Authentication (org.springframework.security.core.Authentication)24 Transactional (org.springframework.transaction.annotation.Transactional)20 Logger (org.slf4j.Logger)18 LoggerFactory (org.slf4j.LoggerFactory)18 java.util (java.util)16 BadCredentialsException (org.springframework.security.authentication.BadCredentialsException)15 Collectors (java.util.stream.Collectors)14 UserDetailsService (org.springframework.security.core.userdetails.UserDetailsService)14 Component (org.springframework.stereotype.Component)14 User (org.springframework.security.core.userdetails.User)13 ArrayList (java.util.ArrayList)12 HashSet (java.util.HashSet)11 UserRepository (io.github.jhipster.sample.repository.UserRepository)9 UsernamePasswordAuthenticationToken (org.springframework.security.authentication.UsernamePasswordAuthenticationToken)9 User (io.github.jhipster.sample.domain.User)6 Date (java.util.Date)6