Search in sources :

Example 46 with UsernameNotFoundException

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

the class RestAuthenticationProvider method authenticateByPublicId.

private Authentication authenticateByPublicId(UserPrincipal userPrincipal, String publicId) {
    CustomerId customerId;
    try {
        customerId = new CustomerId(UUID.fromString(publicId));
    } catch (Exception e) {
        throw new BadCredentialsException("Authentication Failed. Public Id is not valid.");
    }
    Customer publicCustomer = customerService.findCustomerById(customerId);
    if (publicCustomer == null) {
        throw new UsernameNotFoundException("Public entity not found: " + publicId);
    }
    if (!publicCustomer.isPublic()) {
        throw new BadCredentialsException("Authentication Failed. Public Id is not valid.");
    }
    User user = new User(new UserId(EntityId.NULL_UUID));
    user.setTenantId(publicCustomer.getTenantId());
    user.setCustomerId(publicCustomer.getId());
    user.setEmail(publicId);
    user.setAuthority(Authority.CUSTOMER_USER);
    user.setFirstName("Public");
    user.setLastName("Public");
    SecurityUser securityUser = new SecurityUser(user, true, userPrincipal);
    return new UsernamePasswordAuthenticationToken(securityUser, null, securityUser.getAuthorities());
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) User(org.thingsboard.server.common.data.User) SecurityUser(org.thingsboard.server.service.security.model.SecurityUser) Customer(org.thingsboard.server.common.data.Customer) UserId(org.thingsboard.server.common.data.id.UserId) CustomerId(org.thingsboard.server.common.data.id.CustomerId) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) AuthenticationException(org.springframework.security.core.AuthenticationException)

Example 47 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project pentaho-engineering-samples by pentaho.

the class PentahoSamlUserDetailsService method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    UserDetails user;
    // ( let's think of SAML/JBDB for example ), we need to ensure the above statement still holds true
    try {
        user = getUserDetailsService().loadUserByUsername(username);
        if (user == null) {
            logger.warn("Got a null from calling the method loadUserByUsername( String username ) of UserDetailsService: " + getUserDetailsService() + ". This is an interface violation beacuse it is specified that loadUserByUsername method should never return null. Throwing a UsernameNotFoundException.");
            throw new UsernameNotFoundException(username);
        }
    // If the loadUserByUsername method throws UsernameNotFoundException, it means there is no user in the used
    // UserDetailsService.
    } catch (UsernameNotFoundException usernameNotFoundException) {
        if (isCreateDetailsOnUsernameNotFoundException()) {
            logger.warn("No user found for Username '" + username + "' in UserDetailsService '" + getSelectedAuthorizationProvider() + "'. Creating an UserDetails with Username '" + username + "' and the DefaultRole. Please verify that the user exists in the used service and confirm that your configurations are correct.", usernameNotFoundException);
            // Create the UserDetails object
            user = new User(username, "ignored", true, true, true, true, new ArrayList<GrantedAuthority>());
        } else {
            throw usernameNotFoundException;
        }
    }
    Collection<? extends GrantedAuthority> oldAuthorities = user.getAuthorities();
    if (oldAuthorities == null) {
        logger.warn("Got a null from calling the method getAuthorities() of UserDetails: " + user + ". This is an interface violation beacuse it is specified that getAuthorities() method should never return null. Considered no GrantedAuthorities for username " + username);
        oldAuthorities = new ArrayList<GrantedAuthority>();
    }
    // Ensure that any authenticated user gets the DefaultRole, usually "Authenticated"
    Collection<? extends GrantedAuthority> newAuthorities = ensureDefaultRole(oldAuthorities);
    return new User(user.getUsername(), ((user.getPassword() != null) ? user.getPassword() : "N/A"), user.isEnabled(), user.isAccountNonExpired(), user.isCredentialsNonExpired(), user.isAccountNonExpired(), newAuthorities);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetails(org.springframework.security.core.userdetails.UserDetails) User(org.springframework.security.core.userdetails.User) GrantedAuthority(org.springframework.security.core.GrantedAuthority)

Example 48 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project Gemma by PavlidisLab.

the class AuditAdvice method addCreateAuditEvent.

/**
 * Adds 'create' AuditEvent to audit trail of the passed AbstractAuditable.
 *
 * @param note Additional text to add to the automatically generated note.
 */
private void addCreateAuditEvent(final AbstractAuditable auditable, User user, final String note) {
    if (this.isNullOrTransient(auditable))
        return;
    AuditTrail auditTrail = auditable.getAuditTrail();
    this.ensureInSession(auditTrail);
    if (auditTrail != null && !auditTrail.getEvents().isEmpty()) {
        // while persisting parent objects. That's okay.
        if (AuditAdvice.log.isDebugEnabled())
            AuditAdvice.log.debug("Call to addCreateAuditEvent but the auditTrail already has events. AuditTrail id: " + auditTrail.getId());
        return;
    }
    String details = "Create " + auditable.getClass().getSimpleName() + " " + auditable.getId() + note;
    try {
        auditHelper.addCreateAuditEvent(auditable, details, user);
        if (AuditAdvice.log.isDebugEnabled()) {
            AuditAdvice.log.debug("Audited event: " + (note.length() > 0 ? note : "[no note]") + " on " + auditable.getClass().getSimpleName() + ":" + auditable.getId() + " by " + user.getUserName());
        }
    } catch (UsernameNotFoundException e) {
        AuditAdvice.log.warn("No user, cannot add 'create' event");
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) AuditTrail(ubic.gemma.model.common.auditAndSecurity.AuditTrail)

Example 49 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project Gemma by PavlidisLab.

the class UserManagerImpl method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
    User user = this.loadUser(username);
    Set<GrantedAuthority> dbAuthsSet = new HashSet<>();
    if (enableAuthorities) {
        dbAuthsSet.addAll(this.loadUserAuthorities(user.getUserName()));
    }
    if (enableGroups) {
        dbAuthsSet.addAll(this.loadGroupAuthorities(user));
    }
    if (dbAuthsSet.isEmpty()) {
        throw new UsernameNotFoundException("User " + username + " has no GrantedAuthority");
    }
    List<GrantedAuthority> dbAuths = new ArrayList<>(dbAuthsSet);
    return this.createUserDetails(username, new UserDetailsImpl(user), dbAuths);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetailsImpl(gemma.gsec.authentication.UserDetailsImpl) User(gemma.gsec.model.User) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority)

Example 50 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project Gemma by PavlidisLab.

the class AclAuthorizationTest method setup.

@Before
public void setup() {
    arrayDesign = ArrayDesign.Factory.newInstance();
    arrayDesign.setName(arrayDesignName);
    arrayDesign.setShortName(arrayDesignName);
    arrayDesign.setDescription("A test ArrayDesign from " + this.getClass().getName());
    arrayDesign.setPrimaryTaxon(this.getTaxon("mouse"));
    CompositeSequence cs1 = CompositeSequence.Factory.newInstance();
    cs1.setName(compositeSequenceName1);
    CompositeSequence cs2 = CompositeSequence.Factory.newInstance();
    cs2.setName(compositeSequenceName2);
    Collection<CompositeSequence> col = new HashSet<>();
    col.add(cs1);
    col.add(cs2);
    cs1.setArrayDesign(arrayDesign);
    cs2.setArrayDesign(arrayDesign);
    arrayDesign.setCompositeSequences(col);
    // persister helper
    arrayDesign = (ArrayDesign) persisterHelper.persist(arrayDesign);
    try {
        userManager.loadUserByUsername(aDifferentUsername);
    } catch (UsernameNotFoundException e) {
        userManager.createUser(new UserDetailsImpl("foo", aDifferentUsername, true, null, RandomStringUtils.randomAlphabetic(10) + "@gmail.com", "key", new Date()));
    }
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetailsImpl(gemma.gsec.authentication.UserDetailsImpl) CompositeSequence(ubic.gemma.model.expression.designElement.CompositeSequence) Date(java.util.Date) HashSet(java.util.HashSet) Before(org.junit.Before)

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