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