All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.healthy.common.security.social.weixinmp.connet.WeixinMpOAuth2ConnectionFactory Maven / Gradle / Ivy

There is a newer version: 1.2.1.RELEASE
Show newest version
package com.healthy.common.security.social.weixinmp.connet;

import com.healthy.common.security.social.weixinmp.api.WeixinMp;
import org.springframework.social.connect.ApiAdapter;
import org.springframework.social.connect.Connection;
import org.springframework.social.connect.ConnectionData;
import org.springframework.social.connect.support.OAuth2Connection;
import org.springframework.social.connect.support.OAuth2ConnectionFactory;
import org.springframework.social.oauth2.AccessGrant;
import org.springframework.social.oauth2.OAuth2ServiceProvider;

/**
 * 微信公众号连接工厂
 */
public class WeixinMpOAuth2ConnectionFactory extends OAuth2ConnectionFactory {

    /**
     * @param appId
     * @param appSecret
     */
    public WeixinMpOAuth2ConnectionFactory(String providerId, String appId, String appSecret) {
        super(providerId, new WeixinMpOAuth2ServiceProvider(appId, appSecret), new WeixinMpApiAdapter());
    }

    /**
     * 由于微信的openId是和accessToken一起返回的,所以在这里直接根据accessToken设置providerUserId即可,不用像QQ那样通过QQAdapter来获取
     */
    @Override
    protected String extractProviderUserId(AccessGrant accessGrant) {
        if (accessGrant instanceof WeixinMpAccessGrant) {
            return ((WeixinMpAccessGrant) accessGrant).getOpenId();
        }
        return null;
    }

    @Override
    public Connection createConnection(AccessGrant accessGrant) {
        return new OAuth2Connection(getProviderId(), extractProviderUserId(accessGrant), accessGrant.getAccessToken(),
                accessGrant.getRefreshToken(), accessGrant.getExpireTime(), getOAuth2ServiceProvider(), getApiAdapter(extractProviderUserId(accessGrant)));
    }

    @Override
    public Connection createConnection(ConnectionData data) {
        return new OAuth2Connection(data, getOAuth2ServiceProvider(), getApiAdapter(data.getProviderUserId()));
    }

    private ApiAdapter getApiAdapter(String providerUserId) {
        return new WeixinMpApiAdapter(providerUserId);
    }

    private OAuth2ServiceProvider getOAuth2ServiceProvider() {
        return (OAuth2ServiceProvider) getServiceProvider();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy