use of org.springframework.security.core.userdetails.UsernameNotFoundException in project rocket_journal by koderki.
the class MyUserDetailsService method loadUserByUsername.
public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
User user = userRepository.findByEmail(email);
if (user == null) {
throw new UsernameNotFoundException("No user found with username: " + email);
}
boolean enabled = true;
boolean accountNonExpired = true;
boolean credentialsNonExpired = true;
boolean accountNonLocked = true;
return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), enabled, accountNonExpired, credentialsNonExpired, accountNonLocked, getAuthorities(user.getRoles()));
}
use of org.springframework.security.core.userdetails.UsernameNotFoundException in project thingsboard by thingsboard.
the class RefreshTokenAuthenticationProvider method authenticateByUserId.
private SecurityUser authenticateByUserId(UserId userId) {
User user = userService.findUserById(userId);
if (user == null) {
throw new UsernameNotFoundException("User not found by refresh token");
}
UserCredentials userCredentials = userService.findUserCredentialsByUserId(user.getId());
if (userCredentials == null) {
throw new UsernameNotFoundException("User credentials not found");
}
if (!userCredentials.isEnabled()) {
throw new DisabledException("User is not active");
}
if (user.getAuthority() == null)
throw new InsufficientAuthenticationException("User has no authority assigned");
UserPrincipal userPrincipal = new UserPrincipal(UserPrincipal.Type.USER_NAME, user.getEmail());
SecurityUser securityUser = new SecurityUser(user, userCredentials.isEnabled(), userPrincipal);
return securityUser;
}
use of org.springframework.security.core.userdetails.UsernameNotFoundException in project thingsboard by thingsboard.
the class RefreshTokenAuthenticationProvider method authenticateByPublicId.
private SecurityUser authenticateByPublicId(String publicId) {
CustomerId customerId;
try {
customerId = new CustomerId(UUID.fromString(publicId));
} catch (Exception e) {
throw new BadCredentialsException("Refresh token is not valid");
}
Customer publicCustomer = customerService.findCustomerById(customerId);
if (publicCustomer == null) {
throw new UsernameNotFoundException("Public entity not found by refresh token");
}
if (!publicCustomer.isPublic()) {
throw new BadCredentialsException("Refresh token 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");
UserPrincipal userPrincipal = new UserPrincipal(UserPrincipal.Type.PUBLIC_ID, publicId);
SecurityUser securityUser = new SecurityUser(user, true, userPrincipal);
return securityUser;
}
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());
}
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);
}
Aggregations