AuthAlipayRequest.java 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
package me.zhyd.oauth.request;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import me.zhyd.oauth.config.AuthConfig;
11
import me.zhyd.oauth.config.AuthSource;
12
import me.zhyd.oauth.exception.AuthException;
13
import me.zhyd.oauth.model.AuthToken;
14 15 16
import me.zhyd.oauth.model.AuthUser;
import me.zhyd.oauth.model.AuthUserGender;
import me.zhyd.oauth.utils.StringUtils;
17
import me.zhyd.oauth.utils.UrlBuilder;
18 19

/**
20 21
 * 支付宝登录
 *
22 23 24 25 26 27 28 29 30 31
 * @author yadong.zhang (yadong.zhang0415(a)gmail.com)
 * @version 1.0
 * @since 1.8
 */
public class AuthAlipayRequest extends BaseAuthRequest {

    private AlipayClient alipayClient;

    public AuthAlipayRequest(AuthConfig config) {
        super(config, AuthSource.ALIPAY);
32 33
        this.alipayClient = new DefaultAlipayClient(AuthSource.ALIPAY.accessToken(), config.getClientId(), config.getClientSecret(), "json", "UTF-8", config
                .getAlipayPublicKey(), "RSA2");
34 35 36
    }

    @Override
37
    protected AuthToken getAccessToken(String code) {
38 39 40 41 42 43 44 45 46 47 48 49
        AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
        request.setGrantType("authorization_code");
        request.setCode(code);
        AlipaySystemOauthTokenResponse response = null;
        try {
            response = this.alipayClient.execute(request);
        } catch (Exception e) {
            throw new AuthException("Unable to get token from alipay using code [" + code + "]", e);
        }
        if (!response.isSuccess()) {
            throw new AuthException(response.getSubMsg());
        }
50 51
        return AuthToken.builder()
                .accessToken(response.getAccessToken())
智布道's avatar
智布道 已提交
52 53 54
                .uid(response.getUserId())
                .expireIn(Integer.parseInt(response.getExpiresIn()))
                .refreshToken(response.getRefreshToken())
55
                .build();
56 57 58
    }

    @Override
59 60
    protected AuthUser getUserInfo(AuthToken authToken) {
        String accessToken = authToken.getAccessToken();
61 62 63 64 65 66 67 68 69 70
        AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
        AlipayUserInfoShareResponse response = null;
        try {
            response = this.alipayClient.execute(request, accessToken);
        } catch (AlipayApiException e) {
            throw new AuthException(e.getErrMsg(), e);
        }
        if (!response.isSuccess()) {
            throw new AuthException(response.getSubMsg());
        }
智布道's avatar
智布道 已提交
71

72 73
        String province = response.getProvince(),
                city = response.getCity();
智布道's avatar
智布道 已提交
74 75
        String location = String.format("%s %s", StringUtils.isEmpty(province) ? "" : province, StringUtils.isEmpty(city) ? "" : city);

76
        return AuthUser.builder()
智布道's avatar
智布道 已提交
77
                .uuid(response.getUserId())
78
                .username(StringUtils.isEmpty(response.getUserName()) ? response.getNickName() : response.getUserName())
79 80
                .nickname(response.getNickName())
                .avatar(response.getAvatar())
智布道's avatar
智布道 已提交
81
                .location(location)
82
                .gender(AuthUserGender.getRealGender(response.getGender()))
83
                .token(authToken)
84 85 86
                .source(AuthSource.ALIPAY)
                .build();
    }
87 88 89 90 91 92 93 94 95 96

    /**
     * 返回认证url,可自行跳转页面
     *
     * @return 返回授权地址
     */
    @Override
    public String authorize() {
        return UrlBuilder.getAlipayAuthorizeUrl(config.getClientId(), config.getRedirectUri());
    }
97
}