|
|
|
|
@ -2,8 +2,6 @@ package com.yau.digitalrmb.platformintegration.application;
|
|
|
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
|
|
|
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
|
|
|
|
import com.yau.digitalrmb.platformintegration.config.PlatformIntegrationProperties;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
|
|
|
|
|
@ -15,20 +13,26 @@ import java.time.Clock;
|
|
|
|
|
import java.time.Instant;
|
|
|
|
|
import java.util.Base64;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 校验智云(ZhiYun)下发的 SSO JWT。
|
|
|
|
|
* 逻辑与 cross_training_platform 的 TokenProvider.getJWTUserByZhiYun 保持一致:
|
|
|
|
|
* HS256 验签、共享密钥写死为本系统对接智云的同一把密钥、解析失败直接抛异常
|
|
|
|
|
* (不做子系统 token 兜底)。
|
|
|
|
|
*/
|
|
|
|
|
@Component
|
|
|
|
|
@ConditionalOnProperty(prefix = "platform-integration", name = "enabled", havingValue = "true")
|
|
|
|
|
public class PlatformTokenVerifier {
|
|
|
|
|
private final PlatformIntegrationProperties.Token properties;
|
|
|
|
|
/** 智云 SSO 共享密钥,与 cross_training_platform 的 TokenProvider.SECRET_ZHIYUN 为同一把 */
|
|
|
|
|
private static final String ZHIYUN_SECRET = "zy_kzy_mnjy_fp76ckwuczzmb67w0a8x0";
|
|
|
|
|
|
|
|
|
|
private final ObjectMapper objectMapper;
|
|
|
|
|
private final Clock clock;
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
public PlatformTokenVerifier(PlatformIntegrationProperties properties) {
|
|
|
|
|
this(properties.getToken(), new ObjectMapper(), Clock.systemUTC());
|
|
|
|
|
public PlatformTokenVerifier() {
|
|
|
|
|
this(new ObjectMapper(), Clock.systemUTC());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PlatformTokenVerifier(PlatformIntegrationProperties.Token properties, ObjectMapper objectMapper, Clock clock) {
|
|
|
|
|
this.properties = properties;
|
|
|
|
|
PlatformTokenVerifier(ObjectMapper objectMapper, Clock clock) {
|
|
|
|
|
this.objectMapper = objectMapper;
|
|
|
|
|
this.clock = clock;
|
|
|
|
|
}
|
|
|
|
|
@ -49,30 +53,44 @@ public class PlatformTokenVerifier {
|
|
|
|
|
}
|
|
|
|
|
verifySignature(parts);
|
|
|
|
|
JsonNode payload = readJson(parts[1]);
|
|
|
|
|
|
|
|
|
|
// exp 校验(智云 token 可能不带 exp,不带则跳过)
|
|
|
|
|
JsonNode expiration = payload.path("exp");
|
|
|
|
|
if (!expiration.isMissingNode()
|
|
|
|
|
&& (!expiration.canConvertToLong()
|
|
|
|
|
|| Instant.ofEpochSecond(expiration.asLong()).compareTo(now) <= 0)) {
|
|
|
|
|
throw invalid();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
String userId = requiredText(payload, "userId");
|
|
|
|
|
String username = requiredText(payload, "username");
|
|
|
|
|
String password = optionalText(payload, "password");
|
|
|
|
|
if (password == null) {
|
|
|
|
|
if (password == null || password.trim().isEmpty()) {
|
|
|
|
|
password = "123qwe";
|
|
|
|
|
}
|
|
|
|
|
int roleId = requiredRoleId(payload);
|
|
|
|
|
int roleId;
|
|
|
|
|
JsonNode roleNode = payload.path("roleid");
|
|
|
|
|
if (roleNode.isMissingNode() || !roleNode.canConvertToLong()) {
|
|
|
|
|
roleId = 4;
|
|
|
|
|
} else {
|
|
|
|
|
roleId = (int) roleNode.asLong();
|
|
|
|
|
}
|
|
|
|
|
String displayName = optionalText(payload, "name");
|
|
|
|
|
if (displayName == null || displayName.trim().isEmpty()) {
|
|
|
|
|
displayName = username;
|
|
|
|
|
}
|
|
|
|
|
String schoolId = optionalText(payload, "schoolId", "0");
|
|
|
|
|
String schoolName = optionalText(payload, "school", "schoolName");
|
|
|
|
|
String classId = optionalText(payload, "classId");
|
|
|
|
|
String className = optionalText(payload, "class");
|
|
|
|
|
String studentId = optionalText(payload, "studentNo", "studentId", "studentid");
|
|
|
|
|
String collegeId = optionalText(payload, "collegeId");
|
|
|
|
|
String collegeName = optionalText(payload, "collegeName", "college");
|
|
|
|
|
String majorId = optionalText(payload, "majorId");
|
|
|
|
|
String majorName = optionalText(payload, "majorName", "major");
|
|
|
|
|
|
|
|
|
|
return new VerifiedPlatformToken(userId, username, displayName, password, roleId,
|
|
|
|
|
optionalText(payload, "schoolId"),
|
|
|
|
|
optionalText(payload, "schoolName", "school"), optionalText(payload, "collegeId"),
|
|
|
|
|
optionalText(payload, "collegeName"), optionalText(payload, "majorId"),
|
|
|
|
|
optionalText(payload, "majorName", "major"), optionalText(payload, "classId"),
|
|
|
|
|
optionalText(payload, "className", "class"),
|
|
|
|
|
optionalText(payload, "studentId", "studentid", "studentNo"));
|
|
|
|
|
schoolId, schoolName, collegeId, collegeName, majorId, majorName, classId, className, studentId);
|
|
|
|
|
} catch (PlatformTokenException exception) {
|
|
|
|
|
throw exception;
|
|
|
|
|
} catch (Exception exception) {
|
|
|
|
|
@ -82,32 +100,25 @@ public class PlatformTokenVerifier {
|
|
|
|
|
|
|
|
|
|
private void verifySignature(String[] parts) throws Exception {
|
|
|
|
|
Mac mac = Mac.getInstance("HmacSHA256");
|
|
|
|
|
mac.init(new SecretKeySpec(properties.getLinkSecretKey().getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
|
|
|
|
|
mac.init(new SecretKeySpec(ZHIYUN_SECRET.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
|
|
|
|
|
byte[] expected = mac.doFinal((parts[0] + "." + parts[1]).getBytes(StandardCharsets.US_ASCII));
|
|
|
|
|
if (!MessageDigest.isEqual(expected, Base64.getUrlDecoder().decode(parts[2]))) {
|
|
|
|
|
throw invalid();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private int requiredRoleId(JsonNode payload) {
|
|
|
|
|
long roleId = requiredLong(payload, "roleid", "roleId");
|
|
|
|
|
if (roleId != 1L && roleId != 3L && roleId != 4L) {
|
|
|
|
|
throw invalid();
|
|
|
|
|
}
|
|
|
|
|
return (int) roleId;
|
|
|
|
|
private JsonNode readJson(String encoded) throws Exception {
|
|
|
|
|
return objectMapper.readTree(Base64.getUrlDecoder().decode(encoded));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private JsonNode readJson(String encoded) throws Exception { return objectMapper.readTree(Base64.getUrlDecoder().decode(encoded)); }
|
|
|
|
|
private long requiredLong(JsonNode payload, String... names) {
|
|
|
|
|
for (String name : names) {
|
|
|
|
|
JsonNode node = payload.path(name);
|
|
|
|
|
if (node.canConvertToLong()) {
|
|
|
|
|
return node.asLong();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
private String requiredText(JsonNode payload, String name) {
|
|
|
|
|
String value = optionalText(payload, name);
|
|
|
|
|
if (value == null) {
|
|
|
|
|
throw invalid();
|
|
|
|
|
}
|
|
|
|
|
private String requiredText(JsonNode payload, String name) { String value = payload.path(name).asText(); if (value == null || value.trim().isEmpty()) throw invalid(); return value; }
|
|
|
|
|
return value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private String optionalText(JsonNode payload, String... names) {
|
|
|
|
|
for (String name : names) {
|
|
|
|
|
JsonNode node = payload.path(name);
|
|
|
|
|
@ -120,5 +131,8 @@ public class PlatformTokenVerifier {
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
private PlatformTokenException invalid() { return new PlatformTokenException("平台登录凭据无效"); }
|
|
|
|
|
|
|
|
|
|
private PlatformTokenException invalid() {
|
|
|
|
|
return new PlatformTokenException("平台登录凭据无效");
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|