org.springframework.social.connect.ConnectionData Java Examples

The following examples show how to use org.springframework.social.connect.ConnectionData. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: IndexController.java    From pre with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 保存完信息然后跳转到绑定用户信息页面
 *
 * @param request
 * @param response
 * @throws IOException
 */
@GetMapping("/socialSignUp")
public void socialSignUp(HttpServletRequest request, HttpServletResponse response) throws IOException {
    String uuid = UUID.randomUUID().toString();
    SocialUserInfo userInfo = new SocialUserInfo();
    Connection<?> connectionFromSession = providerSignInUtils.getConnectionFromSession(new ServletWebRequest(request));
    userInfo.setHeadImg(connectionFromSession.getImageUrl());
    userInfo.setNickname(connectionFromSession.getDisplayName());
    userInfo.setProviderId(connectionFromSession.getKey().getProviderId());
    userInfo.setProviderUserId(connectionFromSession.getKey().getProviderUserId());
    ConnectionData data = connectionFromSession.createData();
    PreConnectionData preConnectionData = new PreConnectionData();
    BeanUtil.copyProperties(data, preConnectionData);
    socialRedisHelper.saveConnectionData(uuid, preConnectionData);
    // 跳转到用户绑定页面
    response.sendRedirect(url + "/bind?key=" + uuid);
}
 
Example #2
Source File: MongoConnectionRepositoryImpl.java    From JiwhizBlogWeb with Apache License 2.0 6 votes vote down vote up
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);
    }
}
 
Example #3
Source File: UserAccountServiceImpl.java    From JiwhizBlogWeb with Apache License 2.0 6 votes vote down vote up
@Override
public UserAccount createUserAccount(ConnectionData data, UserProfile profile) {
    long userIdSequence = this.counterService.getNextUserIdSequence();
    
    UserRoleType[] roles = (userIdSequence == 1l) ? 
            new UserRoleType[] { UserRoleType.ROLE_USER, UserRoleType.ROLE_AUTHOR, UserRoleType.ROLE_ADMIN } :
            new UserRoleType[] { UserRoleType.ROLE_USER };
    UserAccount account = new UserAccount(USER_ID_PREFIX + userIdSequence, roles);
    account.setEmail(profile.getEmail());
    account.setDisplayName(data.getDisplayName());
    account.setImageUrl(data.getImageUrl());
    if (userIdSequence == 1l) {
        account.setTrustedAccount(true);
    }
    LOGGER.info(String.format("A new user is created (userId='%s') for '%s' with email '%s'.", account.getUserId(),
            account.getDisplayName(), account.getEmail()));
    return this.accountRepository.save(account);
}
 
Example #4
Source File: JpaConnectionRepository.java    From computoser with GNU Affero General Public License v3.0 5 votes vote down vote up
private Connection<?> authToConnection(SocialAuthentication auth) {
    ConnectionFactory<?> connectionFactory = locator.getConnectionFactory(auth.getProviderId());
    ConnectionData data = new ConnectionData(auth.getProviderId(), auth.getProviderUserId(), null, null,
            auth.getImageUrl(), auth.getToken(), auth.getSecret(), auth.getRefreshToken(),
            auth.getExpirationTime());
    return connectionFactory.createConnection(data);
}
 
Example #5
Source File: AutoConnectionSignUp.java    From JiwhizBlogWeb with Apache License 2.0 5 votes vote down vote up
public String execute(Connection<?> connection) {
    ConnectionData data = connection.createData();
    UserAccount account = this.userAccountService.createUserAccount(data, connection.fetchUserProfile());
    
    if (logger.isDebugEnabled()) {
        logger.debug("Automatically create a new user account '"+account.getUserId()+"', for "+account.getDisplayName());
        logger.debug("connection data is from provider '"+data.getProviderId()+"', providerUserId is '"+data.getProviderUserId());
    }
    
    messageSender.sendNewUserRegistered(account);
    return account.getUserId();
}
 
Example #6
Source File: MongoConnectionRepositoryImpl.java    From JiwhizBlogWeb with Apache License 2.0 5 votes vote down vote up
private 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);
}
 
Example #7
Source File: MongoConnectionRepositoryImpl.java    From JiwhizBlogWeb with Apache License 2.0 5 votes vote down vote up
public void addConnection(Connection<?> connection) {
    //check cardinality
    SocialAuthenticationService<?> 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/providerUserId connect to multiple userId
            throw 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());
    }
}
 
Example #8
Source File: SpringSocialAuthenticationFilter.java    From AIDR with GNU Affero General Public License v3.0 5 votes vote down vote up
private void updateUserKeys(ConnectionData connectionData, String userId){
    List<UserConnection> userConnections = userConnectionService.getByUserIdAndProviderUserId(userId, connectionData.getProviderUserId());
    if(!CollectionUtils.isFull(userConnections)){
       for(UserConnection uc : userConnections){
           uc.setAccessToken(connectionData.getAccessToken());
           uc.setSecret(connectionData.getSecret());
           userConnectionService.update(uc);
       }
    }
}
 
Example #9
Source File: SpringSocialAuthenticationFilter.java    From AIDR with GNU Affero General Public License v3.0 5 votes vote down vote up
private void updateUserKeys(ConnectionData connectionData, String userId){
    List<UserConnection> userConnections = userConnectionService.getByProviderUserIdAndUserId(connectionData.getProviderUserId(), userId);
    if(!CollectionUtils.isFull(userConnections)){
       for(UserConnection uc : userConnections){
           uc.setAccessToken(connectionData.getAccessToken());
           uc.setSecret(connectionData.getSecret());
           userConnectionService.update(uc);
       }
    }
}
 
Example #10
Source File: JpaConnectionRepository.java    From computoser with GNU Affero General Public License v3.0 5 votes vote down vote up
public static SocialAuthentication connectionToAuth(Connection<?> connection) {
    SocialAuthentication auth = new SocialAuthentication();
    ConnectionData data = connection.createData();
    auth.setProviderId(data.getProviderId());
    auth.setToken(data.getAccessToken());
    auth.setRefreshToken(data.getRefreshToken());
    auth.setSecret(data.getSecret());
    auth.setProviderUserId(data.getProviderUserId());
    return auth;
}
 
Example #11
Source File: AppSingUpUtils.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
/**
 * 将缓存的社交网站用户信息与系统注册用户信息绑定
 *
 * @param request the request
 * @param userId  the user id
 */
public void doPostSignUp(WebRequest request, String userId) {
	String key = getKey(request);
	if (!redisTemplate.hasKey(key)) {
		throw new AppSecretException("无法找到缓存的用户社交账号信息");
	}
	ConnectionData connectionData = (ConnectionData) redisTemplate.opsForValue().get(key);
	Connection<?> connection = connectionFactoryLocator.getConnectionFactory(connectionData.getProviderId())
			.createConnection(connectionData);
	usersConnectionRepository.createConnectionRepository(userId).addConnection(connection);

	redisTemplate.delete(key);
}
 
Example #12
Source File: WechatConnectionFactory.java    From cola with MIT License 4 votes vote down vote up
@Override
public Connection<Wechat> createConnection(ConnectionData data) {
    return new OAuth2Connection<>(data, getOAuth2ServiceProvider(), getApiAdapter(data.getProviderUserId()));
}
 
Example #13
Source File: ConfigAwareConnectionFactoryLocatorTest.java    From engine with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Connection<Facebook> createConnection(ConnectionData data) {
    return null;
}
 
Example #14
Source File: WeiXinConnectionFactory.java    From FEBS-Security with Apache License 2.0 4 votes vote down vote up
@Override
public Connection<WeiXin> createConnection(ConnectionData data) {
    return new OAuth2Connection<>(data, getOAuth2ServiceProvider(), getApiAdapter(data.getProviderUserId()));
}
 
Example #15
Source File: WeixinConnectionFactory.java    From pre with GNU General Public License v3.0 4 votes vote down vote up
@Override
public Connection<Weixin> createConnection(ConnectionData data) {
    return new OAuth2Connection<Weixin>(data, getOAuth2ServiceProvider(), getApiAdapter(data.getProviderUserId()));
}
 
Example #16
Source File: WechatMpConnectionFactory.java    From cola with MIT License 4 votes vote down vote up
@Override
public Connection<WechatMp> createConnection(ConnectionData data) {
    return new OAuth2Connection<>(data, getOAuth2ServiceProvider(), getApiAdapter(data.getProviderUserId()));
}
 
Example #17
Source File: WeixinConnectionFactory.java    From paascloud-master with Apache License 2.0 2 votes vote down vote up
/**
 * Create connection connection.
 *
 * @param data the data
 *
 * @return the connection
 */
@Override
public Connection<Weixin> createConnection(ConnectionData data) {
	return new OAuth2Connection<>(data, getOAuth2ServiceProvider(), getApiAdapter(data.getProviderUserId()));
}
 
Example #18
Source File: AppSingUpUtils.java    From paascloud-master with Apache License 2.0 2 votes vote down vote up
/**
 * 缓存社交网站用户信息到redis
 *
 * @param request        the request
 * @param connectionData the connection data
 */
public void saveConnectionData(WebRequest request, ConnectionData connectionData) {
	redisTemplate.opsForValue().set(getKey(request), connectionData, 10, TimeUnit.MINUTES);
}
 
Example #19
Source File: UserAccountService.java    From JiwhizBlogWeb with Apache License 2.0 2 votes vote down vote up
/**
 * Creates a new UserAccount with user social network account Connection Data and UserProfile.
 * Default has ROLE_USER role.
 * 
 * @param data
 * @param profile
 * @return
 */
UserAccount createUserAccount(ConnectionData data, UserProfile profile);