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();
}
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;
}
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"));
}
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"));
}
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);
}
Aggregations