同步基本框架处理完毕
This commit is contained in:
@@ -86,11 +86,6 @@ MAIL_POP_PORT=110
|
|||||||
#MAIL_USERNAME=64459@Any3.com
|
#MAIL_USERNAME=64459@Any3.com
|
||||||
#MAIL_PASSWORD=65316975Xiaoq
|
#MAIL_PASSWORD=65316975Xiaoq
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
IPANDPORT=http://localhost:7061/
|
IPANDPORT=http://localhost:7061/
|
||||||
|
|
||||||
#redis
|
#redis
|
||||||
@@ -133,10 +128,8 @@ huawei.OBS.fileMaxSize=10 #文件上传最大M数
|
|||||||
|
|
||||||
logging.level.com.adc: debug
|
logging.level.com.adc: debug
|
||||||
|
|
||||||
|
# ==== IAM????
|
||||||
# ======= ddm权限接口 集成 =========
|
iam.url=https://iamuat.faw-vw.com/
|
||||||
ddm-identity=changan_test
|
iam.appkey=xxxx
|
||||||
ddm-company=9
|
iam.appSecret=xxx
|
||||||
ddm-ras-public-key=
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -170,6 +170,11 @@
|
|||||||
<version>1.4.7</version>
|
<version>1.4.7</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.auth0</groupId>
|
||||||
|
<artifactId>java-jwt</artifactId>
|
||||||
|
<version>3.10.2</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,230 @@
|
|||||||
package com.adc.da.sync.service;
|
package com.adc.da.sync.service;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.IdUtil;
|
||||||
|
import cn.hutool.http.Header;
|
||||||
|
import cn.hutool.http.HttpRequest;
|
||||||
|
import com.adc.da.sync.service.dao.mysql.SyncIAMLogDao;
|
||||||
|
import com.adc.da.sync.service.entity.SyncIAMLogEO;
|
||||||
|
import com.adc.da.sync.service.enums.SyncTypeEnum;
|
||||||
|
import com.adc.da.sys.dao.mysql.OrgEODao;
|
||||||
|
import com.adc.da.sys.entity.OrgEO;
|
||||||
|
import com.adc.da.sys.entity.UserEO;
|
||||||
|
import com.adc.da.sys.service.OrgEOService;
|
||||||
|
import com.adc.da.sys.service.iservice.IUserEoService;
|
||||||
|
import com.adc.da.sys.vo.iam.OrgVoIAM;
|
||||||
|
import com.adc.da.sys.vo.iam.UserVoIAM;
|
||||||
|
import com.adc.da.util.utils.GsonUtil;
|
||||||
|
import com.auth0.jwt.JWT;
|
||||||
|
import com.auth0.jwt.algorithms.Algorithm;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
|
import javax.annotation.Resource;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
@Service
|
||||||
public class SyncIAMTimer {
|
public class SyncIAMTimer {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* IAM的前缀地址
|
||||||
|
*/
|
||||||
|
@Value("${iam.url}")
|
||||||
|
private String iamUrl;
|
||||||
|
|
||||||
|
@Value("${iam.appkey}")
|
||||||
|
private String appkey;
|
||||||
|
|
||||||
|
@Value("${iam.appSecret}")
|
||||||
|
private String appSecret;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 组织机构全量同步接口地址
|
||||||
|
*/
|
||||||
|
public static final String ORG_FULL_URL = "/esc-idm/api/v1/org/listAll";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 组织机构增量同步接口地址
|
||||||
|
*/
|
||||||
|
public static final String ORG_INCREMENT_URL = "/esc-idm/api/v1/org/list";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户全量同步接口地址
|
||||||
|
*/
|
||||||
|
public static final String USER_FULL_URL = "/esc-idm/api/v1/account/listAll";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 用户增量同步接口地址
|
||||||
|
*/
|
||||||
|
public static final String USER_INCREMENT_URL = "/esc-idm/api/v1/account/list";
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
SyncIAMLogDao syncIAMLogDao;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
OrgEOService orgEOService;
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
IUserEoService iUserEoService;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同步组织机构-全量数据并入库
|
||||||
|
* @Param syncModel 同步模式
|
||||||
|
*/
|
||||||
|
public void syncOrgFullData(){
|
||||||
|
String syncUrl = this.iamUrl + ORG_FULL_URL;
|
||||||
|
String jwtToken = this.getJwtToken(appkey, appSecret);
|
||||||
|
|
||||||
|
Date syncStartTime = new Date();
|
||||||
|
|
||||||
|
String orgDataStr = HttpRequest.post(syncUrl)
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.header("Authorization", jwtToken)//头信息,多个头信息多次调用此方法即可
|
||||||
|
.timeout(10 * 60 * 1000) //超时,毫秒
|
||||||
|
.execute().body();
|
||||||
|
|
||||||
|
List<OrgVoIAM> orgVoIAMList = GsonUtil.json2Collection(orgDataStr, OrgVoIAM.class);
|
||||||
|
|
||||||
|
// 同步的VO 转换成 数据库EO
|
||||||
|
List<OrgEO> orgEOList = new ArrayList<>();
|
||||||
|
for (OrgVoIAM orgVoIAM : orgVoIAMList){
|
||||||
|
OrgEO orgEO = this.transOrgIAMVoToOrgEO(orgVoIAM);
|
||||||
|
orgEOList.add(orgEO);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 需要处理 ,比较后再更新
|
||||||
|
this.orgEOService.saveBatch(orgEOList);
|
||||||
|
|
||||||
|
this.addSyncLog(null, syncStartTime, SyncTypeEnum.ORG_FULL_DATA.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同步组织机构-增量数据并入库
|
||||||
|
* @Param syncModel 同步模式
|
||||||
|
*/
|
||||||
|
public void syncOrgIncrData(){
|
||||||
|
String syncUrl = this.iamUrl + ORG_INCREMENT_URL;
|
||||||
|
String jwtToken = this.getJwtToken(appkey, appSecret);
|
||||||
|
|
||||||
|
Date syncStartTime = new Date();
|
||||||
|
|
||||||
|
Calendar c = Calendar.getInstance();
|
||||||
|
c.set(Calendar.HOUR_OF_DAY, (c.get(Calendar.HOUR_OF_DAY) - 24));//HOUR_OF_DAY 指一天中的小时
|
||||||
|
Long incrementStartTime = c.getTimeInMillis(); // 每天同步前一天的数据,需要当前时间往前诺一天
|
||||||
|
|
||||||
|
String orgDataStr = HttpRequest.post(syncUrl)
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.header("Authorization", jwtToken)//头信息,多个头信息多次调用此方法即可
|
||||||
|
.form("startTime", incrementStartTime)
|
||||||
|
.timeout(10 * 60 * 1000) //超时,毫秒
|
||||||
|
.execute().body();
|
||||||
|
|
||||||
|
List<OrgVoIAM> orgVoIAMList = GsonUtil.json2Collection(orgDataStr, OrgVoIAM.class);
|
||||||
|
|
||||||
|
// 同步的VO 转换成 数据库EO
|
||||||
|
List<OrgEO> orgEOList = new ArrayList<>();
|
||||||
|
for (OrgVoIAM orgVoIAM : orgVoIAMList){
|
||||||
|
OrgEO orgEO = this.transOrgIAMVoToOrgEO(orgVoIAM);
|
||||||
|
orgEOList.add(orgEO);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.orgEOService.saveBatch(orgEOList);
|
||||||
|
|
||||||
|
this.addSyncLog(null, syncStartTime, SyncTypeEnum.ORG_INCREMENT_DATA.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 同步用户-全量数据并入库
|
||||||
|
* @Param syncModel 同步模式
|
||||||
|
*/
|
||||||
|
public void syncUserFullData(){
|
||||||
|
String syncUrl = this.iamUrl + USER_FULL_URL;
|
||||||
|
String jwtToken = this.getJwtToken(appkey, appSecret);
|
||||||
|
|
||||||
|
Date syncStartTime = new Date();
|
||||||
|
|
||||||
|
String userDataStr = HttpRequest.post(syncUrl)
|
||||||
|
.header("Content-Type", "application/json")
|
||||||
|
.header("Authorization", jwtToken)//头信息,多个头信息多次调用此方法即可
|
||||||
|
.timeout(10 * 60 * 1000) //超时,毫秒
|
||||||
|
.execute().body();
|
||||||
|
|
||||||
|
List<UserVoIAM> userVoIAMList = GsonUtil.json2Collection(userDataStr, UserVoIAM.class);
|
||||||
|
|
||||||
|
// 同步的VO 转换成 数据库EO,并存库
|
||||||
|
for (UserVoIAM userVoIAM : userVoIAMList){
|
||||||
|
UserEO userEO = this.transUserIAMVoToUserEO(userVoIAM);
|
||||||
|
// 需要处理 ,比较后再更新
|
||||||
|
this.iUserEoService.save(userEO);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.addSyncLog(null, syncStartTime, SyncTypeEnum.ORG_FULL_DATA.getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 根据key和secret获取JWT_TOKEN
|
||||||
|
* @param appKey
|
||||||
|
* @param appSecret
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public String getJwtToken(String appKey, String appSecret) {
|
||||||
|
String token = JWT.create()
|
||||||
|
.withIssuer(appKey)
|
||||||
|
.withIssuedAt(new Date())
|
||||||
|
.withJWTId(IdUtil.fastSimpleUUID())
|
||||||
|
.sign(Algorithm.HMAC256(appSecret));
|
||||||
|
return String.join(" ", "Bearer", token);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 添加同步日志到库中
|
||||||
|
* @param incrementStartTime 增量同步时间,可能为空
|
||||||
|
*/
|
||||||
|
private void addSyncLog(String incrementStartTime, Date syncStartDate, String syncType) {
|
||||||
|
SyncIAMLogEO syncIAMLogEO = new SyncIAMLogEO();
|
||||||
|
syncIAMLogEO.setIncrementStartTime(incrementStartTime);
|
||||||
|
syncIAMLogEO.setSyncStartDate(syncStartDate);
|
||||||
|
syncIAMLogEO.setSyncEndDate(new Date());
|
||||||
|
syncIAMLogEO.setSyncType(syncType);
|
||||||
|
this.syncIAMLogDao.insert(syncIAMLogEO);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对 对接的组织机构实体 转换为 数据库的实体
|
||||||
|
* @param orgVoIAM
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public OrgEO transOrgIAMVoToOrgEO(OrgVoIAM orgVoIAM) {
|
||||||
|
OrgEO orgEO = new OrgEO();
|
||||||
|
orgEO.setOrgCode(orgVoIAM.getOrgCode());
|
||||||
|
orgEO.setOrgName(orgVoIAM.getOrgName());
|
||||||
|
orgEO.setOrgLevel(orgVoIAM.getOrgLevel());
|
||||||
|
orgEO.setSupOrgCode(orgVoIAM.getSupOrgCode());
|
||||||
|
orgEO.setOrgShortName(orgVoIAM.getOrgShortName());
|
||||||
|
orgEO.setAttribution(orgVoIAM.getAttribution());
|
||||||
|
orgEO.setBmdm14(orgVoIAM.getBmdm14());
|
||||||
|
orgEO.setStatus(orgVoIAM.getStatus());
|
||||||
|
orgEO.setUpdateTime(orgVoIAM.getUpdateTime());
|
||||||
|
|
||||||
|
return orgEO;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 对 对接的用户实体 转换为 数据库的实体
|
||||||
|
* @param userVoIAM
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public UserEO transUserIAMVoToUserEO(UserVoIAM userVoIAM) {
|
||||||
|
UserEO userEO = new UserEO();
|
||||||
|
|
||||||
|
// 转换基础数据
|
||||||
|
|
||||||
|
// 转换org的关联数据
|
||||||
|
|
||||||
|
return userEO;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,8 +4,10 @@ package com.adc.da.sync.service.enums;
|
|||||||
*/
|
*/
|
||||||
public enum SyncTypeEnum {
|
public enum SyncTypeEnum {
|
||||||
|
|
||||||
USER_DATA("USER_DATA","用户数据"),
|
USER_FULL_DATA("USER_FULL_DATA","用户数据-全量同步"),
|
||||||
ORG_DATA("ORG_DATA","组织机构数据");
|
USER_INCREMENT_DATA("USER_INCREMENT_DATA","用户数据-增量同步"),
|
||||||
|
ORG_FULL_DATA("ORG_FULL_DATA","组织机构数据-全量同步"),
|
||||||
|
ORG_INCREMENT_DATA("ORG_INCREMENT_DATA","组织机构数据-增量同步");
|
||||||
|
|
||||||
private String label;
|
private String label;
|
||||||
private String value;
|
private String value;
|
||||||
|
|||||||
@@ -0,0 +1,26 @@
|
|||||||
|
package com.adc.da.sys.constant;
|
||||||
|
/***
|
||||||
|
* 同步类型枚举
|
||||||
|
*/
|
||||||
|
public enum UserBelongEnum {
|
||||||
|
|
||||||
|
LOCAL("1","本地新增用户"),
|
||||||
|
IAM("0","IAm同步用户");
|
||||||
|
|
||||||
|
private String label;
|
||||||
|
private String value;
|
||||||
|
|
||||||
|
private UserBelongEnum(String value, String label) {
|
||||||
|
this.value = value;
|
||||||
|
this.label = label;
|
||||||
|
}
|
||||||
|
public String getValue() {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getLabel() {
|
||||||
|
return label;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
@@ -74,7 +74,7 @@ public class UserEOController extends BaseController<UserEO> {
|
|||||||
JsonElement element = parser.parse(data);
|
JsonElement element = parser.parse(data);
|
||||||
UserEO eo = gson.fromJson(element, UserEO.class);
|
UserEO eo = gson.fromJson(element, UserEO.class);
|
||||||
|
|
||||||
eo.setPassword("HZWLsoft.com123");
|
eo.setPassword("catarc2013");
|
||||||
if (StringUtils.isBlank(eo.getAccount())) {
|
if (StringUtils.isBlank(eo.getAccount())) {
|
||||||
return Result.error("用户名不能为空!");
|
return Result.error("用户名不能为空!");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,6 +3,7 @@ package com.adc.da.sys.service;
|
|||||||
import java.text.SimpleDateFormat;
|
import java.text.SimpleDateFormat;
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
|
|
||||||
|
import com.adc.da.sys.constant.UserBelongEnum;
|
||||||
import com.adc.da.sys.dao.mysql.UserRoleEODao;
|
import com.adc.da.sys.dao.mysql.UserRoleEODao;
|
||||||
import com.adc.da.sys.entity.RoleEO;
|
import com.adc.da.sys.entity.RoleEO;
|
||||||
import com.adc.da.sys.entity.UserRoleEO;
|
import com.adc.da.sys.entity.UserRoleEO;
|
||||||
@@ -86,7 +87,7 @@ public class UserEOServiceImpl implements IUserEoService {
|
|||||||
* 1代表本系统自建用户
|
* 1代表本系统自建用户
|
||||||
* 0代表同步过来的用户
|
* 0代表同步过来的用户
|
||||||
*/
|
*/
|
||||||
eo.setIsSelf("1");
|
eo.setIsSelf(UserBelongEnum.LOCAL.getValue());
|
||||||
eo.setUsid(com.adc.da.util.utils.UUID.randomUUID10());
|
eo.setUsid(com.adc.da.util.utils.UUID.randomUUID10());
|
||||||
dao.insert(eo);
|
dao.insert(eo);
|
||||||
} else {
|
} else {
|
||||||
@@ -206,10 +207,10 @@ public class UserEOServiceImpl implements IUserEoService {
|
|||||||
UserEO userEO = new UserEO();
|
UserEO userEO = new UserEO();
|
||||||
BeanUtils.copyProperties(userImportVO, userEO);
|
BeanUtils.copyProperties(userImportVO, userEO);
|
||||||
userEO.setDelFlag(0);
|
userEO.setDelFlag(0);
|
||||||
userEO.setIsSelf("1");
|
userEO.setIsSelf(UserBelongEnum.LOCAL.getValue());
|
||||||
userEO.setUsid(com.adc.da.util.utils.UUID.randomUUID10());
|
userEO.setUsid(com.adc.da.util.utils.UUID.randomUUID10());
|
||||||
userEO.setCellPhoneNumber(userImportVO.getCellPhoneNumber() == null ? EncryptUtil.encrypt("") : EncryptUtil.encrypt(userImportVO.getCellPhoneNumber()));
|
userEO.setCellPhoneNumber(userImportVO.getCellPhoneNumber() == null ? EncryptUtil.encrypt("") : EncryptUtil.encrypt(userImportVO.getCellPhoneNumber()));
|
||||||
userEO.setPassword(EncryptUtil.encrypt("EI$XIL@vdc9ptev7"));
|
userEO.setPassword(EncryptUtil.encrypt("catarc2013"));
|
||||||
userEO.setEmail(userImportVO.getEmail() == null ? EncryptUtil.encrypt("") : EncryptUtil.encrypt(userImportVO.getEmail()));
|
userEO.setEmail(userImportVO.getEmail() == null ? EncryptUtil.encrypt("") : EncryptUtil.encrypt(userImportVO.getEmail()));
|
||||||
userEO.setCreateTime(simpleDateFormat.format(date));
|
userEO.setCreateTime(simpleDateFormat.format(date));
|
||||||
userEO.setUpdateTime(simpleDateFormat.format(date));
|
userEO.setUpdateTime(simpleDateFormat.format(date));
|
||||||
|
|||||||
Reference in New Issue
Block a user