Example usage for org.springframework.security.authentication ProviderNotFoundException ProviderNotFoundException

List of usage examples for org.springframework.security.authentication ProviderNotFoundException ProviderNotFoundException

Introduction

In this page you can find the example usage for org.springframework.security.authentication ProviderNotFoundException ProviderNotFoundException.

Prototype

public ProviderNotFoundException(String msg) 

Source Link

Document

Constructs a ProviderNotFoundException with the specified message.

Usage

From source file:eu.freme.broker.security.SecurityConfig.java

@Bean
public AuthenticationManager authenticationManager() {
    return new AuthenticationManager() {
        @Autowired//from   w  w w. ja v  a2s  . c o m
        AuthenticationProvider[] authenticationProviders;

        @Override
        public Authentication authenticate(Authentication authentication) throws ProviderNotFoundException {

            for (AuthenticationProvider auth : authenticationProviders) {
                if (auth.supports(authentication.getClass())) {
                    return auth.authenticate(authentication);
                }
            }

            throw new ProviderNotFoundException(
                    "No AuthenticationProvider found for " + authentication.getClass());
        }
    };
}

From source file:com.epam.reportportal.auth.DynamicAuthProvider.java

private ProviderNotFoundException noAuthDetailsException(String name) {
    return new ProviderNotFoundException("Auth details '" + name + "' are not configured");
}

From source file:eu.freme.common.security.SecurityConfig.java

@Override
@Bean/* w w w.  jav a  2  s  . c o  m*/
public AuthenticationManager authenticationManager() {
    return new AuthenticationManager() {
        @Autowired
        AuthenticationProvider[] authenticationProviders;

        @Override
        public Authentication authenticate(Authentication authentication) throws ProviderNotFoundException {

            for (AuthenticationProvider auth : authenticationProviders) {
                if (auth.supports(authentication.getClass())) {
                    return auth.authenticate(authentication);
                }
            }

            throw new ProviderNotFoundException(
                    "No AuthenticationProvider found for " + authentication.getClass());
        }
    };
}

From source file:com.cpst.postal.settlement.user.security.CustomProviderManager.java

/**
 * Attempts to authenticate the passed {@link Authentication} object.
 * <p>/*  w  w  w  .j  ava  2s  .  c o  m*/
 * The list of {@link AuthenticationProvider}s will be successively tried until an
 * <code>AuthenticationProvider</code> indicates it is  capable of authenticating the type of
 * <code>Authentication</code> object passed. Authentication will then be attempted with that
 * <code>AuthenticationProvider</code>.
 * <p>
 * If more than one <code>AuthenticationProvider</code> supports the passed <code>Authentication</code>
 * object, only the first <code>AuthenticationProvider</code> tried will determine the result. No subsequent
 * <code>AuthenticationProvider</code>s will be tried.
 *
 * @param authentication the authentication request object.
 *
 * @return a fully authenticated object including credentials.
 *
 * @throws AuthenticationException if authentication fails.
 */
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    Class<? extends Authentication> toTest = authentication.getClass();
    AuthenticationException lastException = null;
    Authentication result = null;
    boolean debug = logger.isDebugEnabled();

    for (AuthenticationProvider provider : getProviders()) {
        if (!provider.supports(toTest)) {
            continue;
        }

        if (debug) {
            logger.debug("Authentication attempt using " + provider.getClass().getName());
        }

        try {
            result = provider.authenticate(authentication);

            if (result != null) {
                copyDetails(authentication, result);
                break;
            }
        } catch (AccountStatusException e) {
            prepareException(e, authentication);
            // SEC-546: Avoid polling additional providers if auth failure is due to invalid account status
            throw e;
        } catch (AuthenticationException e) {
            lastException = e;
        }
    }

    if (result == null && parent != null) {
        // Allow the parent to try.
        try {
            result = parent.authenticate(authentication);
        } catch (ProviderNotFoundException e) {
            // ignore as we will throw below if no other exception occurred prior to calling parent and the parent
            // may throw ProviderNotFound even though a provider in the child already handled the request
        } catch (AuthenticationException e) {
            lastException = e;
        }
    }

    if (result != null) {
        if (eraseCredentialsAfterAuthentication && (result instanceof CredentialsContainer)) {
            // Authentication is complete. Remove credentials and other secret data from authentication
            ((CredentialsContainer) result).eraseCredentials();
        }

        eventPublisher.publishAuthenticationSuccess(result);
        return result;
    }

    // Parent was null, or didn't authenticate (or throw an exception).

    if (lastException == null) {
        lastException = new ProviderNotFoundException(messages.getMessage("ProviderManager.providerNotFound",
                new Object[] { toTest.getName() }, "No AuthenticationProvider found for {0}"));
    }

    prepareException(lastException, authentication);

    throw lastException;
}

From source file:com.gfactor.web.wicket.context.ProviderManager.java

/**
 * Attempts to authenticate the passed {@link Authentication} object.
 * <p>//from www .  j  ava 2s.  c o m
 * The list of {@link AuthenticationProvider}s will be successively tried until an
 * <code>AuthenticationProvider</code> indicates it is  capable of authenticating the type of
 * <code>Authentication</code> object passed. Authentication will then be attempted with that
 * <code>AuthenticationProvider</code>.
 * <p>
 * If more than one <code>AuthenticationProvider</code> supports the passed <code>Authentication</code>
 * object, only the first <code>AuthenticationProvider</code> tried will determine the result. No subsequent
 * <code>AuthenticationProvider</code>s will be tried.
 *
 * @param authentication the authentication request object.
 *
 * @return a fully authenticated object including credentials.
 *
 * @throws AuthenticationException if authentication fails.
 */
public Authentication doAuthentication(Authentication authentication) throws AuthenticationException {
    Class<? extends Authentication> toTest = authentication.getClass();
    AuthenticationException lastException = null;
    Authentication result = null;

    for (AuthenticationProvider provider : getProviders()) {
        if (!provider.supports(toTest)) {
            continue;
        }

        logger.debug("Authentication attempt using " + provider.getClass().getName());

        try {
            result = provider.authenticate(authentication);

            if (result != null) {
                copyDetails(authentication, result);
                break;
            }
        } catch (AccountStatusException e) {
            // SEC-546: Avoid polling additional providers if auth failure is due to invalid account status
            eventPublisher.publishAuthenticationFailure(e, authentication);
            throw e;
        } catch (AuthenticationException e) {
            lastException = e;
        }
    }

    if (result == null && parent != null) {
        // Allow the parent to try.
        try {
            result = parent.authenticate(authentication);
        } catch (ProviderNotFoundException e) {
            // ignore as we will throw below if no other exception occurred prior to calling parent and the parent
            // may throw ProviderNotFound even though a provider in the child already handled the request
        } catch (AuthenticationException e) {
            lastException = e;
        }
    }

    if (result != null) {
        if (eraseCredentialsAfterAuthentication && (result instanceof CredentialsContainer)) {
            // Authentication is complete. Remove credentials and other secret data from authentication
            ((CredentialsContainer) result).eraseCredentials();
        }

        eventPublisher.publishAuthenticationSuccess(result);
        return result;
    }

    // Parent was null, or didn't authenticate (or throw an exception).

    if (lastException == null) {
        lastException = new ProviderNotFoundException(messages.getMessage("ProviderManager.providerNotFound",
                new Object[] { toTest.getName() }, "No AuthenticationProvider found for {0}"));
    }

    eventPublisher.publishAuthenticationFailure(lastException, authentication);

    throw lastException;
}

From source file:org.cloudfoundry.identity.uaa.authentication.manager.ChainedAuthenticationManager.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (authentication == null) {
        return authentication;
    }//from   ww  w. j a  va2s  .  c o  m
    UsernamePasswordAuthenticationToken output = null;
    if (authentication instanceof UsernamePasswordAuthenticationToken) {
        output = (UsernamePasswordAuthenticationToken) authentication;
    } else {
        output = new UsernamePasswordAuthenticationToken(authentication.getPrincipal(),
                authentication.getCredentials(), authentication.getAuthorities());
        output.setDetails(authentication.getDetails());
    }
    boolean authenticated = false;
    Authentication auth = null;
    AuthenticationException lastException = null;
    boolean lastResult = false;
    boolean shallContinue = true;
    if (delegates == null || delegates.length == 0) {
        throw new ProviderNotFoundException("No available authentication providers.");
    }
    for (int i = 0; shallContinue && i < delegates.length; i++) {

        boolean shallAuthenticate = (i == 0)
                || (lastResult && IF_PREVIOUS_TRUE.equals(delegates[i].getRequired()))
                || ((!lastResult) && IF_PREVIOUS_FALSE.equals(delegates[i].getRequired()));

        if (shallAuthenticate) {
            if (logger.isDebugEnabled()) {
                logger.debug("Attempting chained authentication of " + output + " with manager:"
                        + delegates[i].getAuthenticationManager() + " required:" + delegates[i].getRequired());
            }
            Authentication thisAuth = null;
            try {
                thisAuth = delegates[i].getAuthenticationManager().authenticate(auth != null ? auth : output);
            } catch (AuthenticationException x) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Chained authentication exception:" + x.getMessage() + " at:"
                            + (x.getStackTrace().length > 0 ? x.getStackTrace()[0] : "(no stack trace)"));
                }
                lastException = x;
                if (delegates[i].getStopIf() != null) {
                    for (Class<? extends AuthenticationException> exceptionClass : delegates[i].getStopIf()) {
                        if (exceptionClass.isAssignableFrom(x.getClass())) {
                            shallContinue = false;
                            break;
                        }
                    }
                }
            }
            lastResult = thisAuth != null && thisAuth.isAuthenticated();

            if (lastResult) {
                authenticated = true;
                auth = thisAuth;
            } else {
                authenticated = false;
                auth = null;
            }

        } else {
            shallContinue = false;
        }
        if (logger.isDebugEnabled()) {
            logger.debug("Chained Authentication status of " + output + " with manager:" + delegates[i]
                    + "; Authenticated:" + authenticated);
        }
    }
    if (authenticated) {
        return auth;
    } else if (lastException != null) {
        //we had at least one authentication exception, throw it
        throw lastException;
    } else {
        //not authenticated, but return the last of the result
        return auth;
    }
}

From source file:org.cloudfoundry.identity.uaa.login.saml.LoginSamlAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (!supports(authentication.getClass())) {
        throw new IllegalArgumentException(
                "Only SAMLAuthenticationToken is supported, " + authentication.getClass() + " was attempted");
    }/*from   w  w  w  .j av  a 2s . c o  m*/

    IdentityZone zone = IdentityZoneHolder.get();

    SAMLAuthenticationToken token = (SAMLAuthenticationToken) authentication;
    SAMLMessageContext context = token.getCredentials();
    String alias = context.getPeerExtendedMetadata().getAlias();
    boolean addNew = true;
    IdentityProvider<SamlIdentityProviderDefinition> idp;
    SamlIdentityProviderDefinition samlConfig;
    try {
        idp = identityProviderProvisioning.retrieveByOrigin(alias, IdentityZoneHolder.get().getId());
        samlConfig = idp.getConfig();
        addNew = samlConfig.isAddShadowUserOnLogin();
        if (!idp.isActive()) {
            throw new ProviderNotFoundException("Identity Provider has been disabled by administrator.");
        }
    } catch (EmptyResultDataAccessException x) {
        throw new ProviderNotFoundException("Not identity provider found in zone.");
    }
    ExpiringUsernameAuthenticationToken result = getExpiringUsernameAuthenticationToken(authentication);
    UaaPrincipal samlPrincipal = new UaaPrincipal(Origin.NotANumber, result.getName(), result.getName(), alias,
            result.getName(), zone.getId());
    Collection<? extends GrantedAuthority> samlAuthorities = retrieveSamlAuthorities(samlConfig,
            (SAMLCredential) result.getCredentials());
    Collection<? extends GrantedAuthority> authorities = mapAuthorities(idp.getOriginKey(), samlAuthorities);

    Set<String> filteredExternalGroups = filterSamlAuthorities(samlConfig, samlAuthorities);
    MultiValueMap<String, String> userAttributes = retrieveUserAttributes(samlConfig,
            (SAMLCredential) result.getCredentials());
    UaaUser user = createIfMissing(samlPrincipal, addNew, authorities, userAttributes);
    UaaPrincipal principal = new UaaPrincipal(user);
    return new LoginSamlAuthenticationToken(principal, result).getUaaAuthentication(user.getAuthorities(),
            filteredExternalGroups, userAttributes);
}

From source file:org.cloudfoundry.identity.uaa.provider.saml.LoginSamlAuthenticationProvider.java

@Override
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    if (!supports(authentication.getClass())) {
        throw new IllegalArgumentException(
                "Only SAMLAuthenticationToken is supported, " + authentication.getClass() + " was attempted");
    }/*from  www. j a v  a 2s. co m*/

    IdentityZone zone = IdentityZoneHolder.get();

    SAMLAuthenticationToken token = (SAMLAuthenticationToken) authentication;
    SAMLMessageContext context = token.getCredentials();
    String alias = context.getPeerExtendedMetadata().getAlias();
    boolean addNew;
    IdentityProvider<SamlIdentityProviderDefinition> idp;
    SamlIdentityProviderDefinition samlConfig;
    try {
        idp = identityProviderProvisioning.retrieveByOrigin(alias, IdentityZoneHolder.get().getId());
        samlConfig = idp.getConfig();
        addNew = samlConfig.isAddShadowUserOnLogin();
        if (!idp.isActive()) {
            throw new ProviderNotFoundException(
                    "Identity Provider has been disabled by administrator for alias:" + alias);
        }
    } catch (EmptyResultDataAccessException x) {
        throw new ProviderNotFoundException("No SAML identity provider found in zone for alias:" + alias);
    }
    ExpiringUsernameAuthenticationToken result = getExpiringUsernameAuthenticationToken(authentication);
    UaaPrincipal samlPrincipal = new UaaPrincipal(OriginKeys.NotANumber, result.getName(), result.getName(),
            alias, result.getName(), zone.getId());
    Collection<? extends GrantedAuthority> samlAuthorities = retrieveSamlAuthorities(samlConfig,
            (SAMLCredential) result.getCredentials());

    Collection<? extends GrantedAuthority> authorities = null;
    SamlIdentityProviderDefinition.ExternalGroupMappingMode groupMappingMode = idp.getConfig()
            .getGroupMappingMode();
    switch (groupMappingMode) {
    case EXPLICITLY_MAPPED:
        authorities = mapAuthorities(idp.getOriginKey(), samlAuthorities);
        break;
    case AS_SCOPES:
        authorities = new LinkedList<>(samlAuthorities);
        break;
    }

    Set<String> filteredExternalGroups = filterSamlAuthorities(samlConfig, samlAuthorities);
    MultiValueMap<String, String> userAttributes = retrieveUserAttributes(samlConfig,
            (SAMLCredential) result.getCredentials());
    UaaUser user = createIfMissing(samlPrincipal, addNew, authorities, userAttributes);
    UaaPrincipal principal = new UaaPrincipal(user);
    UaaAuthentication resultUaaAuthentication = new LoginSamlAuthenticationToken(principal, result)
            .getUaaAuthentication(user.getAuthorities(), filteredExternalGroups, userAttributes);
    if (samlConfig.isStoreCustomAttributes()) {
        userDatabase.storeUserInfo(user.getId(),
                new UserInfo().setUserAttributes(resultUaaAuthentication.getUserAttributes())
                        .setRoles(new LinkedList(resultUaaAuthentication.getExternalGroups())));
    }
    return resultUaaAuthentication;
}

From source file:org.springframework.security.authentication.ProviderManager.java

/**
 * Attempts to authenticate the passed {@link Authentication} object.
 * <p>//from  w  ww  .  j  a v a 2s. co  m
 * The list of {@link AuthenticationProvider}s will be successively tried until an
 * <code>AuthenticationProvider</code> indicates it is capable of authenticating the
 * type of <code>Authentication</code> object passed. Authentication will then be
 * attempted with that <code>AuthenticationProvider</code>.
 * <p>
 * If more than one <code>AuthenticationProvider</code> supports the passed
 * <code>Authentication</code> object, the first one able to successfully
 * authenticate the <code>Authentication</code> object determines the
 * <code>result</code>, overriding any possible <code>AuthenticationException</code>
 * thrown by earlier supporting <code>AuthenticationProvider</code>s.
 * On successful authentication, no subsequent <code>AuthenticationProvider</code>s
 * will be tried.
 * If authentication was not successful by any supporting
 * <code>AuthenticationProvider</code> the last thrown
 * <code>AuthenticationException</code> will be rethrown.
 *
 * @param authentication the authentication request object.
 *
 * @return a fully authenticated object including credentials.
 *
 * @throws AuthenticationException if authentication fails.
 */
public Authentication authenticate(Authentication authentication) throws AuthenticationException {
    Class<? extends Authentication> toTest = authentication.getClass();
    AuthenticationException lastException = null;
    AuthenticationException parentException = null;
    Authentication result = null;
    Authentication parentResult = null;
    boolean debug = logger.isDebugEnabled();

    for (AuthenticationProvider provider : getProviders()) {
        if (!provider.supports(toTest)) {
            continue;
        }

        if (debug) {
            logger.debug("Authentication attempt using " + provider.getClass().getName());
        }

        try {
            result = provider.authenticate(authentication);

            if (result != null) {
                copyDetails(authentication, result);
                break;
            }
        } catch (AccountStatusException e) {
            prepareException(e, authentication);
            // SEC-546: Avoid polling additional providers if auth failure is due to
            // invalid account status
            throw e;
        } catch (InternalAuthenticationServiceException e) {
            prepareException(e, authentication);
            throw e;
        } catch (AuthenticationException e) {
            lastException = e;
        }
    }

    if (result == null && parent != null) {
        // Allow the parent to try.
        try {
            result = parentResult = parent.authenticate(authentication);
        } catch (ProviderNotFoundException e) {
            // ignore as we will throw below if no other exception occurred prior to
            // calling parent and the parent
            // may throw ProviderNotFound even though a provider in the child already
            // handled the request
        } catch (AuthenticationException e) {
            lastException = parentException = e;
        }
    }

    if (result != null) {
        if (eraseCredentialsAfterAuthentication && (result instanceof CredentialsContainer)) {
            // Authentication is complete. Remove credentials and other secret data
            // from authentication
            ((CredentialsContainer) result).eraseCredentials();
        }

        // If the parent AuthenticationManager was attempted and successful than it will publish an AuthenticationSuccessEvent
        // This check prevents a duplicate AuthenticationSuccessEvent if the parent AuthenticationManager already published it
        if (parentResult == null) {
            eventPublisher.publishAuthenticationSuccess(result);
        }
        return result;
    }

    // Parent was null, or didn't authenticate (or throw an exception).

    if (lastException == null) {
        lastException = new ProviderNotFoundException(messages.getMessage("ProviderManager.providerNotFound",
                new Object[] { toTest.getName() }, "No AuthenticationProvider found for {0}"));
    }

    // If the parent AuthenticationManager was attempted and failed than it will publish an AbstractAuthenticationFailureEvent
    // This check prevents a duplicate AbstractAuthenticationFailureEvent if the parent AuthenticationManager already published it
    if (parentException == null) {
        prepareException(lastException, authentication);
    }

    throw lastException;
}