Search in sources :

Example 71 with UsernameNotFoundException

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);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(amu.zhcet.data.user.User)

Example 72 with UsernameNotFoundException

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);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(org.springframework.security.core.userdetails.User) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser) IPentahoSession(org.pentaho.platform.api.engine.IPentahoSession) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) GrantedAuthority(org.springframework.security.core.GrantedAuthority) ArrayList(java.util.ArrayList) IUserRoleDao(org.pentaho.platform.api.engine.security.userroledao.IUserRoleDao) IPentahoUser(org.pentaho.platform.api.engine.security.userroledao.IPentahoUser) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) ITenant(org.pentaho.platform.api.mt.ITenant) UncategorizedUserRoleDaoException(org.pentaho.platform.api.engine.security.userroledao.UncategorizedUserRoleDaoException) IPentahoRole(org.pentaho.platform.api.engine.security.userroledao.IPentahoRole)

Example 73 with UsernameNotFoundException

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());
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetails(org.springframework.security.core.userdetails.UserDetails) User(org.springframework.security.core.userdetails.User)

Example 74 with UsernameNotFoundException

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();
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(cn.edu.zjnu.acm.judge.domain.User) Nonnull(javax.annotation.Nonnull)

Example 75 with UsernameNotFoundException

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);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) RolePrivilegeEntity(org.codenergic.theskeleton.role.RolePrivilegeEntity)

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