Java tutorial
/* * Copyright 2016 Folion Media, Inc. * * 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.folion.config; import com.folion.core.srv.SocialAccountService; import com.folion.social.CouchbaseUsersConnectionRepository; import com.folion.social.WebSigninAdapter; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Scope; import org.springframework.context.annotation.ScopedProxyMode; import org.springframework.core.env.Environment; import org.springframework.security.core.Authentication; import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.security.web.savedrequest.RequestCache; import org.springframework.social.UserIdSource; import org.springframework.social.config.annotation.ConnectionFactoryConfigurer; import org.springframework.social.config.annotation.EnableSocial; import org.springframework.social.config.annotation.SocialConfigurer; import org.springframework.social.connect.Connection; import org.springframework.social.connect.ConnectionFactoryLocator; import org.springframework.social.connect.ConnectionRepository; import org.springframework.social.connect.UsersConnectionRepository; import org.springframework.social.connect.web.ConnectController; import org.springframework.social.connect.web.ProviderSignInController; import org.springframework.social.connect.web.ReconnectFilter; import org.springframework.social.facebook.api.Facebook; import org.springframework.social.facebook.security.FacebookAuthenticationService; import org.springframework.social.facebook.web.DisconnectController; import javax.inject.Inject; @EnableSocial public class SocialConfiguration implements SocialConfigurer { @Inject private SocialAccountService socialAccountService; @Bean(name = "socialUserService") @Scope("singleton") public SocialAccountService socialAccountService() { return new SocialAccountService(); } @Override public void addConnectionFactories(ConnectionFactoryConfigurer connectionFactoryConfigurer, Environment env) { FacebookAuthenticationService facebookAuthenticationService = new FacebookAuthenticationService( env.getProperty("facebook.appKey"), env.getProperty("facebook.appSecret")); facebookAuthenticationService.setDefaultScope("email"); connectionFactoryConfigurer.addConnectionFactory(facebookAuthenticationService.getConnectionFactory()); } @Override public UserIdSource getUserIdSource() { return () -> { Authentication authentication = SecurityContextHolder.getContext().getAuthentication(); if (authentication == null) { throw new IllegalStateException("Unable to get a ConnectionRepository: no user signed in"); } return authentication.getName(); }; } @Override public UsersConnectionRepository getUsersConnectionRepository( ConnectionFactoryLocator connectionFactoryLocator) { return new CouchbaseUsersConnectionRepository(socialAccountService, connectionFactoryLocator); } // // API Binding Beans // @Bean @Scope(value = "request", proxyMode = ScopedProxyMode.INTERFACES) public Facebook facebook(ConnectionRepository repository) { Connection<Facebook> connection = repository.findPrimaryConnection(Facebook.class); return connection != null ? connection.getApi() : null; } // // Web Controller and Filter Beans // @Bean public ConnectController connectController(ConnectionFactoryLocator connectionFactoryLocator, ConnectionRepository connectionRepository) { ConnectController connectController = new ConnectController(connectionFactoryLocator, connectionRepository); //connectController.addInterceptor(new PostToWallAfterConnectInterceptor()); //connectController.addInterceptor(new TweetAfterConnectInterceptor()); return connectController; } @Bean public ProviderSignInController providerSignInController(ConnectionFactoryLocator connectionFactoryLocator, UsersConnectionRepository usersConnectionRepository, RequestCache requestCache) { return new ProviderSignInController(connectionFactoryLocator, usersConnectionRepository, new WebSigninAdapter(requestCache, socialAccountService)); } @Bean public DisconnectController disconnectController(UsersConnectionRepository usersConnectionRepository, Environment env) { return new DisconnectController(usersConnectionRepository, env.getProperty("facebook.appSecret")); } @Bean public ReconnectFilter apiExceptionHandler(UsersConnectionRepository usersConnectionRepository, UserIdSource userIdSource) { return new ReconnectFilter(usersConnectionRepository, this.getUserIdSource()); } }