org.trustedanalytics.user.common.OAuth2PriviligedInterceptor.java Source code

Java tutorial

Introduction

Here is the source code for org.trustedanalytics.user.common.OAuth2PriviligedInterceptor.java

Source

/**
 *  Copyright(c)2015 IntelCorporation
 *
 *  LicensedundertheApacheLicense,Version2.0(the"License");
 *  youmaynotusethisfileexceptincompliancewiththeLicense.
 *  YoumayobtainacopyoftheLicenseat
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unlessrequiredbyapplicablelaworagreedtoinwriting,software
 *  distributedundertheLicenseisdistributedonan"ASIS"BASIS,
 *  WITHOUTWARRANTIESORCONDITIONSOFANYKIND,eitherexpressorimplied.
 *  SeetheLicenseforthespecificlanguagegoverningpermissionsand
 *  limitationsundertheLicense.
 */
package org.trustedanalytics.user.common;

import feign.RequestInterceptor;
import feign.RequestTemplate;
import org.springframework.security.oauth2.client.OAuth2RestTemplate;
import org.springframework.security.oauth2.client.resource.OAuth2ProtectedResourceDetails;
import org.springframework.security.oauth2.common.OAuth2AccessToken;

public class OAuth2PriviligedInterceptor implements RequestInterceptor {

    private final OAuth2ProtectedResourceDetails clientCredentials;

    public OAuth2PriviligedInterceptor(OAuth2ProtectedResourceDetails clientCredentials) {
        this.clientCredentials = clientCredentials;
    }

    @Override
    public void apply(RequestTemplate requestTemplate) {
        OAuth2RestTemplate rt = new OAuth2RestTemplate(clientCredentials);
        OAuth2AccessToken accessToken = rt.getAccessToken();
        requestTemplate.header("Authorization", "bearer " + accessToken.getValue());
    }
}