Example usage for org.springframework.security.oauth.consumer OAuthSecurityContextHolder getContext

List of usage examples for org.springframework.security.oauth.consumer OAuthSecurityContextHolder getContext

Introduction

In this page you can find the example usage for org.springframework.security.oauth.consumer OAuthSecurityContextHolder getContext.

Prototype

public static OAuthSecurityContext getContext() 

Source Link

Usage

From source file:org.springframework.security.oauth.consumer.filter.OAuthConsumerProcessingFilter.java

public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain chain)
        throws IOException, ServletException {
    HttpServletRequest request = (HttpServletRequest) servletRequest;
    HttpServletResponse response = (HttpServletResponse) servletResponse;

    Set<String> accessTokenDeps = getAccessTokenDependencies(request, response, chain);
    if (!accessTokenDeps.isEmpty()) {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        if (isRequireAuthenticated() && !authentication.isAuthenticated()) {
            throw new InsufficientAuthenticationException("An authenticated principal must be present.");
        }// w w  w . j a  va  2 s. c  om

        OAuthSecurityContext context = OAuthSecurityContextHolder.getContext();
        if (context == null) {
            throw new IllegalStateException(
                    "No OAuth security context has been established. Unable to access resources.");
        }

        Map<String, OAuthConsumerToken> accessTokens = context.getAccessTokens();

        for (String dependency : accessTokenDeps) {
            if (!accessTokens.containsKey(dependency)) {
                throw new AccessTokenRequiredException(
                        getProtectedResourceDetailsService().loadProtectedResourceDetailsById(dependency));
            }
        }

        chain.doFilter(request, response);
    } else {
        if (LOG.isDebugEnabled()) {
            LOG.debug("No access token dependencies for request.");
        }
        chain.doFilter(servletRequest, servletResponse);
    }
}