package com.yau.digitalrmb.institutionidentity.application; import com.yau.digitalrmb.shared.api.ErrorCode; import com.yau.digitalrmb.shared.exception.BusinessException; import org.springframework.security.oauth2.jwt.Jwt; public class InstitutionKeySubject { private final long userId; private final long schoolId; private final long classId; public InstitutionKeySubject(long userId, long schoolId, long classId) { this.userId = userId; this.schoolId = schoolId; this.classId = classId; } public static InstitutionKeySubject from(Jwt jwt) { return new InstitutionKeySubject(requiredLong(jwt, "userId"), requiredLong(jwt, "schoolId"), requiredLong(jwt, "classId")); } private static long requiredLong(Jwt jwt, String claim) { Object value = jwt.getClaims().get(claim); if (value == null) { throw new BusinessException(ErrorCode.UNAUTHORIZED, "Token缺少" + claim); } try { return Long.parseLong(String.valueOf(value)); } catch (NumberFormatException exception) { throw new BusinessException(ErrorCode.UNAUTHORIZED, "Token中的" + claim + "格式不正确"); } } public long getUserId() { return userId; } public long getSchoolId() { return schoolId; } public long getClassId() { return classId; } }