Commit 59a7a603 by jianan

idP

parent 8ac650dd
......@@ -56,14 +56,17 @@ public class CasdoorClient {
/**
* 创建用户,创建后按手机号查回 subject。
* @param nationalNumber 本地号码(不带国家区号),大陆 11 位 / 香港 8 位
* @param countryCode ISO 国家码,如 CN / HK(Casdoor 不接受 +86 形式)
*/
public String createUserAndGetSub(String username, String displayName, String phone, String password) {
public String createUserAndGetSub(String username, String displayName,
String nationalNumber, String countryCode, String password) {
Map<String, Object> body = new LinkedHashMap<>();
body.put("owner", properties.getOrganization());
body.put("name", username);
body.put("displayName", displayName);
body.put("phone", phone);
body.put("countryCode", "CN");
body.put("phone", nationalNumber);
body.put("countryCode", countryCode);
body.put("password", password);
String url = properties.getEndpoint() + "/api/add-user";
......@@ -71,9 +74,9 @@ public class CasdoorClient {
if (!"ok".equalsIgnoreCase(root.path("status").asText())) {
throw new BusinessException("Casdoor 创建用户失败:" + root.path("msg").asText());
}
String sub = findSubByPhone(phone);
String sub = findSubByPhone(nationalNumber);
if (StringUtils.isBlank(sub)) {
throw new BusinessException("Casdoor 创建用户成功但查回 subject 为空,phone=" + phone);
throw new BusinessException("Casdoor 创建用户成功但查回 subject 为空,phone=" + nationalNumber);
}
return sub;
}
......
......@@ -208,47 +208,68 @@ public class ApiClientUserServiceImpl implements ApiClientUserService {
if (StringUtils.isNotBlank(clientUser.getIdpSub())) {
return Result.success(clientUser.getIdpSub());
}
String phone = normalizePhone(clientUser.getPhone());
if (StringUtils.isBlank(phone)) {
throw new BusinessException("客户端用户手机号为空,无法开通统一身份");
}
PhoneNumber phone = parsePhone(clientUser.getPhone());
String sub = casdoorClient.findSubByPhone(phone);
String sub = casdoorClient.findSubByPhone(phone.nationalNumber);
if (StringUtils.isBlank(sub)) {
String initialPassword = buildInitialPassword(clientUser, phone);
String initialPassword = buildInitialPassword(clientUser, phone.nationalNumber);
// Casdoor 登录名用业务ID,保证唯一可溯源;用户实际以手机号登录
sub = casdoorClient.createUserAndGetSub(
clientUser.getClientUserBizId(), clientUser.getName(), phone, initialPassword);
clientUser.getClientUserBizId(), clientUser.getName(),
phone.nationalNumber, phone.countryCode, initialPassword);
}
clientUser.setIdpSub(sub);
iClientUserService.saveOrUpdate(clientUser);
return Result.success(sub);
}
/** 手机号规范化:去非数字、去国家码,得到 11 位本地号码。 */
private String normalizePhone(String phone) {
if (StringUtils.isBlank(phone)) {
return null;
/**
* 解析手机号为「ISO 国家码 + 本地号码」。支持大陆(11 位,可带 86)与香港(8 位,可带 852)。
* 转介人含港陆两类,故不能写死单一国家码。
*/
private PhoneNumber parsePhone(String raw) {
if (StringUtils.isBlank(raw)) {
throw new BusinessException("客户端用户手机号为空,无法开通统一身份");
}
String digits = phone.replaceAll("\\D", "");
String digits = raw.replaceAll("\\D", "");
if (digits.startsWith("86") && digits.length() == 13) {
digits = digits.substring(2);
return new PhoneNumber("CN", digits.substring(2));
}
if (digits.startsWith("852") && digits.length() == 11) {
return new PhoneNumber("HK", digits.substring(3));
}
return digits;
if (digits.length() == 11 && digits.matches("1[3-9]\\d{9}")) {
return new PhoneNumber("CN", digits);
}
if (digits.length() == 8) {
return new PhoneNumber("HK", digits);
}
throw new BusinessException("无法识别的手机号(仅支持大陆11位或香港8位):" + raw);
}
/** 规则初始密码:证件号后六位;无证件或不足六位时用手机号后六位。 */
private String buildInitialPassword(ClientUser clientUser, String phone) {
private String buildInitialPassword(ClientUser clientUser, String nationalNumber) {
String idCard = clientUser.getIdCardNumber();
if (StringUtils.isNotBlank(idCard) && idCard.length() >= 6) {
return idCard.substring(idCard.length() - 6);
}
if (phone.length() >= 6) {
return phone.substring(phone.length() - 6);
if (nationalNumber.length() >= 6) {
return nationalNumber.substring(nationalNumber.length() - 6);
}
throw new BusinessException("无法生成初始密码:证件号与手机号均不足六位");
}
/** 手机号解析结果:countryCode 为 Casdoor 要求的 ISO 码,nationalNumber 为不带区号的本地号。 */
private static class PhoneNumber {
private final String countryCode;
private final String nationalNumber;
private PhoneNumber(String countryCode, String nationalNumber) {
this.countryCode = countryCode;
this.nationalNumber = nationalNumber;
}
}
/**
* 同步sys_user系统用户,批量更新客户端用户表数据
* @param userBizIdList
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment