1 package de.juplo.facebook;
4 import de.juplo.facebook.token.SignedRequestAwareAuthorizationCodeAccessTokenProvider;
5 import de.juplo.facebook.client.GraphApiErrorHandler;
6 import java.util.Arrays;
7 import java.util.LinkedList;
9 import javax.annotation.PostConstruct;
10 import org.slf4j.Logger;
11 import org.slf4j.LoggerFactory;
12 import org.springframework.beans.BeansException;
13 import org.springframework.beans.factory.annotation.Autowired;
14 import org.springframework.beans.factory.config.BeanPostProcessor;
15 import org.springframework.context.annotation.Bean;
16 import org.springframework.context.annotation.Configuration;
17 import org.springframework.security.oauth2.client.OAuth2RestTemplate;
18 import org.springframework.security.oauth2.client.http.OAuth2ErrorHandler;
19 import org.springframework.security.oauth2.client.token.AccessTokenProvider;
20 import org.springframework.security.oauth2.client.token.AccessTokenProviderChain;
21 import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider;
22 import org.springframework.security.oauth2.client.token.grant.implicit.ImplicitAccessTokenProvider;
23 import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider;
28 * This class injects the facebook-utils into the spring-oauth2-configuration
33 public class FacebookUtils
35 private final Logger log = LoggerFactory.getLogger(FacebookUtils.class);
38 @Autowired(required=false)
39 private List<AccessTokenProvider> accessTokenProviderChain;
45 if (accessTokenProviderChain == null)
47 log.info("no AccessTokenProviderChain configured, creating default-chain");
48 accessTokenProviderChain =
49 Arrays.<AccessTokenProvider> asList(
50 new ImplicitAccessTokenProvider(),
51 new ResourceOwnerPasswordAccessTokenProvider(),
52 new ClientCredentialsAccessTokenProvider()
59 public BeanPostProcessor getBeanPostProcessor(final String clientSecret)
61 log.debug("createing new instance of BeanPostProcessor");
62 return new BeanPostProcessor() {
65 public Object postProcessBeforeInitialization(
72 if (bean instanceof OAuth2RestTemplate)
74 log.info("injecting signed_request-aware AccessTokenProviderChain");
75 OAuth2RestTemplate template = (OAuth2RestTemplate)bean;
76 List<AccessTokenProvider> chain =
77 new LinkedList<>(accessTokenProviderChain);
78 SignedRequestAwareAuthorizationCodeAccessTokenProvider provider =
79 new SignedRequestAwareAuthorizationCodeAccessTokenProvider();
80 provider.setSecret(clientSecret);
82 template.setAccessTokenProvider(new AccessTokenProviderChain(chain));
83 log.info("injecting GraphApiErrorHandler");
84 template.setErrorHandler(
85 new GraphApiErrorHandler(
86 (OAuth2ErrorHandler)template.getErrorHandler()
95 public Object postProcessAfterInitialization(