From: Kai Moritz Date: Fri, 21 Mar 2014 16:04:52 +0000 (+0100) Subject: Created class FacebookUtils, that configures the added extensions X-Git-Tag: facebook-utils-1.0~7 X-Git-Url: http://juplo.de/gitweb/?a=commitdiff_plain;h=504b7c003be150f78b45d308bc29ba5242dbf568;p=facebook-utils Created class FacebookUtils, that configures the added extensions --- diff --git a/src/main/java/de/juplo/facebook/FacebookUtils.java b/src/main/java/de/juplo/facebook/FacebookUtils.java new file mode 100644 index 0000000..bf0a5df --- /dev/null +++ b/src/main/java/de/juplo/facebook/FacebookUtils.java @@ -0,0 +1,132 @@ +package de.juplo.facebook; + + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.List; +import javax.annotation.PostConstruct; +import org.codehaus.jackson.map.ObjectMapper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.config.BeanPostProcessor; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.http.converter.HttpMessageConverter; +import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter; +import org.springframework.security.oauth2.client.OAuth2RestTemplate; +import org.springframework.security.oauth2.client.token.AccessTokenProvider; +import org.springframework.security.oauth2.client.token.AccessTokenProviderChain; +import org.springframework.security.oauth2.client.token.grant.client.ClientCredentialsAccessTokenProvider; +import org.springframework.security.oauth2.client.token.grant.implicit.ImplicitAccessTokenProvider; +import org.springframework.security.oauth2.client.token.grant.password.ResourceOwnerPasswordAccessTokenProvider; +import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter; + + + +/** + * This class injects the facebook-utils into the spring-oauth2-configuration + * + * @author kai + */ +@Configuration +public class FacebookUtils +{ + private final Logger log = LoggerFactory.getLogger(FacebookUtils.class); + + + @Autowired(required=false) + private List accessTokenProviderChain; + /** + * Needed, to extract Jackson-ObjectMapper. + * Defined by + */ + @Autowired + private RequestMappingHandlerAdapter requestMappingHandlerAdapter; + + private ObjectMapper objectMapper; + + + @PostConstruct + public void init() + { + for (HttpMessageConverter messageConverter : requestMappingHandlerAdapter.getMessageConverters()) + { + if (messageConverter instanceof MappingJacksonHttpMessageConverter) + { + MappingJacksonHttpMessageConverter m = + (MappingJacksonHttpMessageConverter)messageConverter; + objectMapper = m.getObjectMapper(); + log.info( + "found ObjectMapper {} ({})", + objectMapper, + objectMapper.getClass().getCanonicalName() + ); + break; + } + } + if (objectMapper == null) + { + throw new RuntimeException( + "Unable to find MappingJAcksonHttpMessageConverter!" + ); + } + + if (accessTokenProviderChain == null) + { + log.info("no AccessTokenProviderChain configured, creating default-chain"); + accessTokenProviderChain = + Arrays. asList( + new ImplicitAccessTokenProvider(), + new ResourceOwnerPasswordAccessTokenProvider(), + new ClientCredentialsAccessTokenProvider() + ); + } + } + + + @Bean + public BeanPostProcessor getBeanPostProcessor(final String clientSecret) + { + log.debug("createing new instance of BeanPostProcessor"); + return new BeanPostProcessor() { + + @Override + public Object postProcessBeforeInitialization( + Object bean, + String beanName + ) + throws + BeansException + { + if (bean instanceof OAuth2RestTemplate) + { + log.info("injecting signed_request-aware AccessTokenProviderChain"); + OAuth2RestTemplate template = (OAuth2RestTemplate)bean; + List chain = + new LinkedList<>(accessTokenProviderChain); + SignedRequestAwareAuthorizationCodeAccessTokenProvider provider = + new SignedRequestAwareAuthorizationCodeAccessTokenProvider(); + provider.setSecret(clientSecret); + provider.setObjectMapper(objectMapper); + chain.add(provider); + template.setAccessTokenProvider(new AccessTokenProviderChain(chain)); + } + + return bean; + } + + @Override + public Object postProcessAfterInitialization( + Object bean, + String beanName + ) + throws + BeansException + { + return bean; + } + }; + } +}