google第三方登陆服务器校验

本文介绍了一个用于处理Google登录请求的Java类。该类实现了LoginHandler接口,并通过验证ID令牌来获取用户的唯一标识符(sub),用于后续的账户管理和第三方绑定操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import java.util.*;

@Component
public class GoogleLogin implements LoginHandler {
    public GoogleLogin() {
        LoginHandlerFactory.addHandler(AccountConstant.LoginTypeEnum.ByGoogle, this);
    }



    private static String verifyUrl = "https://www.googleapis.com/oauth2/v3/tokeninfo";
    private final Logger log = LoggerFactory.getLogger(this.getClass());
    private static final String DefaultDecode = "UTF-8";

    @Autowired
    private AccountManager accountManager;
    @Autowired
    private UserForbiddenManager userForbiddenManager;
    @Autowired
    private SmsService smsService;
    @Autowired
    private AppThirdBindManager thirdBindManager;


    @Override
    public AccountThirdBO process(LoginRequest req) {
        if (StringUtils.isEmpty(req.getIdToken())) {
            throw new BusinessException(ResultEnum.PARA_ERR);
        }
        String provider = AccountConstant.ProviderEnum.google.toString();
        AppThirdBindCfgBO bindCfgBO = thirdBindManager.getLoginAppThirdBindCfgBO(req.getAppPackageName(), DeviceTypeEnum.getDeviceTypeEnum(req.getDeviceType()), LoginTypeEnum.Google);
        if (bindCfgBO == null || StringUtils.isEmpty(bindCfgBO.getThirdAppId())) {
            throw new BusinessException(ResultEnum.CONFIG_PARAM_UNEXIST);
        }

        String thirdId = getGoogleTokenUnionId(req.getIdToken(), bindCfgBO.getThirdAppId());
        AccountIndex accountIndex = accountManager.getAccountIndex(provider, thirdId);
        boolean unregistered = accountIndex == null;
        if (!unregistered) {
            accountManager.checkPhoneValidation(accountIndex.getUid());
        }
        List<ThirdInfo> thirdInfoList = new ArrayList<>();
        thirdInfoList.add(new ThirdInfo(provider, thirdId, thirdId, ""));

        return new AccountThirdBO(thirdInfoList,
                accountIndex == null ? null : accountIndex.getUid(),
                unregistered,
                false,
                unregistered);
    }


    private String getGoogleTokenUnionId(String idToken, String clientId) {
        //判断idToken是否为空或者null
        if (StringUtils.isEmpty(idToken)) {
            throw new BusinessException(ResultEnum.PARA_ERR);
        }
        Map<String, String> paramMap = new HashMap<>();
        paramMap.put("id_token", idToken);
        String result = HttpInvokeUtil.httpPost(verifyUrl, paramMap,DefaultDecode);
        log.info("checkGoogleToken idToken:{} result:{}", idToken, result);
        if (StringUtils.isBlank(result)) {
            throw new BusinessException(ResultEnum.PARA_ERR);
        }
        JSONObject jsonObject = JSON.parseObject(result);
        String dataKey = "aud";
        if (jsonObject.containsKey(dataKey)) {
            String aud = jsonObject.getString(dataKey);
            if (clientId.equals(aud)) {
                return jsonObject.get("sub").toString();
            }
        }
        throw new BusinessException(ResultEnum.PARA_ERR);
    }


}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值