Search in sources :

Example 21 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project FP-PSP-SERVER by FundacionParaguaya.

the class UserDetailsServiceImpl method loadUserByUsername.

@Override
public UserDetailsDTO loadUserByUsername(String username) throws UsernameNotFoundException {
    LOG.info("Loading user details: {}", username);
    UserEntity user = userRepository.findOneByUsername(username).orElseThrow(() -> new UsernameNotFoundException("Username not found " + username));
    List<UserRoleEntity> roles = userRoleRepo.findByUser(user);
    UserApplicationEntity userApp = userApplicationRepo.findByUser(user).orElseGet(UserApplicationEntity::new);
    Optional<OrganizationEntity> organization = userApp.getOrganizationOpt();
    Optional<ApplicationEntity> application = userApp.getApplicationOpt();
    return UserDetailsDTO.builder().username(user.getUsername()).password(user.getPass()).enabled(user.isActive()).application(application.map(applicationMapper::entityToDto).orElse(null)).organization(organization.map(organizationMapper::entityToDto).orElse(null)).grantedAuthorities(this.getGrantedAuthorities(roles)).build();
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserRoleEntity(py.org.fundacionparaguaya.pspserver.security.entities.UserRoleEntity) OrganizationEntity(py.org.fundacionparaguaya.pspserver.network.entities.OrganizationEntity) ApplicationEntity(py.org.fundacionparaguaya.pspserver.network.entities.ApplicationEntity) UserApplicationEntity(py.org.fundacionparaguaya.pspserver.network.entities.UserApplicationEntity) UserApplicationEntity(py.org.fundacionparaguaya.pspserver.network.entities.UserApplicationEntity) UserEntity(py.org.fundacionparaguaya.pspserver.security.entities.UserEntity)

Example 22 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project lumberjack by fn-ctional.

the class DatabaseAdminUsers method loadAdminUser.

public AdminUser loadAdminUser(String email) throws UsernameNotFoundException, SQLException {
    PreparedStatement stmt = databaseConnection.getConnection().prepareStatement("SELECT * FROM Admins WHERE Email = ?");
    stmt.setString(1, email);
    ResultSet rs = stmt.executeQuery();
    AdminUser adminUser = loadAdminUserFromResultSet(rs);
    if (adminUser == null)
        throw new UsernameNotFoundException("Username not found: " + email);
    return adminUser;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) AdminUser(uk.ac.bris.cs.rfideasalreadytaken.lumberjack.authentication.data.AdminUser)

Example 23 with UsernameNotFoundException

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

the class DomainUserDetailsService method loadUserByUsername.

@Override
@Transactional
public UserDetails loadUserByUsername(final String login) {
    log.debug("Authenticating {}", login);
    String lowercaseLogin = login.toLowerCase(Locale.ENGLISH);
    Optional<User> userFromDatabase = userRepository.findOneWithAuthoritiesByLogin(lowercaseLogin);
    return userFromDatabase.map(user -> {
        if (!user.getActivated()) {
            throw new UserNotActivatedException("User " + lowercaseLogin + " was not activated");
        }
        List<GrantedAuthority> grantedAuthorities = user.getAuthorities().stream().map(authority -> new SimpleGrantedAuthority(authority.getName())).collect(Collectors.toList());
        return new org.springframework.security.core.userdetails.User(lowercaseLogin, user.getPassword(), grantedAuthorities);
    }).orElseThrow(() -> new UsernameNotFoundException("User " + lowercaseLogin + " was not found in the " + "database"));
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(com.baeldung.domain.User) LoggerFactory(org.slf4j.LoggerFactory) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) Collectors(java.util.stream.Collectors) GrantedAuthority(org.springframework.security.core.GrantedAuthority) Component(org.springframework.stereotype.Component) UserRepository(com.baeldung.repository.UserRepository) UserDetails(org.springframework.security.core.userdetails.UserDetails) Transactional(org.springframework.transaction.annotation.Transactional) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(com.baeldung.domain.User) Transactional(org.springframework.transaction.annotation.Transactional)

Example 24 with UsernameNotFoundException

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

the class CustomDaoAuthenticationProvider method authenticate.

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    String name = authentication.getName();
    String password = authentication.getCredentials().toString();
    UserDetails u = null;
    try {
        u = getUserDetailsService().loadUserByUsername(name);
    } catch (UsernameNotFoundException ex) {
        log.error("User '" + name + "' not found");
    } catch (Exception e) {
        log.error("Exception in CustomDaoAuthenticationProvider: " + e);
    }
    if (u != null) {
        if (u.getPassword().equals(password)) {
            return new UsernamePasswordAuthenticationToken(u, password, u.getAuthorities());
        }
    }
    throw new BadCredentialsException(messages.getMessage("CustomDaoAuthenticationProvider.badCredentials", "Bad credentials"));
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetails(org.springframework.security.core.userdetails.UserDetails) UsernamePasswordAuthenticationToken(org.springframework.security.authentication.UsernamePasswordAuthenticationToken) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) BadCredentialsException(org.springframework.security.authentication.BadCredentialsException) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) AuthenticationException(org.springframework.security.core.AuthenticationException)

Example 25 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project tutorials-java by Artister.

the class UserDetailsServiceImpl method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    UserEntity userEntity = userService.getByUsername(username);
    if (userEntity == null) {
        throw new UsernameNotFoundException("用户不存在!");
    }
    List<SimpleGrantedAuthority> simpleGrantedAuthorities = createAuthorities(userEntity.getRoles());
    return new User(userEntity.getUsername(), userEntity.getPassword(), simpleGrantedAuthorities);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(org.springframework.security.core.userdetails.User) UserEntity(org.ko.security.entity.UserEntity)

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