From 973571eb35d755bbe169b5635305fc1bf31c9329 Mon Sep 17 00:00:00 2001 From: hu177768073 <177768073@qq.com> Date: Fri, 8 Nov 2024 15:56:00 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=AF=E4=BB=98=E5=AE=9D=E6=8E=88=E6=9D=83?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../hfkj/controller/BsUserAuthController.java | 18 ++-- .../hfkj/controller/UserAuthController.java | 91 +++++++++++++++++++ .../com/hfkj/common/alipay/AlipayUtils.java | 12 +-- .../user/BsUserPlatformAuthorizeService.java | 9 ++ .../BsUserPlatformAuthorizeServiceImpl.java | 13 +++ 5 files changed, 128 insertions(+), 15 deletions(-) create mode 100644 cweb/src/main/java/com/hfkj/controller/UserAuthController.java diff --git a/bweb/src/main/java/com/hfkj/controller/BsUserAuthController.java b/bweb/src/main/java/com/hfkj/controller/BsUserAuthController.java index 8eb1ea4..0d0df85 100644 --- a/bweb/src/main/java/com/hfkj/controller/BsUserAuthController.java +++ b/bweb/src/main/java/com/hfkj/controller/BsUserAuthController.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")); - if(response.isSuccess()){ - return ResponseMsgUtil.success(response); + AlipaySystemOauthTokenResponse response = AlipayUtils.initClient().execute(request); + if(response.isSuccess()) { + 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) { diff --git a/cweb/src/main/java/com/hfkj/controller/UserAuthController.java b/cweb/src/main/java/com/hfkj/controller/UserAuthController.java new file mode 100644 index 0000000..ed372d0 --- /dev/null +++ b/cweb/src/main/java/com/hfkj/controller/UserAuthController.java @@ -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); + } + } + +} diff --git a/service/src/main/java/com/hfkj/common/alipay/AlipayUtils.java b/service/src/main/java/com/hfkj/common/alipay/AlipayUtils.java index 1bcdcd2..c5ff091 100644 --- a/service/src/main/java/com/hfkj/common/alipay/AlipayUtils.java +++ b/service/src/main/java/com/hfkj/common/alipay/AlipayUtils.java @@ -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/7FDpCcWNRPdwcHEN8A0ltoO5MNuleZz2q9hrW627qiQNEoaDyqbh/PNFXXLCK5nBWzWPAgMBAAECggEAHC9ouT7oKW0lU12CpynEn/22Jb82GjOHVaHyq7yD5qgt+RN/2P1TqBujj2ea0p8V6B/LSTCXPhdvRAF67Og5bCY7oQNBLT+aDlll3IsDw3QJw2v5xCwxa7SW3YeJ0k0IwMdeopC4/kYIVJgD9CzPlr5iWgyhRqUjrYOv+6/uyaA+XLJjF/eUG0UEnkyuCPW6ClX+ulpI6Tk7ycB9HJFGpLNoYBUSs7mgiUOOKI9AA9S5aDG2QYBxkUK8WF4gtYt6SLKAWw5GSMCeK5zjhRZw0GqIIouIUDSigdK5VzKth/OXiA9B6uXh21fS1GQPhcfo4ZbKml6p8JB9pvQRPIXpiQKBgQD3u4ph5RMv4lvUvk/q6f1fyATcRlaCGiiPrUj8IFq4vjxIHfB+nb0YTHkEHClWsGbc4I/l2UQE+wzR89ZcFE2h+v4c7dswMW/QPX9TyFZQIRs2nvAbPKMdg40zdLDiUj3dknSrWx9OqnTXghhK8O2Ah1qNLdCVPD9eFehfTADsIwKBgQCbbbI8G7X2RWkiJtPItrtWWhOJ4BEQG9bBendeirvZmfQilpzeC7UWdVintGzK+xSGDwYqDWtyIWeT/wu2VdIl2yTJ/2HfRDD4E0jnbed32g9bcRReOCjnwuSGZUp845nZKilxuBHD0tXS0pi/6VCje2myaZVmXnLlOE57FkuhpQKBgGJl9GZD5eYcI9uRqA6n2EMmIIAZ1ByjJT9EVfwHIeHFdg4zDiZMoyI2pc6zHNxY/tJ2w9FJBhJwYTw3fQpf6iIPnsWA2JIA4Oe2tY9iwJ3dOIDuinJXGHcNnJU2oVeT0QzkMkEp1XqajARZoSqLHdryaE4xR2svXgAR9ZV8i9U/AoGBAI67tios6HU4WMvcDDEOXgt2vOqosgKxDg6vgF951/iEwQXiejwPVEVDjh60OhRNbxONSIPlvv4YXx4x4XeYaFwLW0WFGUQHQ1ENpK2i6CXQQroepi3ANRBgkaw56KW3/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"); // 设置字符集 diff --git a/service/src/main/java/com/hfkj/service/user/BsUserPlatformAuthorizeService.java b/service/src/main/java/com/hfkj/service/user/BsUserPlatformAuthorizeService.java index 3b1eb2c..517484e 100644 --- a/service/src/main/java/com/hfkj/service/user/BsUserPlatformAuthorizeService.java +++ b/service/src/main/java/com/hfkj/service/user/BsUserPlatformAuthorizeService.java @@ -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 getUserAuth(Long userId); + + /** + * 查询用户授权 + * @param userId + * @param platform + * @return + */ + BsUserPlatformAuthorize getUserAuth(Long userId, UserAuthorizePlatformEnum platform); } diff --git a/service/src/main/java/com/hfkj/service/user/impl/BsUserPlatformAuthorizeServiceImpl.java b/service/src/main/java/com/hfkj/service/user/impl/BsUserPlatformAuthorizeServiceImpl.java index 77fd8e6..60880f1 100644 --- a/service/src/main/java/com/hfkj/service/user/impl/BsUserPlatformAuthorizeServiceImpl.java +++ b/service/src/main/java/com/hfkj/service/user/impl/BsUserPlatformAuthorizeServiceImpl.java @@ -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 list = userPlatformAuthorizeMapper.selectByExample(example); + if (!list.isEmpty()) { + return list.get(0); + } else { + return null; + } + } }