Browse Source

新增:接入山东通的sso代码

dev
wangxianzhang 3 years ago
parent
commit
f872354f63
  1. 14
      epmet-auth/src/main/java/com/epmet/controller/ThirdLoginController.java
  2. 9
      epmet-auth/src/main/java/com/epmet/service/ThirdLoginService.java
  3. 52
      epmet-auth/src/main/java/com/epmet/service/impl/ThirdLoginServiceImpl.java
  4. 11
      epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java
  5. 10
      epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/feign/ThirdOpenFeignClient.java
  6. 28
      epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtBaseResult.java
  7. 12
      epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtGetAccessTokenResult.java
  8. 17
      epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtStaffDetailResult.java
  9. 17
      epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtStaffInfoResult.java
  10. 47
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/SdtController.java
  11. 16
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/SdtAppInfoDao.java
  12. 54
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/entity/SdtAppInfoEntity.java
  13. 35
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/SdtService.java
  14. 156
      epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/SdtServiceImpl.java
  15. 19
      epmet-module/epmet-third/epmet-third-server/src/main/resources/db/migration/V0.0.19__sdt_create_corp_info.sql
  16. 22
      epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/SdtAppInfoDao.xml

14
epmet-auth/src/main/java/com/epmet/controller/ThirdLoginController.java

@ -228,4 +228,18 @@ public class ThirdLoginController {
return new Result<UserTokenResultDTO>().ok(thirdLoginService.yanTaiSSOLogin(authCode));
}
/**
* 山东通sso登录
* @author wxz
* @date 2022/11/3 10:19
* * @param authCode
* * @return Result<UserTokenResultDTO>
*/
@PostMapping("sso-govlogin-sdt/{authCode}")
public Result<UserTokenResultDTO> sdtSSOLogin(@PathVariable(value = "authCode") String authCode) {
// 烟台客户id,暂且写死。
String customerId = "1535072605621841922";
return new Result<UserTokenResultDTO>().ok(thirdLoginService.sdtSSOLogin(authCode, customerId));
}
}

9
epmet-auth/src/main/java/com/epmet/service/ThirdLoginService.java

@ -124,4 +124,13 @@ public interface ThirdLoginService {
* @return
*/
UserTokenResultDTO yanTaiSSOLogin(String authCode);
/**
* 山东通sso登录
* @author wxz
* @date 2022/11/3 10:20
* * @param authCode
* * @return UserTokenResultDTO
*/
UserTokenResultDTO sdtSSOLogin(String authCode, String customerId);
}

52
epmet-auth/src/main/java/com/epmet/service/impl/ThirdLoginServiceImpl.java

@ -41,6 +41,8 @@ import com.epmet.feign.*;
import com.epmet.jwt.JwtTokenProperties;
import com.epmet.jwt.JwtTokenUtils;
import com.epmet.redis.CaptchaRedis;
import com.epmet.sdt.SdtStaffDetailResult;
import com.epmet.sdt.SdtStaffInfoResult;
import com.epmet.service.GovWebService;
import com.epmet.service.ThirdLoginService;
import com.taobao.api.ApiException;
@ -1168,4 +1170,54 @@ public class ThirdLoginServiceImpl implements ThirdLoginService, ResultDataResol
}
return customerId;
}
@Override
public UserTokenResultDTO sdtSSOLogin(String authCode, String customerId) {
String accessToken = getResultDataOrThrowsException(thirdOpenFeignClient.getSdtCachedAccessToken(customerId), ServiceConstant.EPMET_THIRD_SERVER,
EpmetErrorCode.SERVER_ERROR.getCode(), "获取山东通access_token失败", null);
// 1.获取用户编码
HashMap<String, Object> params = new HashMap<>();
params.put("access_token", accessToken);
params.put("code", authCode);
Result<String> sdtResult = HttpClientManager.getInstance().sendGet("/cgi-bin/user/getuserinfo", params);
if (!sdtResult.success()) {
// http状态判断
log.error("【山东通登录】查询用户编码-http错误:{}", sdtResult.getMsg());
throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), "调用山东通查询用户编码失败", null);
}
SdtStaffInfoResult staffInfoResult = JSON.parseObject(sdtResult.getData(), SdtStaffInfoResult.class);
if (!staffInfoResult.success()) {
// 山东通业务返回状态判断
log.error("【山东通登录】查询用户编码失败,错误信息:{}", staffInfoResult.getErrmsg());
throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), "调用山东通查询用户编码失败", null);
}
// 2.获取用户详细信息
HashMap<String, Object> userDetailParams = new HashMap<>();
userDetailParams.put("access_token", accessToken);
userDetailParams.put("userid", staffInfoResult.getUserId());
Result<String> userDetailResult = HttpClientManager.getInstance().sendGet("/cgi-bin/user/get", userDetailParams);
if (!userDetailResult.success()) {
// http状态判断
log.error("【山东通登录】查询用户详细信息-http错误:{}", sdtResult.getMsg());
throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), "调用山东通查询用户详细信息失败", null);
}
SdtStaffDetailResult staffDetailResult = JSON.parseObject(userDetailResult.getData(), SdtStaffDetailResult.class);
if (!staffDetailResult.success()) {
// 山东通业务返回状态判断
log.error("【山东通登录】查询用户详细信息失败,错误信息:{}", staffInfoResult.getErrmsg());
throw new EpmetException(EpmetErrorCode.SERVER_ERROR.getCode(), "调用山东通查询用户详细信息失败", null);
}
// 3.有了手机号,可以做登录了
String mobile = staffDetailResult.getMobile();
GovWebLoginFormDTO loginGovParam = new GovWebLoginFormDTO();
loginGovParam.setCustomerId(customerId);
loginGovParam.setPhone(mobile);
return govWebService.loginByThirdPlatform(loginGovParam);
}
}

11
epmet-commons/epmet-commons-tools/src/main/java/com/epmet/commons/tools/redis/RedisKeys.java

@ -922,4 +922,15 @@ public class RedisKeys {
public static String getOperExamineResourceUrls() {
return rootPrefix.concat("oper:access:examineresources");
}
/**
* 山东通的Accesstoken缓存
* @author wxz
* @date 2022/11/2 22:59
* * @param customerId
* * @return String
*/
public static String getSdtAccessToken(String customerId) {
return rootPrefix.concat("thirdplatform:sdt:accesstoken:").concat(customerId);
};
}

10
epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/feign/ThirdOpenFeignClient.java

@ -6,6 +6,7 @@ import com.epmet.dto.form.*;
import com.epmet.dto.result.ThirdplatformResultDTO;
import com.epmet.feign.fallback.ThirdOpenFeignClientFallbackFactory;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
@ -89,4 +90,13 @@ public interface ThirdOpenFeignClient {
@PostMapping("/third/dataSync/yanTai/sync/user")
Result<Boolean> getYanTaiUserInfo(@RequestParam("orgId") String orgId);
/**
* 查询山东通AccesToken
* @author wxz
* @date 2022/11/3 10:12
*
* * @return Result<String>
*/
@GetMapping("/third/sdt/getCachedAccessToken")
Result<String> getSdtCachedAccessToken(@RequestParam("customerId") String customerId);
}

28
epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtBaseResult.java

@ -0,0 +1,28 @@
package com.epmet.sdt;
import lombok.Data;
/**
* 山东通返回结果封装
*/
@Data
public class SdtBaseResult {
/**
* 成功状态码
*/
public static final Integer SUCCESS_CODE = 0;
private Integer errcode;
private String errmsg;
/**
* 请求是否成功
* @author wxz
* @date 2022/11/2 22:43
* @return boolean
*/
public boolean success() {
return (errcode != null && errcode == SUCCESS_CODE) ? true : false;
}
}

12
epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtGetAccessTokenResult.java

@ -0,0 +1,12 @@
package com.epmet.sdt;
import lombok.Data;
/**
* 获取山东通AccessToken接口的返回Result
*/
@Data
public class SdtGetAccessTokenResult extends SdtBaseResult {
private String access_token;
private Long expires_in;
}

17
epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtStaffDetailResult.java

@ -0,0 +1,17 @@
package com.epmet.sdt;
import lombok.Data;
/**
* 返回了挺多属性但是我只取手机号
* @ClassName SdtStaffInfoResult
* @Description TODO
* @Author wangxianzhang
* @Date 2022/11/3 10:58
*/
@Data
public class SdtStaffDetailResult extends SdtBaseResult {
private String userid;
private String name;
private String mobile;
}

17
epmet-module/epmet-third/epmet-third-client/src/main/java/com/epmet/sdt/SdtStaffInfoResult.java

@ -0,0 +1,17 @@
package com.epmet.sdt;
import lombok.Data;
/**
* @ClassName SdtStaffInfoResult
* @Description TODO
* @Author wangxianzhang
* @Date 2022/11/3 10:58
*/
@Data
public class SdtStaffInfoResult extends SdtBaseResult {
private String UserId;
private String DeviceId;
private String user_ticket;
private String expires_in;
}

47
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/controller/SdtController.java

@ -0,0 +1,47 @@
package com.epmet.controller;
import com.epmet.commons.tools.utils.EpmetRequestHolder;
import com.epmet.commons.tools.utils.Result;
import com.epmet.service.SdtService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
/**
* 山东通controller
*/
@RestController
@RequestMapping("sdt")
public class SdtController {
@Autowired
private SdtService sdtService;
/**
* 刷新山东通token
* @author wxz
* @date 2022/11/2 22:11
* @return Result
*/
@GetMapping("refreshAccessToken")
public Result refreshAccessToken() {
sdtService.refreshAccessToken(null);
return new Result();
}
/**
* 查询山东通AccesToken
* @author wxz
* @date 2022/11/3 10:12
*
* * @return Result<String>
*/
@GetMapping("getCachedAccessToken")
public Result<String> getCachedAccessToken(@RequestParam("customerId") String customerId) {
String cachedAccessToken = sdtService.getCachedAccessToken(EpmetRequestHolder.getLoginUserCustomerId());
return new Result<String>().ok(cachedAccessToken);
}
}

16
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/dao/SdtAppInfoDao.java

@ -0,0 +1,16 @@
package com.epmet.dao;
import com.epmet.commons.mybatis.dao.BaseDao;
import com.epmet.entity.SdtAppInfoEntity;
import org.apache.ibatis.annotations.Mapper;
/**
* 接入山东通app信息
*
* @author generator generator@elink-cn.com
* @since v1.0.0 2022-11-02
*/
@Mapper
public interface SdtAppInfoDao extends BaseDao<SdtAppInfoEntity> {
}

54
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/entity/SdtAppInfoEntity.java

@ -0,0 +1,54 @@
package com.epmet.entity;
import com.baomidou.mybatisplus.annotation.TableName;
import com.epmet.commons.mybatis.entity.BaseEpmetEntity;
import lombok.Data;
import lombok.EqualsAndHashCode;
import java.util.Date;
/**
* 接入山东通app信息
*
* @author generator generator@elink-cn.com
* @since v1.0.0 2022-11-02
*/
@Data
@EqualsAndHashCode(callSuper=false)
@TableName("sdt_app_info")
public class SdtAppInfoEntity extends BaseEpmetEntity {
private static final long serialVersionUID = 1L;
/**
* 客户ID
*/
private String customerId;
/**
* 组织ID
*/
private String corpId;
/**
* 秘钥
*/
private String corpSecret;
/**
* api地址
*/
private String apiAddress;
/**
* 接口调用凭证
*/
private String accessToken;
/**
* 何时过期
*/
private Date expiresAt;
}

35
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/SdtService.java

@ -0,0 +1,35 @@
package com.epmet.service;
import com.zaxxer.hikari.util.FastList;
import lombok.Data;
/**
* @ClassName SdtService
* @Description 山东通相关service
* @Author wangxianzhang
* @Date 2022/11/2 22:02
*/
public interface SdtService {
/**
* 获取accessToken的url地址
* /cgi-bin/gettoken?corpid=id&corpsecret=secrect
*/
String API_GET_ACCESS_TOKEN_URL = "/cgi-bin/gettoken";
/**
* 刷新token
* @author wxz
* @date 2022/11/2 22:50
*/
void refreshAccessToken(String customerId);
/**
* 获取缓存的AccessToken
* @author wxz
* @date 2022/11/2 22:56
*
* * @return String
*/
String getCachedAccessToken(String customerId);
}

156
epmet-module/epmet-third/epmet-third-server/src/main/java/com/epmet/service/impl/SdtServiceImpl.java

@ -0,0 +1,156 @@
package com.epmet.service.impl;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.epmet.commons.tools.redis.RedisKeys;
import com.epmet.commons.tools.redis.RedisUtils;
import com.epmet.commons.tools.utils.HttpClientManager;
import com.epmet.commons.tools.utils.Result;
import com.epmet.dao.SdtAppInfoDao;
import com.epmet.entity.SdtAppInfoEntity;
import com.epmet.sdt.SdtGetAccessTokenResult;
import com.epmet.service.SdtService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.*;
/**
* @ClassName SdtServiceImpl
* @Description 山东通相关service
* @Author wangxianzhang
* @Date 2022/11/2 22:03
*/
@Service
@Slf4j
public class SdtServiceImpl implements SdtService {
@Autowired
private SdtAppInfoDao sdtAppInfoDao;
@Autowired
private RedisUtils redisUtils;
/**
* 刷新accessToken
* @author wxz
* @date 2022/11/2 22:53
*/
@Override
public void refreshAccessToken(String customerId) {
List<SdtAppInfoEntity> appInfos;
if (StringUtils.isBlank(customerId)) {
// 没传递customerId,刷新所有
appInfos = sdtAppInfoDao.selectList(new QueryWrapper<>());
} else {
// 传递了customerId,只刷新该客户的
LambdaQueryWrapper<SdtAppInfoEntity> query = new LambdaQueryWrapper<>();
query.eq(SdtAppInfoEntity::getCustomerId, customerId);
appInfos = sdtAppInfoDao.selectList(query);
}
appInfos.stream().forEach(app -> {
Date now = new Date();
String apiAddress = app.getApiAddress();
String customerIdInner = app.getCustomerId();
HashMap<String, Object> params = new HashMap<>();
params.put("corpid", app.getCorpId());
params.put("corpsecret", app.getCorpSecret());
Result<String> stringResult = HttpClientManager.getInstance().sendGet(apiAddress + API_GET_ACCESS_TOKEN_URL, params);
if (!stringResult.success()) {
// 请求有问题
log.error("【山东通】刷新accessToken出现异常:{}", stringResult.getMsg());
return;
}
SdtGetAccessTokenResult sdtResult = JSON.parseObject(stringResult.getMsg(), SdtGetAccessTokenResult.class);
if (!sdtResult.success()) {
log.error("【山东通】刷新accessToken失败:{}", sdtResult.getErrmsg());
return;
}
String access_token = sdtResult.getAccess_token();
Long expires_in = sdtResult.getExpires_in();
SdtAppInfoEntity updateEntity = new SdtAppInfoEntity();
updateEntity.setId(app.getId());
updateEntity.setAccessToken(access_token);
updateEntity.setExpiresAt(getExpireAt(now, expires_in));
sdtAppInfoDao.updateById(updateEntity);
// token刷新了以后,要刷新缓存中的token,这样下次才能获取到最新的token
String key = RedisKeys.getOpenApiAccessTokenKey(customerIdInner);
redisUtils.set(key, access_token, expires_in);
});
}
/**
* 计算过期时间
* @author wxz
* @date 2022/11/2 23:22
* * @param startTime
* @param expires_in
* * @return Date
*/
private Date getExpireAt(Date startTime, Long expires_in) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(startTime);
calendar.add(Calendar.SECOND, expires_in.intValue());
return calendar.getTime();
}
/**
* 还有多少秒过期
* @author wxz
* @date 2022/11/3 10:08
* * @param expireAt
* * @return Long
*/
private Long getExpireIn(Date expireAt) {
return expireAt.getTime() - System.currentTimeMillis();
}
/**
* 获取缓存的AccessToken
* @author wxz
* @date 2022/11/2 22:56
*
* * @return String
*/
@Override
public String getCachedAccessToken(String customerId) {
String key = RedisKeys.getOpenApiAccessTokenKey(customerId);
String accessToken = redisUtils.getString(key);
if (StringUtils.isNotBlank(accessToken)) {
return accessToken;
}
// db中查询
LambdaQueryWrapper<SdtAppInfoEntity> query = new LambdaQueryWrapper<>();
query.eq(SdtAppInfoEntity::getCustomerId, customerId);
SdtAppInfoEntity sdtAppInfoEntity = sdtAppInfoDao.selectOne(query);
if (sdtAppInfoEntity == null) {
log.error("【查询山东通AccessToken】失败,原因:该客户id没有配置山东通相关信息");
return null;
}
accessToken = sdtAppInfoEntity.getAccessToken();
Date expiresAt = sdtAppInfoEntity.getExpiresAt();
if (StringUtils.isNotBlank(accessToken)) {
redisUtils.set(key, accessToken, getExpireIn(expiresAt));
return accessToken;
} else {
// 配置了appInfo,但是没有accessToken,刷新token,刷新缓存,然后重新从缓存中取
refreshAccessToken(customerId);
return redisUtils.getString(key);
}
}
}

19
epmet-module/epmet-third/epmet-third-server/src/main/resources/db/migration/V0.0.19__sdt_create_corp_info.sql

@ -0,0 +1,19 @@
create table sdt_app_info
(
`ID` varchar(64) NOT NULL COMMENT '主键',
`CUSTOMER_ID` varchar(64) NOT NULL COMMENT '客户ID',
`CORPID` varchar(32) not null comment '组织ID',
`CORPSECRET` varchar(64) not null comment '秘钥',
`ACCESS_TOKEN` varchar(512) not null comment '接口调用凭证',
`API_ADDRESS` varchar(128) not null comment 'api地址',
`EXPIRES_AT` datetime not null comment '何时过期',
`DEL_FLAG` int(11) NOT NULL COMMENT '删除标识:0.未删除 1.已删除',
`REVISION` int(11) NOT NULL COMMENT '乐观锁',
`CREATED_BY` varchar(64) NOT NULL COMMENT '创建人',
`CREATED_TIME` datetime NOT NULL COMMENT '创建时间',
`UPDATED_BY` varchar(64) NOT NULL COMMENT '更新人',
`UPDATED_TIME` datetime NOT NULL COMMENT '更新时间',
PRIMARY KEY (`ID`)
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4 COMMENT ='接入山东通app信息';

22
epmet-module/epmet-third/epmet-third-server/src/main/resources/mapper/SdtAppInfoDao.xml

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.epmet.dao.SdtAppInfoDao">
<resultMap type="com.epmet.entity.SdtAppInfoEntity" id="sdtAppInfoMap">
<result property="id" column="ID"/>
<result property="customerId" column="CUSTOMER_ID"/>
<result property="corpId" column="CORP_ID"/>
<result property="corpSecret" column="CORP_SECRET"/>
<result property="accessToken" column="ACCESS_TOKEN"/>
<result property="expiresAt" column="EXPIRES_AT"/>
<result property="delFlag" column="DEL_FLAG"/>
<result property="revision" column="REVISION"/>
<result property="createdBy" column="CREATED_BY"/>
<result property="createdTime" column="CREATED_TIME"/>
<result property="updatedBy" column="UPDATED_BY"/>
<result property="updatedTime" column="UPDATED_TIME"/>
</resultMap>
</mapper>
Loading…
Cancel
Save