feat(education): resolve student tenant context
This commit is contained in:
@@ -4,6 +4,10 @@ import lombok.Data;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* 教育模块配置属性
|
||||
*
|
||||
@@ -24,4 +28,19 @@ public class EducationProperties {
|
||||
*/
|
||||
private String version = "1.0.0";
|
||||
|
||||
/**
|
||||
* 精确主机名到租户名的映射,用于 DNS 与 system_tenant.websites 不一致的场景。
|
||||
* key = 标准化后的主机名(小写、无端口),value = 租户名。
|
||||
* 示例:{ "staging.school.com": "demo-school" }
|
||||
*
|
||||
* 映射优先级高于 system_tenant.websites 字段匹配。
|
||||
*/
|
||||
private Map<String, String> hostnameTenantMap = new HashMap<>();
|
||||
|
||||
/**
|
||||
* 学生端全局开放的登录方式。它描述当前部署启用的 Member 登录入口,
|
||||
* 不是租户级 OAuth 提供方探测结果。
|
||||
*/
|
||||
private List<String> loginMethods = List.of("PASSWORD", "SMS");
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,71 @@
|
||||
package cn.iocoder.yudao.module.education.controller.app;
|
||||
|
||||
import cn.iocoder.yudao.framework.common.biz.system.tenant.TenantCommonApi;
|
||||
import cn.iocoder.yudao.framework.common.biz.system.tenant.dto.TenantRespDTO;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
|
||||
import cn.iocoder.yudao.framework.tenant.core.context.TenantContextHolder;
|
||||
import cn.iocoder.yudao.module.education.controller.app.vo.EducationContextRespVO;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.enums.GlobalErrorCodeConstants.UNAUTHORIZED;
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
import static cn.iocoder.yudao.module.education.enums.ErrorCodeConstants.EDUCATION_TENANT_NOT_ACTIVE;
|
||||
|
||||
/**
|
||||
* 教育当前上下文 Controller — 学生端
|
||||
*
|
||||
* <p>根据当前认证用户和安全上下文返回教育业务所需的租户与用户信息。
|
||||
* 该端点需要认证,不信任请求体中的 userId。</p>
|
||||
*
|
||||
* @author 恭学教育
|
||||
*/
|
||||
@Tag(name = "用户 APP - 教育上下文")
|
||||
@RestController
|
||||
@RequestMapping("/education")
|
||||
@Validated
|
||||
@ConditionalOnProperty(prefix = "yudao.education", name = "enabled", havingValue = "true")
|
||||
public class EducationContextController {
|
||||
|
||||
@Resource
|
||||
private TenantCommonApi tenantCommonApi;
|
||||
|
||||
@GetMapping("/context")
|
||||
@Operation(summary = "获取当前教育上下文",
|
||||
description = "根据当前认证用户和租户上下文返回教育业务信息。需要登录态。")
|
||||
public CommonResult<EducationContextRespVO> getContext() {
|
||||
// 1. 从安全上下文获取用户编号(不信任请求参数)
|
||||
Long userId = SecurityFrameworkUtils.getLoginUserId();
|
||||
if (userId == null) {
|
||||
throw exception(UNAUTHORIZED);
|
||||
}
|
||||
|
||||
// 2. 获取当前租户并验证状态(由 TenantSecurityWebFilter 前置完成,此处二次验证)
|
||||
Long tenantId = TenantContextHolder.getRequiredTenantId();
|
||||
tenantCommonApi.validateTenant(tenantId);
|
||||
|
||||
// 3. 获取租户信息用于展示
|
||||
TenantRespDTO tenant = tenantCommonApi.getTenant(tenantId);
|
||||
if (tenant == null) {
|
||||
throw exception(EDUCATION_TENANT_NOT_ACTIVE);
|
||||
}
|
||||
|
||||
// 构建响应
|
||||
EducationContextRespVO resp = EducationContextRespVO.builder()
|
||||
.userId(userId)
|
||||
.tenantId(tenantId)
|
||||
.tenantName(tenant.getName())
|
||||
.displayName(tenant.getName())
|
||||
.build();
|
||||
return success(resp);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
package cn.iocoder.yudao.module.education.controller.app.tenant;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import cn.iocoder.yudao.framework.common.biz.system.tenant.TenantCommonApi;
|
||||
import cn.iocoder.yudao.framework.common.biz.system.tenant.dto.TenantRespDTO;
|
||||
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
|
||||
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
|
||||
import cn.iocoder.yudao.framework.common.util.date.DateUtils;
|
||||
import cn.iocoder.yudao.framework.tenant.core.aop.TenantIgnore;
|
||||
import cn.iocoder.yudao.module.education.config.EducationProperties;
|
||||
import cn.iocoder.yudao.module.education.controller.app.tenant.vo.EducationTenantRespVO;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.Parameter;
|
||||
import io.swagger.v3.oas.annotations.Parameters;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import jakarta.annotation.Resource;
|
||||
import jakarta.annotation.security.PermitAll;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.validation.annotation.Validated;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
|
||||
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
|
||||
import static cn.iocoder.yudao.module.education.enums.ErrorCodeConstants.*;
|
||||
|
||||
/**
|
||||
* 教育租户识别 Controller — 学生端入口
|
||||
*
|
||||
* <p>用于在登录前通过主机名或租户名解析当前租户,返回引导信息。
|
||||
* 该端点无需认证(@PermitAll),忽略租户上下文(@TenantIgnore)。</p>
|
||||
*
|
||||
* @author 恭学教育
|
||||
*/
|
||||
@Tag(name = "用户 APP - 教育租户识别")
|
||||
@RestController
|
||||
@RequestMapping("/education/tenant")
|
||||
@Validated
|
||||
@ConditionalOnProperty(prefix = "yudao.education", name = "enabled", havingValue = "true")
|
||||
public class EducationTenantController {
|
||||
|
||||
@Resource
|
||||
private TenantCommonApi tenantCommonApi;
|
||||
|
||||
@Resource
|
||||
private EducationProperties educationProperties;
|
||||
|
||||
@GetMapping("/resolve")
|
||||
@PermitAll
|
||||
@TenantIgnore
|
||||
@Operation(summary = "解析租户",
|
||||
description = "通过主机名或租户名解析租户信息,返回登录引导所需的基础字段。" +
|
||||
"hostname 和 tenantName 至少提供一个。")
|
||||
@Parameters({
|
||||
@Parameter(name = "hostname", description = "标准化主机名(小写、无端口、无协议)", example = "school.example.com"),
|
||||
@Parameter(name = "tenantName", description = "租户名", example = "demo-school")
|
||||
})
|
||||
public CommonResult<EducationTenantRespVO> resolve(
|
||||
@RequestParam(value = "hostname", required = false) String hostname,
|
||||
@RequestParam(value = "tenantName", required = false) String tenantName) {
|
||||
|
||||
// 1. 校验输入:至少提供一个
|
||||
if (StrUtil.isBlank(hostname) && StrUtil.isBlank(tenantName)) {
|
||||
throw exception(EDUCATION_TENANT_RESOLVE_FAILED, "hostname 和 tenantName 不能同时为空");
|
||||
}
|
||||
|
||||
// 2. 主机名标准化
|
||||
String normalizedHostname = normalizeHostname(hostname);
|
||||
|
||||
// 3. 解析租户并验证一致性
|
||||
TenantRespDTO tenant = resolveTenantAndEnsureConsistency(normalizedHostname, tenantName);
|
||||
|
||||
// 4. 验证租户状态
|
||||
if (tenant == null) {
|
||||
throw exception(EDUCATION_TENANT_NOT_FOUND);
|
||||
}
|
||||
if (CommonStatusEnum.isDisable(tenant.getStatus())) {
|
||||
throw exception(EDUCATION_TENANT_DISABLED);
|
||||
}
|
||||
if (DateUtils.isExpired(tenant.getExpireTime())) {
|
||||
throw exception(EDUCATION_TENANT_NOT_ACTIVE);
|
||||
}
|
||||
|
||||
// 5. 构建响应
|
||||
EducationTenantRespVO resp = EducationTenantRespVO.builder()
|
||||
.tenantId(tenant.getId())
|
||||
.tenantName(tenant.getName())
|
||||
.displayName(tenant.getName())
|
||||
.status("ACTIVE")
|
||||
.loginMethods(new ArrayList<>(educationProperties.getLoginMethods()))
|
||||
.build();
|
||||
return success(resp);
|
||||
}
|
||||
|
||||
/**
|
||||
* 按优先级解析租户:
|
||||
* 1. 如果提供了 tenantName,直接通过租户名查询
|
||||
* 2. 如果提供了 hostname:
|
||||
* a. 先查 EducationProperties.hostnameTenantMap 配置映射
|
||||
* b. 再通过 system_tenant.websites 字段匹配
|
||||
*
|
||||
* @param hostname 标准化后的主机名,可能为 null
|
||||
* @param tenantName 租户名,可能为 null
|
||||
* @return 租户 DTO,未找到返回 null
|
||||
*/
|
||||
private TenantRespDTO resolveTenantAndEnsureConsistency(String hostname, String tenantName) {
|
||||
TenantRespDTO byName = StrUtil.isNotBlank(tenantName)
|
||||
? tenantCommonApi.getTenantByName(tenantName.trim()) : null;
|
||||
TenantRespDTO byHostname = StrUtil.isNotBlank(hostname) ? resolveTenantByHostname(hostname) : null;
|
||||
if (byName != null && byHostname != null && !byName.getId().equals(byHostname.getId())) {
|
||||
throw exception(EDUCATION_TENANT_RESOLVE_FAILED, "hostname 与 tenantName 指向不同租户");
|
||||
}
|
||||
if (StrUtil.isNotBlank(tenantName) && byName == null) {
|
||||
return null;
|
||||
}
|
||||
return byName != null ? byName : byHostname;
|
||||
}
|
||||
|
||||
private TenantRespDTO resolveTenantByHostname(String hostname) {
|
||||
String mappedTenantName = educationProperties.getHostnameTenantMap().entrySet().stream()
|
||||
.filter(entry -> hostname.equals(normalizeHostname(entry.getKey())))
|
||||
.map(entry -> entry.getValue())
|
||||
.findFirst()
|
||||
.orElse(null);
|
||||
if (StrUtil.isNotBlank(mappedTenantName)) {
|
||||
return tenantCommonApi.getTenantByName(mappedTenantName);
|
||||
}
|
||||
return tenantCommonApi.getTenantByWebsite(hostname);
|
||||
}
|
||||
|
||||
/**
|
||||
* 主机名标准化:转为小写并拒绝协议、路径和非法端口。
|
||||
* 端口会被保留,因为 system_tenant.websites 使用精确 authority 匹配。
|
||||
*
|
||||
* @param hostname 原始主机名
|
||||
* @return 标准化后的主机名,输入为空时返回 null
|
||||
* @throws cn.iocoder.yudao.framework.common.exception.ServiceException 格式不合法时
|
||||
*/
|
||||
static String normalizeHostname(String hostname) {
|
||||
if (StrUtil.isBlank(hostname)) {
|
||||
return null;
|
||||
}
|
||||
String normalized = hostname.trim();
|
||||
|
||||
// 拒绝含协议的输入(如 http://example.com)
|
||||
if (normalized.contains("://")) {
|
||||
throw exception(EDUCATION_TENANT_RESOLVE_FAILED,
|
||||
"hostname 不应包含协议,收到: " + hostname);
|
||||
}
|
||||
// 拒绝含路径的输入
|
||||
if (normalized.contains("/")) {
|
||||
throw exception(EDUCATION_TENANT_RESOLVE_FAILED,
|
||||
"hostname 不应包含路径,收到: " + hostname);
|
||||
}
|
||||
|
||||
// 校验端口。保留合法端口,确保与 system_tenant.websites 的精确值一致。
|
||||
int colonIdx = normalized.lastIndexOf(':');
|
||||
if (colonIdx > 0 && !normalized.startsWith("[")) {
|
||||
String afterColon = normalized.substring(colonIdx + 1);
|
||||
if (afterColon.isEmpty() || !afterColon.chars().allMatch(Character::isDigit)) {
|
||||
throw exception(EDUCATION_TENANT_RESOLVE_FAILED, "hostname 端口不合法");
|
||||
}
|
||||
int port;
|
||||
try {
|
||||
port = Integer.parseInt(afterColon);
|
||||
} catch (NumberFormatException ex) {
|
||||
throw exception(EDUCATION_TENANT_RESOLVE_FAILED, "hostname 端口不合法");
|
||||
}
|
||||
if (port < 1 || port > 65535) {
|
||||
throw exception(EDUCATION_TENANT_RESOLVE_FAILED, "hostname 端口不合法");
|
||||
}
|
||||
}
|
||||
|
||||
return normalized.toLowerCase();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,35 @@
|
||||
package cn.iocoder.yudao.module.education.controller.app.tenant.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Schema(description = "用户 APP - 教育租户识别 Response VO")
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class EducationTenantRespVO {
|
||||
|
||||
@Schema(description = "租户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
|
||||
private Long tenantId;
|
||||
|
||||
@Schema(description = "租户名", requiredMode = Schema.RequiredMode.REQUIRED, example = "demo-school")
|
||||
private String tenantName;
|
||||
|
||||
@Schema(description = "租户显示名称", example = "Demo School")
|
||||
private String displayName;
|
||||
|
||||
@Schema(description = "租户状态", requiredMode = Schema.RequiredMode.REQUIRED, example = "ACTIVE",
|
||||
allowableValues = {"ACTIVE", "DISABLED", "EXPIRED"})
|
||||
private String status;
|
||||
|
||||
@Schema(description = "支持的登录方式", requiredMode = Schema.RequiredMode.REQUIRED,
|
||||
example = "[\"PASSWORD\", \"SMS\"]")
|
||||
private List<String> loginMethods;
|
||||
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package cn.iocoder.yudao.module.education.controller.app.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
@Schema(description = "用户 APP - 教育当前上下文 Response VO")
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class EducationContextRespVO {
|
||||
|
||||
@Schema(description = "用户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "1024")
|
||||
private Long userId;
|
||||
|
||||
@Schema(description = "租户编号", requiredMode = Schema.RequiredMode.REQUIRED, example = "2048")
|
||||
private Long tenantId;
|
||||
|
||||
@Schema(description = "租户名", requiredMode = Schema.RequiredMode.REQUIRED, example = "demo-school")
|
||||
private String tenantName;
|
||||
|
||||
@Schema(description = "租户显示名称", example = "Demo School")
|
||||
private String displayName;
|
||||
|
||||
}
|
||||
@@ -12,4 +12,10 @@ public interface ErrorCodeConstants {
|
||||
// ========== 通用模块 1-005-001-000 ==========
|
||||
ErrorCode EDUCATION_DISABLED = new ErrorCode(1_005_001_000, "教育模块未启用,请联系管理员");
|
||||
|
||||
// ========== 租户识别 1-005-001-001 ~ 1-005-001-010 ==========
|
||||
ErrorCode EDUCATION_TENANT_NOT_FOUND = new ErrorCode(1_005_001_001, "租户不存在");
|
||||
ErrorCode EDUCATION_TENANT_DISABLED = new ErrorCode(1_005_001_002, "租户已被禁用");
|
||||
ErrorCode EDUCATION_TENANT_RESOLVE_FAILED = new ErrorCode(1_005_001_003, "租户识别失败:{}");
|
||||
ErrorCode EDUCATION_TENANT_NOT_ACTIVE = new ErrorCode(1_005_001_004, "当前租户不可用,请联系管理员");
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user