Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yd-user
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
xingmin
yd-user
Commits
59a7a603
Commit
59a7a603
authored
Oct 22, 2026
by
jianan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
idP
parent
8ac650dd
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
46 additions
and
22 deletions
+46
-22
yd-user-api/src/main/java/com/yd/user/api/client/CasdoorClient.java
+8
-5
yd-user-api/src/main/java/com/yd/user/api/service/impl/ApiClientUserServiceImpl.java
+38
-17
No files found.
yd-user-api/src/main/java/com/yd/user/api/client/CasdoorClient.java
View file @
59a7a603
...
...
@@ -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
;
}
...
...
yd-user-api/src/main/java/com/yd/user/api/service/impl/ApiClientUserServiceImpl.java
View file @
59a7a603
...
...
@@ -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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment