支付宝授权

master
胡锐 2 weeks ago
parent 02373d1178
commit 973571eb35
  1. 16
      bweb/src/main/java/com/hfkj/controller/BsUserAuthController.java
  2. 91
      cweb/src/main/java/com/hfkj/controller/UserAuthController.java
  3. 12
      service/src/main/java/com/hfkj/common/alipay/AlipayUtils.java
  4. 9
      service/src/main/java/com/hfkj/service/user/BsUserPlatformAuthorizeService.java
  5. 13
      service/src/main/java/com/hfkj/service/user/impl/BsUserPlatformAuthorizeServiceImpl.java

@ -57,19 +57,19 @@ public class BsUserAuthController {
try {
if (body == null || StringUtils.isBlank(body.getString("code"))) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}/*
}
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
request.setGrantType("authorization_code");
request.setCode(body.getString("code"));
AlipaySystemOauthTokenResponse response = AlipayUtils.initClient().certificateExecute(request);
if(response.isSuccess()){
return ResponseMsgUtil.success(response);
}*/
AlipayUserInfoShareRequest request = new AlipayUserInfoShareRequest();
AlipayUserInfoShareResponse response = AlipayUtils.initClient().certificateExecute(request,body.getString("code"));
AlipaySystemOauthTokenResponse response = AlipayUtils.initClient().execute(request);
if(response.isSuccess()) {
return ResponseMsgUtil.success(response);
AlipayUserInfoShareRequest alipayUserInfoShareRequest = new AlipayUserInfoShareRequest();
AlipayUserInfoShareResponse alipayUserInfoShareResponse = AlipayUtils.initClient().execute(alipayUserInfoShareRequest,response.getAccessToken());
if(alipayUserInfoShareResponse.isSuccess()){
return ResponseMsgUtil.success(alipayUserInfoShareResponse);
}
}
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "获取失败");
} catch (Exception e) {

@ -0,0 +1,91 @@
package com.hfkj.controller;
import com.alibaba.fastjson.JSONObject;
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 com.hfkj.common.alipay.AlipayUtils;
import com.hfkj.common.exception.ErrorCode;
import com.hfkj.common.exception.ErrorHelp;
import com.hfkj.common.exception.SysCode;
import com.hfkj.common.security.UserCenter;
import com.hfkj.common.utils.ResponseMsgUtil;
import com.hfkj.entity.BsUserPlatformAuthorize;
import com.hfkj.model.ResponseData;
import com.hfkj.model.SecUserSessionObject;
import com.hfkj.model.UserSessionObject;
import com.hfkj.service.user.BsUserGradeService;
import com.hfkj.service.user.BsUserParentRelService;
import com.hfkj.service.user.BsUserPlatformAuthorizeService;
import com.hfkj.service.user.BsUserService;
import com.hfkj.sysenum.user.UserAuthorizePlatformEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.annotation.Resource;
/**
* @className: CmsController
* @author: HuRui
* @date: 2024/9/24
**/
@Controller
@RequestMapping(value = "/userAuth")
@Api(value = "用户授权管理")
public class UserAuthController {
private static Logger log = LoggerFactory.getLogger(UserAuthController.class);
@Resource
private BsUserPlatformAuthorizeService userPlatformAuthorizeService;
@Resource
private UserCenter userCenter;
@RequestMapping(value="/alipay",method = RequestMethod.POST)
@ResponseBody
@ApiOperation(value = "支付宝授权")
public ResponseData alipay(@RequestBody JSONObject body) {
try {
UserSessionObject session = userCenter.getSessionModel(UserSessionObject.class);
if (body == null || StringUtils.isBlank(body.getString("code"))) {
throw ErrorHelp.genException(SysCode.System, ErrorCode.REQ_PARAMS_ERROR, "");
}
AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
request.setGrantType("authorization_code");
request.setCode(body.getString("code"));
AlipaySystemOauthTokenResponse response = AlipayUtils.initClient().execute(request);
if(response.isSuccess()) {
AlipayUserInfoShareRequest alipayUserInfoShareRequest = new AlipayUserInfoShareRequest();
AlipayUserInfoShareResponse alipayUserInfoShareResponse = AlipayUtils.initClient().execute(alipayUserInfoShareRequest,response.getAccessToken());
if(alipayUserInfoShareResponse.isSuccess()) {
// 授权信息
BsUserPlatformAuthorize userPlatformAuthorize = userPlatformAuthorizeService.getUserAuth(session.getUser().getId(), UserAuthorizePlatformEnum.type1);
if (userPlatformAuthorize == null) {
userPlatformAuthorize = new BsUserPlatformAuthorize();
}
userPlatformAuthorize.setUserId(session.getUser().getId());
userPlatformAuthorize.setPlatformCode(UserAuthorizePlatformEnum.type1.getType());
userPlatformAuthorize.setPlatformName(UserAuthorizePlatformEnum.type1.getName());
userPlatformAuthorize.setOpenId(alipayUserInfoShareResponse.getOpenId());
userPlatformAuthorize.setAvatar(alipayUserInfoShareResponse.getAvatar());
userPlatformAuthorize.setNickName(alipayUserInfoShareResponse.getNickName());
userPlatformAuthorizeService.edit(userPlatformAuthorize);
return ResponseMsgUtil.success(userPlatformAuthorize);
}
}
throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "授权失败");
} catch (Exception e) {
return ResponseMsgUtil.exception(e);
}
}
}

@ -14,9 +14,9 @@ public class AlipayUtils {
private final static String serverUrl = "https://openapi.alipay.com/gateway.do";
private final static String appId = "2021004176645906";
private final static String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCWaLQdwl6E5QVo2RUWquJ1Et4oLRsbfLULQyYT3o6Fs+2ZWOHjjMf6Jg5yVuM3rPa1GNB4+6cBU84gaLQ8WZl9tdQAOhQlnvjOsJA0twb+Lq1Luaa+cOOgp3t3+PuD0nRP9vuPAW3aqrFyjTeDx0vOAzulrWWwS+zLWkUmv96Db5MjlR8gK/pLOszQsk+K3uu06KoyLrCn0K/AIrhJClcEvy/umDfC/P8PnYEgTppNgZ/IBkKp5P9JZzyYEgR7rgD/Z8aWKQ5a8bO0VxC/7F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gkaw56KW3/djINzcPaoECZQouC8hxYnQ/KVmGTIStx6Voh+nRF7NhAoGAa2KOWIWkElc3q8E7DP7bJ19fEkn1XUJZ2wjgX3DuFTXAgSkRws2dwM+3PryyqUKdrHMZR5xCU+0636SWb7vBLAp8W5nSc8eghgMYyRVd6ssaBcA6FyPzCloBOeZ6NgJaWF+UoafBIhSsFu9KMt0LEHUm3/rjnj82DV43M0fL7hs=";
private final static String APP_CERT_PATH = "/home/project/youtao/cert/alipay/appCertPublicKey_2021004176645906.crt";
private final static String ALIPAY_CERT_PATH = "/home/project/youtao/cert/alipay/alipayCertPublicKey_RSA2.crt";
private final static String ALIPAY_ROOT_CERT_PATH = "/home/project/youtao/cert/alipay/alipayRootCert.crt";
private final static String APP_CERT = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAlmi0HcJehOUFaNkVFqridRLeKC0bG3y1C0MmE96OhbPtmVjh44zH+iYOclbjN6z2tRjQePunAVPOIGi0PFmZfbXUADoUJZ74zrCQNLcG/i6tS7mmvnDjoKd7d/j7g9J0T/b7jwFt2qqxco03g8dLzgM7pa1lsEvsy1pFJr/eg2+TI5UfICv6SzrM0LJPit7rtOiqMi6wp9CvwCK4SQpXBL8v7pg3wvz/D52BIE6aTYGfyAZCqeT/SWc8mBIEe64A/2fGlikOWvGztFcQv+xQ6QnFjUT3cHBxDfANJbaDuTDbpXmc9qvYa1utu6okDRKGg8qm4fzzRV1ywiuZwVs1jwIDAQAB";
private final static String ALIPAY_CERT = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAxHvgDmHdKEU9BOiQ6ZYTyW1tn0xzuZ/PnT9/yhyDnhpaWdKJWr4+4oelRU028FMKPffXQ4yWX7XF5wcdpKqBH9vQSvI4GSz3CjTBGV3aadfMPzc/Inb2Fq4BgBYwyGeTVaMblIyl7wE37oRRMwlBjTgafZGUL078Ll8eZgn2ziRfvtBwzHMTEvhy/jNiC6raW3oFhy2ltnhFCob7z2HEecMMX9RGRdS8aHi+LaYNBktj4JSSElIbz7S+L1AYwyNuxP5i6xjc9kBrOdIWcNg/IGMWWL7bHW/RngSR6D2pzTLZ+sShQoX2M6DIYQsIPzdhkYymWQTQMBMgSTx5lfs4GwIDAQAB";
private final static String ALIPAY_ROOT_CERT = "/home/project/youtao/cert/alipay/alipayRootCert.crt";
/**
* 初始化客户端请求
@ -31,11 +31,11 @@ public class AlipayUtils {
// 设置应用私钥
alipayConfig.setPrivateKey(APP_PRIVATE_KEY);
// 设置应用公钥证书路径
alipayConfig.setAppCertPath(APP_CERT_PATH);
alipayConfig.setAppCertContent(APP_CERT);
// 设置支付宝公钥证书路径
alipayConfig.setAlipayPublicCertPath(ALIPAY_CERT_PATH);
// alipayConfig.setAlipayPublicCertContent(ALIPAY_CERT);
// 设置支付宝根证书路径
alipayConfig.setRootCertPath(ALIPAY_ROOT_CERT_PATH);
alipayConfig.setRootCertPath(ALIPAY_ROOT_CERT);
// 设置请求格式,固定值json
alipayConfig.setFormat("json");
// 设置字符集

@ -1,6 +1,7 @@
package com.hfkj.service.user;
import com.hfkj.entity.BsUserPlatformAuthorize;
import com.hfkj.sysenum.user.UserAuthorizePlatformEnum;
import java.util.List;
@ -23,4 +24,12 @@ public interface BsUserPlatformAuthorizeService {
* @return
*/
List<BsUserPlatformAuthorize> getUserAuth(Long userId);
/**
* 查询用户授权
* @param userId
* @param platform
* @return
*/
BsUserPlatformAuthorize getUserAuth(Long userId, UserAuthorizePlatformEnum platform);
}

@ -4,6 +4,7 @@ import com.hfkj.dao.BsUserPlatformAuthorizeMapper;
import com.hfkj.entity.BsUserPlatformAuthorize;
import com.hfkj.entity.BsUserPlatformAuthorizeExample;
import com.hfkj.service.user.BsUserPlatformAuthorizeService;
import com.hfkj.sysenum.user.UserAuthorizePlatformEnum;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
@ -38,4 +39,16 @@ public class BsUserPlatformAuthorizeServiceImpl implements BsUserPlatformAuthori
example.createCriteria().andUserIdEqualTo(userId).andStatusNotEqualTo(0);
return userPlatformAuthorizeMapper.selectByExample(example);
}
@Override
public BsUserPlatformAuthorize getUserAuth(Long userId, UserAuthorizePlatformEnum platform) {
BsUserPlatformAuthorizeExample example = new BsUserPlatformAuthorizeExample();
example.createCriteria().andUserIdEqualTo(userId).andPlatformCodeEqualTo(platform.getType()).andStatusNotEqualTo(0);
List<BsUserPlatformAuthorize> list = userPlatformAuthorizeMapper.selectByExample(example);
if (!list.isEmpty()) {
return list.get(0);
} else {
return null;
}
}
}

Loading…
Cancel
Save