javascript
为MongoDB定制Spring Social Connect框架
 在上一篇文章中 ,我談到了我面臨的第一個(gè)挑戰(zhàn)是更改數(shù)據(jù)模型并添加連接框架。 在這里,我想提供有關(guān)我如何做的更多細(xì)節(jié)。 Spring Social項(xiàng)目已經(jīng)提供了基于jdbc的連接存儲(chǔ)庫(kù)實(shí)現(xiàn),以將用戶連接數(shù)據(jù)持久保存到關(guān)系數(shù)據(jù)庫(kù)中。 但是,我使用的是MongoDB,因此我需要自定義代碼,并且發(fā)現(xiàn)這樣做相對(duì)容易。 用戶連接數(shù)據(jù)將保存為UserSocialConnection的對(duì)象,它是一個(gè)MongoDB文檔: 
BaseEntity僅具有“ id”。 在Spring Data項(xiàng)目的幫助下,我不需要為UserSocialConnection編寫(xiě)任何CRUD操作代碼,只需擴(kuò)展MongoRepository :
public interface UserSocialConnectionRepository extends MongoRepository<UserSocialConnection, String>{List<UserSocialConnection> findByUserId(String userId);List<UserSocialConnection> findByUserIdAndProviderId(String userId, String providerId);List<UserSocialConnection> findByProviderIdAndProviderUserId(String providerId, String providerUserId);UserSocialConnection findByUserIdAndProviderIdAndProviderUserId(String userId, String providerId, String providerUserId);List<UserSocialConnection> findByProviderIdAndProviderUserIdIn(String providerId, Collection<String> providerUserIds); }在擁有數(shù)據(jù)庫(kù)UserSocialConnectionRepository ,我們將實(shí)現(xiàn)Spring Social所需的ConnectionRepository和UsersConnectionRepository 。 我只是從JdbcConnectionRepository和JdbcUsersConnectionRepository復(fù)制了代碼,并創(chuàng)建了自己的MongoConnectionRepository和MongoUsersConnectionRepository 。
public class MongoUsersConnectionRepository implements UsersConnectionRepository{private final UserSocialConnectionRepository userSocialConnectionRepository;private final SocialAuthenticationServiceLocator socialAuthenticationServiceLocator;private final TextEncryptor textEncryptor;private ConnectionSignUp connectionSignUp;public MongoUsersConnectionRepository(UserSocialConnectionRepository userSocialConnectionRepository, SocialAuthenticationServiceLocator socialAuthenticationServiceLocator, TextEncryptor textEncryptor){this.userSocialConnectionRepository = userSocialConnectionRepository;this.socialAuthenticationServiceLocator = socialAuthenticationServiceLocator;this.textEncryptor = textEncryptor;}/*** The command to execute to create a new local user profile in the event no user id could be mapped to a connection.* Allows for implicitly creating a user profile from connection data during a provider sign-in attempt.* Defaults to null, indicating explicit sign-up will be required to complete the provider sign-in attempt.* @see #findUserIdsWithConnection(Connection)*/public void setConnectionSignUp(ConnectionSignUp connectionSignUp) {this.connectionSignUp = connectionSignUp;}public List<String> findUserIdsWithConnection(Connection<?> connection) {ConnectionKey key = connection.getKey();List<UserSocialConnection> userSocialConnectionList = this.userSocialConnectionRepository.findByProviderIdAndProviderUserId(key.getProviderId(), key.getProviderUserId());List<String> localUserIds = new ArrayList<String>();for (UserSocialConnection userSocialConnection : userSocialConnectionList){localUserIds.add(userSocialConnection.getUserId());}if (localUserIds.size() == 0 && connectionSignUp != null) {String newUserId = connectionSignUp.execute(connection);if (newUserId != null){createConnectionRepository(newUserId).addConnection(connection);return Arrays.asList(newUserId);}}return localUserIds;}public Set<String> findUserIdsConnectedTo(String providerId, Set<String> providerUserIds) {final Set<String> localUserIds = new HashSet<String>();List<UserSocialConnection> userSocialConnectionList = this.userSocialConnectionRepository.findByProviderIdAndProviderUserIdIn(providerId, providerUserIds);for (UserSocialConnection userSocialConnection : userSocialConnectionList){localUserIds.add(userSocialConnection.getUserId());}return localUserIds;}public ConnectionRepository createConnectionRepository(String userId) {if (userId == null) {throw new IllegalArgumentException('userId cannot be null');}return new MongoConnectionRepository(userId, userSocialConnectionRepository, socialAuthenticationServiceLocator, textEncryptor);}}MongoUsersConnectionRepository非常類似于JdbcUsersConnectionRepository 。 但是對(duì)于MongoConnectionRepository ,我需要進(jìn)行一些更改:
public class MongoConnectionRepository implements ConnectionRepository {private final String userId;private final UserSocialConnectionRepository userSocialConnectionRepository;private final SocialAuthenticationServiceLocator socialAuthenticationServiceLocator;private final TextEncryptor textEncryptor;public MongoConnectionRepository(String userId, UserSocialConnectionRepository userSocialConnectionRepository,SocialAuthenticationServiceLocator socialAuthenticationServiceLocator, TextEncryptor textEncryptor) {this.userId = userId;this.userSocialConnectionRepository = userSocialConnectionRepository;this.socialAuthenticationServiceLocator = socialAuthenticationServiceLocator;this.textEncryptor = textEncryptor;}public MultiValueMap<String, Connection<?>> findAllConnections() {List<UserSocialConnection> userSocialConnectionList = this.userSocialConnectionRepository.findByUserId(userId);MultiValueMap<String, Connection<?>> connections = new LinkedMultiValueMap<String, Connection<?>>();Set<String> registeredProviderIds = socialAuthenticationServiceLocator.registeredProviderIds();for (String registeredProviderId : registeredProviderIds) {connections.put(registeredProviderId, Collections.<Connection<?>> emptyList());}for (UserSocialConnection userSocialConnection : userSocialConnectionList) {String providerId = userSocialConnection.getProviderId();if (connections.get(providerId).size() == 0) {connections.put(providerId, new LinkedList<Connection<?>>());}connections.add(providerId, buildConnection(userSocialConnection));}return connections;}public List<Connection<?>> findConnections(String providerId) {List<Connection<?>> resultList = new LinkedList<Connection<?>>();List<UserSocialConnection> userSocialConnectionList = this.userSocialConnectionRepository.findByUserIdAndProviderId(userId, providerId);for (UserSocialConnection userSocialConnection : userSocialConnectionList) {resultList.add(buildConnection(userSocialConnection));}return resultList;}@SuppressWarnings('unchecked')public <A> List<Connection<A>> findConnections(Class<A> apiType) {List<?> connections = findConnections(getProviderId(apiType));return (List<Connection<A>>) connections;}public MultiValueMap<String, Connection<?>> findConnectionsToUsers(MultiValueMap<String, String> providerUsers) {if (providerUsers == null || providerUsers.isEmpty()) {throw new IllegalArgumentException('Unable to execute find: no providerUsers provided');}MultiValueMap<String, Connection<?>> connectionsForUsers = new LinkedMultiValueMap<String, Connection<?>>();for (Iterator<Entry<String, List<String>>> it = providerUsers.entrySet().iterator(); it.hasNext();) {Entry<String, List<String>> entry = it.next();String providerId = entry.getKey();List<String> providerUserIds = entry.getValue();List<UserSocialConnection> userSocialConnections = this.userSocialConnectionRepository.findByProviderIdAndProviderUserIdIn(providerId, providerUserIds);List<Connection<?>> connections = new ArrayList<Connection<?>>(providerUserIds.size());for (int i = 0; i < providerUserIds.size(); i++) {connections.add(null);}connectionsForUsers.put(providerId, connections);for (UserSocialConnection userSocialConnection : userSocialConnections) {String providerUserId = userSocialConnection.getProviderUserId();int connectionIndex = providerUserIds.indexOf(providerUserId);connections.set(connectionIndex, buildConnection(userSocialConnection));}}return connectionsForUsers;}public Connection<?> getConnection(ConnectionKey connectionKey) {UserSocialConnection userSocialConnection = this.userSocialConnectionRepository.findByUserIdAndProviderIdAndProviderUserId(userId, connectionKey.getProviderId(),connectionKey.getProviderUserId());if (userSocialConnection != null) {return buildConnection(userSocialConnection);}throw new NoSuchConnectionException(connectionKey);}@SuppressWarnings('unchecked')public <A> Connection<A> getConnection(Class<A> apiType, String providerUserId) {String providerId = getProviderId(apiType);return (Connection<A>) getConnection(new ConnectionKey(providerId, providerUserId));}@SuppressWarnings('unchecked')public <A> Connection<A> getPrimaryConnection(Class<A> apiType) {String providerId = getProviderId(apiType);Connection<A> connection = (Connection<A>) findPrimaryConnection(providerId);if (connection == null) {throw new NotConnectedException(providerId);}return connection;}@SuppressWarnings('unchecked')public <A> Connection<A> findPrimaryConnection(Class<A> apiType) {String providerId = getProviderId(apiType);return (Connection<A>) findPrimaryConnection(providerId);}public void addConnection(Connection<?> connection) {//check cardinalitySocialAuthenticationService<?> socialAuthenticationService = this.socialAuthenticationServiceLocator.getAuthenticationService(connection.getKey().getProviderId());if (socialAuthenticationService.getConnectionCardinality() == ConnectionCardinality.ONE_TO_ONE ||socialAuthenticationService.getConnectionCardinality() == ConnectionCardinality.ONE_TO_MANY){List<UserSocialConnection> storedConnections = this.userSocialConnectionRepository.findByProviderIdAndProviderUserId(connection.getKey().getProviderId(), connection.getKey().getProviderUserId());if (storedConnections.size() > 0){//not allow one providerId connect to multiple userIdthrow new DuplicateConnectionException(connection.getKey());}}UserSocialConnection userSocialConnection = this.userSocialConnectionRepository.findByUserIdAndProviderIdAndProviderUserId(userId, connection.getKey().getProviderId(), connection.getKey().getProviderUserId());if (userSocialConnection == null) {ConnectionData data = connection.createData();userSocialConnection = new UserSocialConnection(userId, data.getProviderId(), data.getProviderUserId(), 0,data.getDisplayName(), data.getProfileUrl(), data.getImageUrl(), encrypt(data.getAccessToken()),encrypt(data.getSecret()), encrypt(data.getRefreshToken()), data.getExpireTime());this.userSocialConnectionRepository.save(userSocialConnection);} else {throw new DuplicateConnectionException(connection.getKey());}}public void updateConnection(Connection<?> connection) {ConnectionData data = connection.createData();UserSocialConnection userSocialConnection = this.userSocialConnectionRepository.findByUserIdAndProviderIdAndProviderUserId(userId, connection.getKey().getProviderId(), connection.getKey().getProviderUserId());if (userSocialConnection != null) {userSocialConnection.setDisplayName(data.getDisplayName());userSocialConnection.setProfileUrl(data.getProfileUrl());userSocialConnection.setImageUrl(data.getImageUrl());userSocialConnection.setAccessToken(encrypt(data.getAccessToken()));userSocialConnection.setSecret(encrypt(data.getSecret()));userSocialConnection.setRefreshToken(encrypt(data.getRefreshToken()));userSocialConnection.setExpireTime(data.getExpireTime());this.userSocialConnectionRepository.save(userSocialConnection);}}public void removeConnections(String providerId) {List<UserSocialConnection> userSocialConnectionList = this.userSocialConnectionRepository.findByUserIdAndProviderId(userId, providerId);for (UserSocialConnection userSocialConnection : userSocialConnectionList) {this.userSocialConnectionRepository.delete(userSocialConnection);}}public void removeConnection(ConnectionKey connectionKey) {UserSocialConnection userSocialConnection = this.userSocialConnectionRepository.findByUserIdAndProviderIdAndProviderUserId(userId, connectionKey.getProviderId(), connectionKey.getProviderUserId());this.userSocialConnectionRepository.delete(userSocialConnection);}// internal helpersprivate Connection<?> buildConnection(UserSocialConnection userSocialConnection) {ConnectionData connectionData = new ConnectionData(userSocialConnection.getProviderId(),userSocialConnection.getProviderUserId(), userSocialConnection.getDisplayName(),userSocialConnection.getProfileUrl(), userSocialConnection.getImageUrl(),decrypt(userSocialConnection.getAccessToken()), decrypt(userSocialConnection.getSecret()),decrypt(userSocialConnection.getRefreshToken()), userSocialConnection.getExpireTime());ConnectionFactory<?> connectionFactory = this.socialAuthenticationServiceLocator.getConnectionFactory(connectionData.getProviderId());return connectionFactory.createConnection(connectionData);}private Connection<?> findPrimaryConnection(String providerId) {List<UserSocialConnection> userSocialConnectionList = this.userSocialConnectionRepository.findByUserIdAndProviderId(userId, providerId);return buildConnection(userSocialConnectionList.get(0));}private <A> String getProviderId(Class<A> apiType) {return socialAuthenticationServiceLocator.getConnectionFactory(apiType).getProviderId();}private String encrypt(String text) {return text != null ? textEncryptor.encrypt(text) : text;}private String decrypt(String encryptedText) {return encryptedText != null ? textEncryptor.decrypt(encryptedText) : encryptedText;}}首先,我將JdbcTemplate替換為UserSocialConnectionRepository以從數(shù)據(jù)庫(kù)中檢索UserSocialConnection對(duì)象。 然后用spring-social-security模塊中的SocialAuthenticationServiceLocator替換ConnectionFactoryLocator 。 最大的變化是addConnection方法(上面已突出顯示),它首先檢查連接基數(shù)。 如果connectionCardinality的socialAuthenticationService是ONE_TO_ONE (這意味著一個(gè)用戶id與一個(gè)且僅一個(gè)對(duì)providerId / providerUserId的),或ONE_TO_MANY (這意味著一個(gè)用戶id可以連接到一個(gè)或多個(gè)providerId / providerUserId,但一對(duì)providerId / providerUserId的只能連接到一個(gè)userId)。
完成所有這些自定義之后,最后一步是在spring config中將它們粘合在一起:
@Configuration public class SocialAndSecurityConfig {@Injectprivate Environment environment;@InjectAccountService accountService;@Injectprivate AuthenticationManager authenticationManager;@Injectprivate UserSocialConnectionRepository userSocialConnectionRepository;@Beanpublic SocialAuthenticationServiceLocator socialAuthenticationServiceLocator() {SocialAuthenticationServiceRegistry registry = new SocialAuthenticationServiceRegistry();//add googleOAuth2ConnectionFactory<Google> googleConnectionFactory = new GoogleConnectionFactory(environment.getProperty('google.clientId'),environment.getProperty('google.clientSecret'));OAuth2AuthenticationService<Google> googleAuthenticationService = new OAuth2AuthenticationService<Google>(googleConnectionFactory);googleAuthenticationService.setScope('https://www.googleapis.com/auth/userinfo.profile');registry.addAuthenticationService(googleAuthenticationService);//add twitterOAuth1ConnectionFactory<Twitter> twitterConnectionFactory = new TwitterConnectionFactory(environment.getProperty('twitter.consumerKey'),environment.getProperty('twitter.consumerSecret'));OAuth1AuthenticationService<Twitter> twitterAuthenticationService = new OAuth1AuthenticationService<Twitter>(twitterConnectionFactory);registry.addAuthenticationService(twitterAuthenticationService);//add facebookOAuth2ConnectionFactory<Facebook> facebookConnectionFactory = new FacebookConnectionFactory(environment.getProperty('facebook.clientId'),environment.getProperty('facebook.clientSecret'));OAuth2AuthenticationService<Facebook> facebookAuthenticationService = new OAuth2AuthenticationService<Facebook>(facebookConnectionFactory);facebookAuthenticationService.setScope('');registry.addAuthenticationService(facebookAuthenticationService);return registry;}/*** Singleton data access object providing access to connections across all users.*/@Beanpublic UsersConnectionRepository usersConnectionRepository() {MongoUsersConnectionRepository repository = new MongoUsersConnectionRepository(userSocialConnectionRepository,socialAuthenticationServiceLocator(), Encryptors.noOpText());repository.setConnectionSignUp(autoConnectionSignUp());return repository;}/*** Request-scoped data access object providing access to the current user's connections.*/@Bean@Scope(value = 'request', proxyMode = ScopedProxyMode.INTERFACES)public ConnectionRepository connectionRepository() {UserAccount user = AccountUtils.getLoginUserAccount();return usersConnectionRepository().createConnectionRepository(user.getUsername());}/*** A proxy to a request-scoped object representing the current user's primary Google account.* * @throws NotConnectedException* if the user is not connected to Google.*/@Bean@Scope(value = 'request', proxyMode = ScopedProxyMode.INTERFACES)public Google google() {Connection<Google> google = connectionRepository().findPrimaryConnection(Google.class);return google != null ? google.getApi() : new GoogleTemplate();}@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@Scope(value='request', proxyMode=ScopedProxyMode.INTERFACES) public Twitter twitter() {Connection<Twitter> twitter = connectionRepository().findPrimaryConnection(Twitter.class);return twitter != null ? twitter.getApi() : new TwitterTemplate();}@Beanpublic ConnectionSignUp autoConnectionSignUp() {return new AutoConnectionSignUp(accountService);}@Beanpublic SocialAuthenticationFilter socialAuthenticationFilter() {SocialAuthenticationFilter filter = new SocialAuthenticationFilter(authenticationManager, accountService,usersConnectionRepository(), socialAuthenticationServiceLocator());filter.setFilterProcessesUrl('/signin');filter.setSignupUrl(null); filter.setConnectionAddedRedirectUrl('/myAccount');filter.setPostLoginUrl('/myAccount');return filter;}@Beanpublic SocialAuthenticationProvider socialAuthenticationProvider(){return new SocialAuthenticationProvider(usersConnectionRepository(), accountService);}@Beanpublic LoginUrlAuthenticationEntryPoint socialAuthenticationEntryPoint(){return new LoginUrlAuthenticationEntryPoint('/signin');}}accountService是我自己的用戶帳戶服務(wù),用于提供與帳戶相關(guān)的功能,它實(shí)現(xiàn)了SocialUserDetailsService , UserDetailsService , UserIdExtractor 。
 還有很多地方需要改進(jìn),例如重構(gòu)MongoConnectionRepository和MongoUsersConnectionRepository以使用Spring Data Repository接口實(shí)現(xiàn)抽象的社交連接存儲(chǔ)庫(kù)實(shí)現(xiàn)。 而且我發(fā)現(xiàn)有人已經(jīng)對(duì)此提出了一個(gè)問(wèn)題: 利用Spring Data for UsersConnectionRepository 。 
參考:來(lái)自我們的JCG合作伙伴 Yuan Ji在Jiwhiz博客上為MongoDB定制Spring Social Connect Framework 。
翻譯自: https://www.javacodegeeks.com/2013/03/customize-spring-social-connect-framework-for-mongodb.html
總結(jié)
以上是生活随笔為你收集整理的为MongoDB定制Spring Social Connect框架的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: VISTA如何系统还原(WindowsV
- 下一篇: 玩游戏电脑配置清单(游戏型组装电脑配置清
