Search in sources :

Example 96 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project molgenis by molgenis.

the class UserDetailsService method loadUserByUsername.

@Override
@RunAsSystem
public UserDetails loadUserByUsername(String username) {
    User user = dataService.query(UserMetaData.USER, User.class).eq(UserMetaData.USERNAME, username).findOne();
    if (user == null) {
        throw new UsernameNotFoundException("unknown user '" + username + "'");
    }
    Collection<? extends GrantedAuthority> authorities = getAuthorities(user);
    return new org.springframework.security.core.userdetails.User(user.getUsername(), user.getPassword(), user.isActive(), true, true, true, authorities);
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(org.molgenis.data.security.auth.User) RunAsSystem(org.molgenis.security.core.runas.RunAsSystem)

Example 97 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project hello-world by haoziapple.

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> userByEmailFromDatabase = userRepository.findOneWithAuthoritiesByEmail(lowercaseLogin);
    return userByEmailFromDatabase.map(user -> createSpringSecurityUser(lowercaseLogin, user)).orElseGet(() -> {
        Optional<User> userByLoginFromDatabase = userRepository.findOneWithAuthoritiesByLogin(lowercaseLogin);
        return userByLoginFromDatabase.map(user -> createSpringSecurityUser(lowercaseLogin, user)).orElseThrow(() -> new UsernameNotFoundException("User " + lowercaseLogin + " was not found in the " + "database"));
    });
}
Also used : java.util(java.util) Logger(org.slf4j.Logger) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) LoggerFactory(org.slf4j.LoggerFactory) UserDetailsService(org.springframework.security.core.userdetails.UserDetailsService) SimpleGrantedAuthority(org.springframework.security.core.authority.SimpleGrantedAuthority) User(com.haozi.app.domain.User) Collectors(java.util.stream.Collectors) GrantedAuthority(org.springframework.security.core.GrantedAuthority) UserRepository(com.haozi.app.repository.UserRepository) Component(org.springframework.stereotype.Component) UserDetails(org.springframework.security.core.userdetails.UserDetails) Transactional(org.springframework.transaction.annotation.Transactional) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(com.haozi.app.domain.User) Transactional(org.springframework.transaction.annotation.Transactional)

Example 98 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project vaadin-jsf-integration by alejandro-du.

the class PasswordHint method execute.

public String execute() {
    getFacesContext().getViewRoot().setViewId("/passwordHint.xhtml");
    // ensure that the username has been sent
    if (username == null || "".equals(username)) {
        log.warn("Username not specified, notifying user that it's a required field.");
        addError("errors.required", getText("user.username"));
        return null;
    } else if (username.endsWith(".xhtml")) {
        username = username.substring(0, username.indexOf(".xhtml"));
    }
    if (log.isDebugEnabled()) {
        log.debug("Processing Password Hint...");
    }
    // look up the user's information
    try {
        User user = userManager.getUserByUsername(username);
        StringBuilder msg = new StringBuilder();
        msg.append("Your password hint is: ").append(user.getPasswordHint());
        msg.append("\n\nLogin at: ").append(RequestUtil.getAppURL(getRequest()));
        message.setTo(user.getEmail());
        String subject = '[' + getText("webapp.name") + "] " + getText("user.passwordHint");
        message.setSubject(subject);
        message.setText(msg.toString());
        mailEngine.send(message);
        addMessage("login.passwordHint.sent", new Object[] { username, user.getEmail() });
    } catch (UsernameNotFoundException e) {
        log.warn(e.getMessage());
        // If exception is expected do not rethrow
        addError("login.passwordHint.error", username);
    } catch (MailException me) {
        addError(me.getCause().getLocalizedMessage());
    }
    return "success";
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) User(org.appfuse.model.User) MailException(org.springframework.mail.MailException)

Example 99 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project summerb by skarpushin.

the class UserDetailsServiceDefaultImpl method loadUserByUsername.

@Override
public UserDetails loadUserByUsername(String userEmail) throws UsernameNotFoundException {
    try {
        User user = userService.getUserByEmail(userEmail);
        List<String> permissions = permissionService.findUserPermissionsForSubject(SecurityConstants.DOMAIN, user.getUuid(), null);
        AuthToken authToken = null;
        UserDetailsImpl ret = new UserDetailsImpl(user, null, permissions, authToken);
        return ret;
    } catch (UserNotFoundException e) {
        throw new UsernameNotFoundException("User not found", e);
    } catch (FieldValidationException e) {
        throw new UsernameNotFoundException("Email provided in invalid format", e);
    } catch (Throwable t) {
        throw new UsernameNotFoundException("Failed to get user by email", t);
    }
}
Also used : UserDetailsImpl(org.summerb.approaches.springmvc.security.dto.UserDetailsImpl) UserNotFoundException(org.summerb.microservices.users.api.exceptions.UserNotFoundException) UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) FieldValidationException(org.summerb.approaches.validation.FieldValidationException) User(org.summerb.microservices.users.api.dto.User) AuthToken(org.summerb.microservices.users.api.dto.AuthToken)

Example 100 with UsernameNotFoundException

use of org.springframework.security.core.userdetails.UsernameNotFoundException in project fw-cloud-framework by liuweijw.

the class AjaxAuthenticationProvider method authenticate.

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    AjaxAuthenticationToken ajaxAuthenticationToken = (AjaxAuthenticationToken) authentication;
    AuthUser user = userService.findUserByMobile((String) ajaxAuthenticationToken.getPrincipal());
    if (null == user)
        throw new UsernameNotFoundException("登录账户[" + ajaxAuthenticationToken.getPrincipal() + "]不存在");
    UserDetailsImpl userDetails = buildUserDeatils(user);
    if (null == userDetails)
        throw new InternalAuthenticationServiceException("登录用户[" + ajaxAuthenticationToken.getPrincipal() + "]不存在!");
    AjaxAuthenticationToken authenticationToken = new AjaxAuthenticationToken(userDetails, userDetails.getAuthorities());
    authenticationToken.setDetails(ajaxAuthenticationToken.getDetails());
    return authenticationToken;
}
Also used : UsernameNotFoundException(org.springframework.security.core.userdetails.UsernameNotFoundException) UserDetailsImpl(com.github.liuweijw.system.auth.service.UserDetailsImpl) InternalAuthenticationServiceException(org.springframework.security.authentication.InternalAuthenticationServiceException) AuthUser(com.github.liuweijw.core.beans.system.AuthUser)

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