use of org.springframework.security.core.userdetails.UsernameNotFoundException in project zhcet-web by zhcet-amu.
the class ResetTokenSender method sendResetToken.
public void sendResetToken(String email) {
User user = userService.getUserByEmail(email).orElseThrow(() -> new UsernameNotFoundException("User with the email " + email + " not found"));
if (!user.isEmailVerified())
throw new UsernameNotFoundException("User with the email " + email + " has not verified its account!");
PasswordResetToken passwordResetToken = passwordResetTokenService.generate(user);
sendMail(passwordResetToken);
}
use of org.springframework.security.core.userdetails.UsernameNotFoundException in project pentaho-platform by pentaho.
the class UserRoleDaoUserDetailsService method loadUserByUsername.
// ~ Constructors
// ====================================================================================================
// ~ Methods
// =========================================================================================================
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
final boolean ACCOUNT_NON_EXPIRED = true;
final boolean CREDS_NON_EXPIRED = true;
final boolean ACCOUNT_NON_LOCKED = true;
IPentahoUser user;
try {
if (userRoleDao == null) {
userRoleDao = PentahoSystem.get(IUserRoleDao.class, "userRoleDaoProxy", PentahoSessionHolder.getSession());
}
user = userRoleDao.getUser(null, username);
} catch (UncategorizedUserRoleDaoException e) {
throw new UserRoleDaoUserDetailsServiceException(Messages.getInstance().getString("UserRoleDaoUserDetailsService.ERROR_0003_DATA_ACCESS_EXCEPTION"), // $NON-NLS-1$
e);
}
if (user == null) {
throw new UsernameNotFoundException(Messages.getInstance().getString(// $NON-NLS-1$
"UserRoleDaoUserDetailsService.ERROR_0001_USER_NOT_FOUND"));
}
// convert IPentahoUser to a UserDetails instance
List<IPentahoRole> userRoles = userRoleDao.getUserRoles(null, username);
int authsSize = userRoles != null ? userRoles.size() : 0;
GrantedAuthority[] auths = new GrantedAuthority[authsSize];
int i = 0;
for (IPentahoRole role : userRoles) {
auths[i++] = new SimpleGrantedAuthority(role.getName());
}
List<GrantedAuthority> dbAuths = new ArrayList<GrantedAuthority>(Arrays.asList(auths));
addCustomAuthorities(user.getUsername(), dbAuths);
// Store the Tenant ID in the session
IPentahoSession session = PentahoSessionHolder.getSession();
String tenantId = (String) session.getAttribute(IPentahoSession.TENANT_ID_KEY);
if (tenantId == null) {
ITenant tenant = JcrTenantUtils.getTenant(username, true);
session.setAttribute(IPentahoSession.TENANT_ID_KEY, tenant.getId());
}
if (!StringUtils.isEmpty(defaultRoleString)) {
defaultRole = new SimpleGrantedAuthority(defaultRoleString);
}
if (defaultRole != null && !dbAuths.contains(defaultRole)) {
dbAuths.add(defaultRole);
}
if (dbAuths.size() == 0) {
throw new UsernameNotFoundException(Messages.getInstance().getString(// $NON-NLS-1$
"UserRoleDaoUserDetailsService.ERROR_0002_NO_AUTHORITIES"));
}
return new User(user.getUsername(), user.getPassword(), user.isEnabled(), ACCOUNT_NON_EXPIRED, CREDS_NON_EXPIRED, ACCOUNT_NON_LOCKED, dbAuths);
}
use of org.springframework.security.core.userdetails.UsernameNotFoundException in project pentaho-platform by pentaho.
the class PentahoCachingUserDetailsService method loadUserByUsername.
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
boolean tenanted = JcrTenantUtils.isTenantedUser(username);
String principleName = tenanted ? JcrTenantUtils.getPrincipalName(username, true) : username;
UserDetails user = userCache.getUserFromCache(principleName);
if (user == null) {
String principleId = tenanted ? username : getPrincipleId(username);
try {
user = delegate.loadUserByUsername(principleId);
} catch (UsernameNotFoundException e) {
user = new NotFoundUserDetails(principleName, e);
}
if (user == null) {
user = new NotFoundUserDetails(principleName, new UsernameNotFoundException("UserDetailsService " + delegate + " returned null for username " + username + ". " + "This is an interface contract violation"));
}
userCache.putUserInCache(user);
}
if (user instanceof NotFoundUserDetails) {
UsernameNotFoundException e = ((NotFoundUserDetails) user).getOriginalException();
throw new UsernameNotFoundException(e.getMessage(), e);
}
return new User(user.getUsername(), (user.getPassword() == null ? "ignored" : user.getPassword()), user.isEnabled(), user.isAccountNonExpired(), user.isCredentialsNonExpired(), user.isAccountNonLocked(), user.getAuthorities());
}
use of org.springframework.security.core.userdetails.UsernameNotFoundException in project judge by zjnu-acm.
the class UserDetailsServiceImpl method loadUserByUsername.
@Nonnull
@Override
public UserDetails loadUserByUsername(String userId) throws UsernameNotFoundException {
User user = userMapper.findOne(userId);
if (user == null) {
throw new UsernameNotFoundException(userId);
}
int role = 0;
for (String rightstr : userRoleMapper.findAllByUserId(user.getId())) {
if (rightstr != null) {
switch(rightstr.toLowerCase()) {
case "administrator":
role = 2;
break;
case "source_browser":
role = Math.max(role, 1);
break;
case "news_publisher":
// TODO
break;
default:
break;
}
}
}
return org.springframework.security.core.userdetails.User.withUsername(userId).password(user.getPassword()).authorities(ROLES.get(role)).build();
}
use of org.springframework.security.core.userdetails.UsernameNotFoundException in project theskeleton by codenergic.
the class UserServiceImpl method loadUserByUsername.
@Override
public UserDetails loadUserByUsername(String username) {
UserEntity user = findUserByUsername(username);
if (user == null)
user = findUserByEmail(username);
if (user == null)
user = userRepository.findOne(username);
if (user == null)
throw new UsernameNotFoundException("Cannot find user with username or email of " + username);
Set<RolePrivilegeEntity> rolePrivileges = new HashSet<>();
user.getRoles().forEach(role -> rolePrivileges.addAll(rolePrivilegeRepository.findByRoleCode(role.getRole().getCode())));
return user.setAuthorities(rolePrivileges);
}
Aggregations