Java tutorial
/* * Copyright 2011 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.notemyweb.config; import javax.inject.Inject; import org.springframework.beans.factory.annotation.Value; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.web.savedrequest.RequestCache; import org.springframework.social.connect.Connection; import org.springframework.social.connect.ConnectionFactory; import org.springframework.social.connect.ConnectionFactoryLocator; import org.springframework.social.connect.ConnectionRepository; import org.springframework.social.connect.UsersConnectionRepository; import org.springframework.social.connect.support.ConnectionFactoryRegistry; import org.springframework.social.connect.web.ProviderSignInController; import org.springframework.social.facebook.api.Facebook; import org.springframework.social.facebook.api.impl.FacebookTemplate; import org.springframework.social.facebook.connect.FacebookConnectionFactory; import com.notemyweb.spring.security.AwsPersistentTokenBasedRememberMeServices; import com.notemyweb.spring.security.SimpleSignInAdapter; /** * Spring Social Configuration. * */ @Configuration public class SocialConfig { @Value("${PARAM1}") private String accessKey; @Value("${PARAM2}") private String secretKey; @Inject private UsersConnectionRepository usersConnectionRepository; /** * When a new provider is added to the app, register its {@link ConnectionFactory} here. * @see FacebookConnectionFactory */ @Bean public ConnectionFactoryLocator connectionFactoryLocator() { ConnectionFactoryRegistry registry = new ConnectionFactoryRegistry(); registry.addConnectionFactory(new FacebookConnectionFactory(accessKey, secretKey)); /*registry.addConnectionFactory(new GoogleConnectionFactory( googleClientId, googleClientSecret));*/ return registry; } @Bean @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES) public ConnectionRepository connectionRepository() { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null) { throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in"); } return usersConnectionRepository.createConnectionRepository(authentication.getName()); } @Bean @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES) public Facebook facebook() { Connection<Facebook> facebook = connectionRepository().findPrimaryConnection(Facebook.class); return facebook != null ? facebook.getApi() : new FacebookTemplate(); } @Bean public ProviderSignInController providerSignInController(RequestCache requestCache, AwsPersistentTokenBasedRememberMeServices tokenBasedRememberMeServices) { return new ProviderSignInController(connectionFactoryLocator(), usersConnectionRepository, new SimpleSignInAdapter(requestCache, tokenBasedRememberMeServices)); } }