Example usage for org.springframework.security.core.userdetails UsernameNotFoundException UsernameNotFoundException

List of usage examples for org.springframework.security.core.userdetails UsernameNotFoundException UsernameNotFoundException

Introduction

In this page you can find the example usage for org.springframework.security.core.userdetails UsernameNotFoundException UsernameNotFoundException.

Prototype

public UsernameNotFoundException(String msg) 

Source Link

Document

Constructs a UsernameNotFoundException with the specified message.

Usage

From source file:org.apache.nifi.web.security.authorization.NiFiAuthorizationService.java

/**
 * Loads the user details for the specified dn.
 *
 * @param dn user dn// w ww  .java  2  s  .c om
 * @return user detail
 */
private NiFiUserDetails getNiFiUserDetails(String dn) {
    try {
        NiFiUser user = userService.checkAuthorization(dn);
        return new NiFiUserDetails(user);
    } catch (AdministrationException ase) {
        throw new AuthenticationServiceException(
                String.format("An error occurred while accessing the user credentials for '%s': %s", dn,
                        ase.getMessage()),
                ase);
    } catch (AccountDisabledException | AccountPendingException e) {
        throw new AccountStatusException(e.getMessage(), e) {
        };
    } catch (AccountNotFoundException anfe) {
        throw new UsernameNotFoundException(anfe.getMessage());
    }
}

From source file:org.apache.rave.portal.service.impl.DefaultUserService.java

@Override
public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException, DataAccessException {
    log.debug("loadUserByUsername called with: {}", username);
    final User user = userRepository.getByUsername(username);
    if (user == null) {
        throw new UsernameNotFoundException("User with username '" + username + "' was not found!");
    }/*from ww w.j ava  2  s .  c om*/
    return user;
}

From source file:org.apache.rave.portal.service.impl.DefaultUserService.java

@Override
public void setAuthenticatedUser(String userId) {
    final User user = userRepository.get(userId);
    if (user == null) {
        throw new UsernameNotFoundException("User with id '" + userId + "' was not found!");
    }/*  ww w.  ja  va2s  . c om*/
    SecurityContext securityContext = createContext(user);
    SecurityContextHolder.setContext(securityContext);
}

From source file:org.apache.rave.portal.service.impl.DefaultUserService.java

@Override
public UserDetails loadUserDetails(OpenIDAuthenticationToken token) throws UsernameNotFoundException {
    final String openId = token.getIdentityUrl();
    User user = this.getUserByOpenId(openId);
    if (user == null) {
        log.info("Open ID User with URL " + openId + " was not found!");
        throw new UsernameNotFoundException("Open ID User with URL " + openId + " was not found!");
    }//from   ww  w.  j a  v  a  2 s . c  om
    return user;
}

From source file:org.apache.syncope.core.misc.security.AuthDataAccessor.java

@Transactional
public Set<SyncopeGrantedAuthority> load(final String username) {
    final Set<SyncopeGrantedAuthority> authorities = new HashSet<>();
    if (anonymousUser.equals(username)) {
        authorities.add(new SyncopeGrantedAuthority(StandardEntitlement.ANONYMOUS));
    } else if (adminUser.equals(username)) {
        CollectionUtils.collect(EntitlementsHolder.getInstance().getValues(),
                new Transformer<String, SyncopeGrantedAuthority>() {

                    @Override/*ww  w . j a v  a 2s  . com*/
                    public SyncopeGrantedAuthority transform(final String entitlement) {
                        return new SyncopeGrantedAuthority(entitlement, SyncopeConstants.ROOT_REALM);
                    }
                }, authorities);
    } else {
        User user = userDAO.find(username);
        if (user == null) {
            throw new UsernameNotFoundException("Could not find any user with id " + username);
        }

        if (user.isMustChangePassword()) {
            authorities.add(new SyncopeGrantedAuthority(StandardEntitlement.MUST_CHANGE_PASSWORD));
        } else {
            final Map<String, Set<String>> entForRealms = new HashMap<>();

            // Give entitlements as assigned by roles (with realms, where applicable) - assigned either
            // statically and dynamically
            for (final Role role : userDAO.findAllRoles(user)) {
                IterableUtils.forEach(role.getEntitlements(), new Closure<String>() {

                    @Override
                    public void execute(final String entitlement) {
                        Set<String> realms = entForRealms.get(entitlement);
                        if (realms == null) {
                            realms = new HashSet<>();
                            entForRealms.put(entitlement, realms);
                        }

                        CollectionUtils.collect(role.getRealms(), new Transformer<Realm, String>() {

                            @Override
                            public String transform(final Realm realm) {
                                return realm.getFullPath();
                            }
                        }, realms);
                    }
                });
            }

            // Give group entitlements for owned groups
            for (Group group : groupDAO.findOwnedByUser(user.getKey())) {
                for (String entitlement : Arrays.asList(StandardEntitlement.GROUP_READ,
                        StandardEntitlement.GROUP_UPDATE, StandardEntitlement.GROUP_DELETE)) {

                    Set<String> realms = entForRealms.get(entitlement);
                    if (realms == null) {
                        realms = new HashSet<>();
                        entForRealms.put(entitlement, realms);
                    }

                    realms.add(RealmUtils.getGroupOwnerRealm(group.getRealm().getFullPath(), group.getKey()));
                }
            }

            // Finally normalize realms for each given entitlement and generate authorities
            for (Map.Entry<String, Set<String>> entry : entForRealms.entrySet()) {
                SyncopeGrantedAuthority authority = new SyncopeGrantedAuthority(entry.getKey());
                authority.addRealms(RealmUtils.normalize(entry.getValue()));
                authorities.add(authority);
            }
        }
    }

    return authorities;
}

From source file:org.apache.syncope.core.misc.security.SyncopeUserDetailsService.java

@Override
public UserDetails loadUserByUsername(final String username) {
    final Set<SyncopeGrantedAuthority> authorities = new HashSet<>();
    if (anonymousUser.equals(username)) {
        authorities.add(new SyncopeGrantedAuthority(Entitlement.ANONYMOUS));
    } else if (adminUser.equals(username)) {
        CollectionUtils//from   w  ww.j  a  va 2 s.c  om
                .collect(
                        IteratorUtils.filteredIterator(Entitlement.values().iterator(),
                                PredicateUtils
                                        .notPredicate(PredicateUtils.equalPredicate(Entitlement.ANONYMOUS))),
                        new Transformer<String, SyncopeGrantedAuthority>() {

                            @Override
                            public SyncopeGrantedAuthority transform(final String entitlement) {
                                return new SyncopeGrantedAuthority(entitlement, SyncopeConstants.ROOT_REALM);
                            }
                        }, authorities);
    } else {
        org.apache.syncope.core.persistence.api.entity.user.User user = userDAO.find(username);
        if (user == null) {
            throw new UsernameNotFoundException("Could not find any user with id " + username);
        }

        // Give entitlements as assigned by roles (with realms, where applicable) - assigned either
        // statically and dynamically
        for (final Role role : userDAO.findAllRoles(user)) {
            CollectionUtils.forAllDo(role.getEntitlements(), new Closure<String>() {

                @Override
                public void execute(final String entitlement) {
                    SyncopeGrantedAuthority authority = new SyncopeGrantedAuthority(entitlement);
                    authorities.add(authority);

                    List<String> realmFullPahs = new ArrayList<>();
                    CollectionUtils.collect(role.getRealms(), new Transformer<Realm, String>() {

                        @Override
                        public String transform(final Realm realm) {
                            return realm.getFullPath();
                        }
                    }, realmFullPahs);
                    authority.addRealms(realmFullPahs);
                }
            });
        }

        // Give group entitlements for owned groups
        for (Group group : groupDAO.findOwnedByUser(user.getKey())) {
            for (String entitlement : Arrays.asList(Entitlement.GROUP_READ, Entitlement.GROUP_UPDATE,
                    Entitlement.GROUP_DELETE)) {

                SyncopeGrantedAuthority authority = new SyncopeGrantedAuthority(entitlement);
                authority.addRealm(
                        RealmUtils.getGroupOwnerRealm(group.getRealm().getFullPath(), group.getKey()));
                authorities.add(authority);
            }
        }
    }

    return new User(username, "<PASSWORD_PLACEHOLDER>", authorities);
}

From source file:org.apache.syncope.core.spring.security.AuthDataAccessor.java

@Transactional
public Set<SyncopeGrantedAuthority> getAuthorities(final String username) {
    Set<SyncopeGrantedAuthority> authorities;

    if (anonymousUser.equals(username)) {
        authorities = ANONYMOUS_AUTHORITIES;
    } else if (adminUser.equals(username)) {
        authorities = getAdminAuthorities();
    } else {//from w w  w.  j  av a2 s  .  c  om
        User user = userDAO.findByUsername(username);
        if (user == null) {
            throw new UsernameNotFoundException("Could not find any user with id " + username);
        }

        authorities = getUserAuthorities(user);
    }

    return authorities;
}

From source file:org.apdplat.module.security.service.UserDetailsServiceImpl.java

/**
 * ?//from   w  ww.j  a  v a 2 s  .  com
 * @param username ??
 * @return ?
 * @throws UsernameNotFoundException ??
 */
@Override
public synchronized UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
    //spring security?????
    SPRING_SECURITY_LAST_USERNAME = username;
    //try catchfinally??
    try {
        if (ipAccessControler.deny(OpenEntityManagerInViewFilter.request)) {
            message = "IP?";
            LOG.info(message);
            throw new UsernameNotFoundException(message);
        }
        return load(username);
    } catch (UsernameNotFoundException e) {
        throw e;
    } finally {
        LOG.debug("??? " + username + " " + message);
        messages.put(TextEscapeUtils.escapeEntities(username), message);
    }
}

From source file:org.apdplat.module.security.service.UserDetailsServiceImpl.java

private UserDetails load(String username) throws UsernameNotFoundException {
    if (FileUtils.existsFile("/WEB-INF/licence") && PropertyHolder.getBooleanProperty("security")) {
        Collection<String> reqs = FileUtils.getTextFileContent("/WEB-INF/licence");
        message = "?";
        if (reqs != null && reqs.size() == 1) {
            message += ":" + reqs.iterator().next().toString();
        }//from   w w  w  .j  av  a  2 s .com
        LOG.info(message);
        throw new UsernameNotFoundException(message);
    }
    if (StringUtils.isBlank(username)) {
        message = "??";
        LOG.info(message);
        throw new UsernameNotFoundException(message);
    }
    /* ? */
    PropertyCriteria propertyCriteria = new PropertyCriteria(Criteria.or);
    propertyCriteria.addPropertyEditor(new PropertyEditor("username", Operator.eq, "String", username));

    //PropertyEditor sub1=new PropertyEditor(Criteria.or);
    //sub1.addSubPropertyEditor(new PropertyEditor("id", Operator.eq, 1));
    //sub1.addSubPropertyEditor(new PropertyEditor("id", Operator.eq, 2));

    //PropertyEditor sub=new PropertyEditor(Criteria.and);
    //sub.addSubPropertyEditor(new PropertyEditor("id", Operator.ne, 6));
    //sub.addSubPropertyEditor(new PropertyEditor("id", Operator.ne, 7));
    //sub.addSubPropertyEditor(new PropertyEditor("id", Operator.ne, 8));
    //sub.addSubPropertyEditor(sub1);

    //propertyCriteria.addPropertyEditor(sub);

    Page<User> page = serviceFacade.query(User.class, null, propertyCriteria);

    if (page.getTotalRecords() != 1) {
        message = "??";
        LOG.info(message + ": " + username);
        throw new UsernameNotFoundException(message);
    }
    User user = page.getModels().get(0);
    message = user.loginValidate();
    if (message != null) {
        LOG.info(message);
        throw new UsernameNotFoundException(message);
    }
    //?????
    message = "??";

    return user;
}

From source file:org.artifactory.security.SecurityServiceImpl.java

@Override
public UserInfo findUser(String username) {
    UserInfo user = userGroupStoreService.findUser(username);
    if (user == null) {
        throw new UsernameNotFoundException("User " + username + " does not exists!");
    }/*w w w . j av  a  2 s . co m*/
    return user;
}