feat(open-platform): 新增用户分页查询接口并更新订单权限及时间格式
- 添加 OpenUserController 支持分页查询本租户用户,权限为 sys:user:list - 引入 OpenUserPageParam 作为用户查询白名单参数类,过滤敏感字段 - 定义 OpenUserVO,确保响应脱敏手机号、邮箱且不包含密码字段 - 修正订单接口权限 scope 由 order:read 改为 shop:shopOrder:list - 订单及退款时间字段增加 JsonFormat 注解,统一时间格式为 yyyy-MM-dd HH:mm:ss - 更新权限常量 OpenScopes,新增 SHOP_ORDER_LIST 和 SYS_USER_LIST - 增加 OpenUserVO 单元测试,确保敏感字段不被泄露且脱敏逻辑正确 - 新增开放平台调用验证脚本 scripts/verify-open-api.sh,覆盖令牌获取及接口调用验证 - 修改接口文档与示例,明确权限 scope 和时间格式要求 - 修复 TenantController 权限校验问题,确保租户字段来源于令牌且不可伪造 - 禁用伪造 tenantId 请求头修改数据范围,保证租户数据隔离安全
This commit is contained in:
@@ -9,8 +9,20 @@ package com.gxwebsoft.openplatform.constant;
|
||||
*/
|
||||
public final class OpenScopes {
|
||||
|
||||
/** 查询订单 */
|
||||
public static final String ORDER_READ = "order:read";
|
||||
/**
|
||||
* 查询项目订单。
|
||||
*
|
||||
* <p>与 sys_menu.authority 里的既有权限点同名(菜单 157795「查询」、182274「项目订单」),
|
||||
* 便于与内部权限体系对照。</p>
|
||||
*/
|
||||
public static final String SHOP_ORDER_LIST = "shop:shopOrder:list";
|
||||
|
||||
/**
|
||||
* 查询用户列表。
|
||||
*
|
||||
* <p>与 sys_menu.authority 里的既有权限点同名(菜单 3561 等「查询」)。</p>
|
||||
*/
|
||||
public static final String SYS_USER_LIST = "sys:user:list";
|
||||
|
||||
private OpenScopes() {
|
||||
}
|
||||
|
||||
@@ -50,7 +50,7 @@ public class OpenOrderController {
|
||||
@Resource
|
||||
private OpenPlatformProperties properties;
|
||||
|
||||
@PreAuthorize("hasAuthority('SCOPE_" + OpenScopes.ORDER_READ + "')")
|
||||
@PreAuthorize("hasAuthority('SCOPE_" + OpenScopes.SHOP_ORDER_LIST + "')")
|
||||
@Operation(summary = "分页查询本租户订单")
|
||||
@GetMapping("/page")
|
||||
public ApiResult<OpenPageResult<OpenOrderVO>> page(@AuthenticationPrincipal Jwt jwt,
|
||||
|
||||
@@ -0,0 +1,82 @@
|
||||
package com.gxwebsoft.openplatform.controller;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.gxwebsoft.common.core.Constants;
|
||||
import com.gxwebsoft.common.core.web.ApiResult;
|
||||
import com.gxwebsoft.common.core.web.PageResult;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.common.system.param.UserParam;
|
||||
import com.gxwebsoft.common.system.service.UserService;
|
||||
import com.gxwebsoft.openplatform.config.OpenPlatformProperties;
|
||||
import com.gxwebsoft.openplatform.constant.OpenScopes;
|
||||
import com.gxwebsoft.openplatform.context.OpenTenantContext;
|
||||
import com.gxwebsoft.openplatform.param.OpenUserPageParam;
|
||||
import com.gxwebsoft.openplatform.vo.OpenUserVO;
|
||||
import com.gxwebsoft.openplatform.web.OpenPageResult;
|
||||
import io.swagger.v3.oas.annotations.Operation;
|
||||
import io.swagger.v3.oas.annotations.tags.Tag;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.security.access.prepost.PreAuthorize;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 开放平台 · 用户接口。
|
||||
*
|
||||
* <p>{@code sys_user} 不在 {@code MybatisPlusConfig} 的 {@code ignoreTable} 白名单里,
|
||||
* 多租户插件会自动加上 {@code tenant_id} 条件;这里仍显式设置一次,
|
||||
* 两个来源都指向令牌里的租户({@code OpenTenantContext}),属于双保险。</p>
|
||||
*
|
||||
* @author WebSoft
|
||||
*/
|
||||
@Tag(name = "开放接口-用户")
|
||||
@RestController
|
||||
@ConditionalOnProperty(prefix = "open-platform", name = "enabled", havingValue = "true", matchIfMissing = true)
|
||||
@RequestMapping("/api/open/v1/user")
|
||||
public class OpenUserController {
|
||||
|
||||
@Resource
|
||||
private UserService userService;
|
||||
@Resource
|
||||
private OpenPlatformProperties properties;
|
||||
|
||||
@PreAuthorize("hasAuthority('SCOPE_" + OpenScopes.SYS_USER_LIST + "')")
|
||||
@Operation(summary = "分页查询本租户用户")
|
||||
@GetMapping("/page")
|
||||
public ApiResult<OpenPageResult<OpenUserVO>> page(OpenUserPageParam query) {
|
||||
// 租户只认令牌,拦截器已校验 tenant_id 存在
|
||||
Integer tenantId = OpenTenantContext.getTenantId();
|
||||
|
||||
UserParam param = new UserParam();
|
||||
param.setPage(normalizePage(query.getPage()));
|
||||
param.setLimit(normalizeLimit(query.getLimit()));
|
||||
param.setTenantId(tenantId);
|
||||
param.setUsername(StrUtil.trimToNull(query.getUsername()));
|
||||
param.setNickname(StrUtil.trimToNull(query.getNickname()));
|
||||
param.setType(query.getType());
|
||||
param.setStatus(query.getStatus());
|
||||
param.setCreateTimeStart(StrUtil.trimToNull(query.getCreateTimeStart()));
|
||||
param.setCreateTimeEnd(StrUtil.trimToNull(query.getCreateTimeEnd()));
|
||||
|
||||
PageResult<User> result = userService.pageRel(param);
|
||||
List<OpenUserVO> list = OpenUserVO.from(result.getList(), properties.isMaskSensitive());
|
||||
return new ApiResult<>(Constants.RESULT_OK_CODE, Constants.RESULT_OK_MSG,
|
||||
new OpenPageResult<>(list, result.getCount(), param.getPage(), param.getLimit()));
|
||||
}
|
||||
|
||||
private Long normalizePage(Long page) {
|
||||
return (page == null || page < 1L) ? 1L : page;
|
||||
}
|
||||
|
||||
private Long normalizeLimit(Long limit) {
|
||||
if (limit == null || limit < 1L) {
|
||||
return 20L;
|
||||
}
|
||||
return Math.min(limit, OpenUserPageParam.MAX_LIMIT);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
package com.gxwebsoft.openplatform.param;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
/**
|
||||
* 开放接口用户分页查询入参。
|
||||
*
|
||||
* <p>白名单字段,刻意不含 {@code password}、{@code tenantId} 等内部可构造字段。</p>
|
||||
*
|
||||
* @author WebSoft
|
||||
*/
|
||||
@Data
|
||||
@Schema(name = "OpenUserPageParam", description = "开放接口用户分页查询参数")
|
||||
public class OpenUserPageParam implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
/** 每页最大条数 */
|
||||
public static final long MAX_LIMIT = 100L;
|
||||
|
||||
@Schema(description = "页码,从 1 开始", example = "1")
|
||||
private Long page = 1L;
|
||||
|
||||
@Schema(description = "每页数量,最大 100", example = "20")
|
||||
private Long limit = 20L;
|
||||
|
||||
@Schema(description = "账号,模糊匹配")
|
||||
private String username;
|
||||
|
||||
@Schema(description = "昵称,模糊匹配")
|
||||
private String nickname;
|
||||
|
||||
@Schema(description = "用户类型")
|
||||
private Integer type;
|
||||
|
||||
@Schema(description = "用户状态")
|
||||
private Integer status;
|
||||
|
||||
@Schema(description = "注册时间起始,格式 yyyy-MM-dd HH:mm:ss")
|
||||
private String createTimeStart;
|
||||
|
||||
@Schema(description = "注册时间结束,格式 yyyy-MM-dd HH:mm:ss")
|
||||
private String createTimeEnd;
|
||||
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
package com.gxwebsoft.openplatform.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.gxwebsoft.common.system.entity.Order;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
@@ -71,13 +72,19 @@ public class OpenOrderVO implements Serializable {
|
||||
@Schema(description = "备注")
|
||||
private String comments;
|
||||
|
||||
@Schema(description = "支付时间")
|
||||
// 与 Order 实体的序列化保持一致:输出库中存储的挂钟时间,不做时区换算。
|
||||
// 注意 JacksonConfig 里的 @Primary ObjectMapper 是新建的,不加载 spring.jackson.* 配置,
|
||||
// 不显式声明格式会退化成 ISO-8601(2024-11-12T21:03:31.000+00:00)。
|
||||
@Schema(description = "支付时间,格式 yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date payTime;
|
||||
|
||||
@Schema(description = "退款时间")
|
||||
@Schema(description = "退款时间,格式 yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date refundTime;
|
||||
|
||||
@Schema(description = "下单时间")
|
||||
@Schema(description = "下单时间,格式 yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private Date createTime;
|
||||
|
||||
/**
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
package com.gxwebsoft.openplatform.vo;
|
||||
|
||||
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.Data;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 开放接口的用户出参。
|
||||
*
|
||||
* <p><b>安全要点</b>:{@code User} 实体没有对 {@code password} 做 {@code @JsonIgnore},
|
||||
* 且 UserMapper 用的是 {@code SELECT a.*},直接把实体返回给第三方会泄露密码哈希与支付密码。
|
||||
* 这里只挑出必要字段,密码类字段一律不出现。</p>
|
||||
*
|
||||
* @author WebSoft
|
||||
*/
|
||||
@Data
|
||||
@Schema(name = "OpenUser", description = "开放接口用户")
|
||||
public class OpenUserVO implements Serializable {
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@Schema(description = "用户id")
|
||||
private Integer userId;
|
||||
|
||||
@Schema(description = "用户编码")
|
||||
private String userCode;
|
||||
|
||||
@Schema(description = "账号")
|
||||
private String username;
|
||||
|
||||
@Schema(description = "昵称")
|
||||
private String nickname;
|
||||
|
||||
@Schema(description = "真实姓名")
|
||||
private String realName;
|
||||
|
||||
@Schema(description = "用户类型")
|
||||
private Integer type;
|
||||
|
||||
@Schema(description = "性别字典值")
|
||||
private String sex;
|
||||
|
||||
@Schema(description = "性别名称")
|
||||
private String sexName;
|
||||
|
||||
@Schema(description = "手机号(按配置脱敏)")
|
||||
private String phone;
|
||||
|
||||
@Schema(description = "邮箱(按配置脱敏)")
|
||||
private String email;
|
||||
|
||||
@Schema(description = "邮箱是否验证,0否 1是")
|
||||
private Integer emailVerified;
|
||||
|
||||
@Schema(description = "机构id")
|
||||
private Integer organizationId;
|
||||
|
||||
@Schema(description = "机构名称")
|
||||
private String organizationName;
|
||||
|
||||
@Schema(description = "状态")
|
||||
private Integer status;
|
||||
|
||||
@Schema(description = "审核状态:0待审核 1已通过 2已拒绝")
|
||||
private Integer auditStatus;
|
||||
|
||||
@Schema(description = "注册时间,格式 yyyy-MM-dd HH:mm:ss")
|
||||
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||
private LocalDateTime createTime;
|
||||
|
||||
/**
|
||||
* 实体转出参。
|
||||
*
|
||||
* @param users 用户列表
|
||||
* @param maskSensitive 是否对手机号、邮箱脱敏
|
||||
*/
|
||||
public static List<OpenUserVO> from(List<User> users, boolean maskSensitive) {
|
||||
if (users == null || users.isEmpty()) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
List<OpenUserVO> list = new ArrayList<>(users.size());
|
||||
for (User user : users) {
|
||||
OpenUserVO vo = new OpenUserVO();
|
||||
vo.setUserId(user.getUserId());
|
||||
vo.setUserCode(user.getUserCode());
|
||||
vo.setUsername(user.getUsername());
|
||||
vo.setNickname(user.getNickname());
|
||||
vo.setRealName(user.getRealName());
|
||||
vo.setType(user.getType());
|
||||
vo.setSex(user.getSex());
|
||||
vo.setSexName(user.getSexName());
|
||||
vo.setPhone(maskSensitive ? maskPhone(user.getPhone()) : user.getPhone());
|
||||
vo.setEmail(maskSensitive ? maskEmail(user.getEmail()) : user.getEmail());
|
||||
vo.setEmailVerified(user.getEmailVerified());
|
||||
vo.setOrganizationId(user.getOrganizationId());
|
||||
vo.setOrganizationName(user.getOrganizationName());
|
||||
vo.setStatus(user.getStatus());
|
||||
vo.setAuditStatus(user.getAuditStatus());
|
||||
vo.setCreateTime(user.getCreateTime());
|
||||
list.add(vo);
|
||||
}
|
||||
return list;
|
||||
}
|
||||
|
||||
private static String maskPhone(String phone) {
|
||||
if (phone == null || phone.length() < 7) {
|
||||
return phone;
|
||||
}
|
||||
return phone.substring(0, 3) + "****" + phone.substring(phone.length() - 4);
|
||||
}
|
||||
|
||||
private static String maskEmail(String email) {
|
||||
if (email == null) {
|
||||
return null;
|
||||
}
|
||||
int at = email.indexOf('@');
|
||||
if (at <= 0) {
|
||||
// 不是正常的邮箱格式,原样返回,不臆造
|
||||
return email;
|
||||
}
|
||||
// 统一保留局部名首字符与完整域名:zhangsan@example.com -> z***@example.com
|
||||
return email.charAt(0) + "***" + email.substring(at);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -70,7 +70,7 @@ class OpenPlatformJwtValidationTest {
|
||||
.subject("demo-app")
|
||||
.claim("client_id", "demo-app")
|
||||
.claim("tenant_id", 1001)
|
||||
.claim("scope", "order:read")
|
||||
.claim("scope", "shop:shopOrder:list")
|
||||
.issueTime(Date.from(Instant.now().minusSeconds(30)))
|
||||
.expirationTime(Date.from(expiresAt))
|
||||
.build();
|
||||
@@ -89,7 +89,7 @@ class OpenPlatformJwtValidationTest {
|
||||
assertNotNull(jwt);
|
||||
assertEquals("demo-app", jwt.getClaimAsString("client_id"));
|
||||
assertEquals(1001, ((Number) jwt.getClaim("tenant_id")).intValue());
|
||||
assertEquals("order:read", jwt.getClaimAsString("scope"));
|
||||
assertEquals("shop:shopOrder:list", jwt.getClaimAsString("scope"));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -42,7 +42,7 @@ class OpenTenantBindingTest {
|
||||
Jwt.Builder builder = Jwt.withTokenValue("test-token")
|
||||
.header("alg", "RS256")
|
||||
.claim("client_id", "demo-app")
|
||||
.claim("scope", "order:read order:write");
|
||||
.claim("scope", "shop:shopOrder:list shop:shopOrder:save");
|
||||
if (tenantId != null) {
|
||||
builder.claim("tenant_id", tenantId);
|
||||
}
|
||||
@@ -62,7 +62,8 @@ class OpenTenantBindingTest {
|
||||
|
||||
assertEquals(1001, OpenTenantContext.getTenantId());
|
||||
assertEquals("demo-app", OpenTenantContext.get().getClientId());
|
||||
assertEquals(List.of("order:read", "order:write"), OpenTenantContext.get().getScopes());
|
||||
assertEquals(List.of("shop:shopOrder:list", "shop:shopOrder:save"),
|
||||
OpenTenantContext.get().getScopes());
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -0,0 +1,99 @@
|
||||
package com.gxwebsoft.openplatform;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.gxwebsoft.common.system.entity.User;
|
||||
import com.gxwebsoft.openplatform.vo.OpenUserVO;
|
||||
import org.junit.jupiter.api.DisplayName;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* 开放接口用户出参测试。
|
||||
*
|
||||
* <p>重点是<b>不能泄露密码</b>:User 实体没有 @JsonIgnore,UserMapper 又用 SELECT a.*,
|
||||
* 所以这里直接序列化结果做断言,防止以后有人图省事把实体返回出去。</p>
|
||||
*
|
||||
* @author WebSoft
|
||||
*/
|
||||
class OpenUserVOTest {
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
private User user() {
|
||||
User user = new User();
|
||||
user.setUserId(1);
|
||||
user.setUsername("zhangsan");
|
||||
user.setNickname("张三");
|
||||
user.setPhone("13800000009");
|
||||
user.setEmail("zhangsan@example.com");
|
||||
// 敏感字段:绝不能出现在开放接口响应里
|
||||
user.setPassword("$2a$10$abcdefghijklmnopqrstuv");
|
||||
user.setPayPassword("$2a$10$paypaypaypaypaypaypayp");
|
||||
user.setIdCard("450102199001011234");
|
||||
return user;
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("序列化结果里不含密码、支付密码、身份证号")
|
||||
void neverExposesCredentials() throws Exception {
|
||||
List<OpenUserVO> list = OpenUserVO.from(List.of(user()), true);
|
||||
|
||||
String json = MAPPER.writeValueAsString(list);
|
||||
|
||||
assertFalse(json.contains("password"), "响应不应包含 password / payPassword 字段");
|
||||
assertFalse(json.contains("payPassword"), "响应不应包含 payPassword 字段");
|
||||
assertFalse(json.contains("$2a$10$"), "响应不应包含密码哈希");
|
||||
assertFalse(json.contains("idCard"), "响应不应包含身份证号字段");
|
||||
assertTrue(json.contains("zhangsan"));
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("手机号与邮箱按开关脱敏")
|
||||
void masksContactInfo() {
|
||||
OpenUserVO masked = OpenUserVO.from(List.of(user()), true).get(0);
|
||||
assertEquals("138****0009", masked.getPhone());
|
||||
assertEquals("z***@example.com", masked.getEmail());
|
||||
|
||||
OpenUserVO plain = OpenUserVO.from(List.of(user()), false).get(0);
|
||||
assertEquals("13800000009", plain.getPhone());
|
||||
assertEquals("zhangsan@example.com", plain.getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("异常联系方式的兜底处理")
|
||||
void handlesUnusualContactInfo() {
|
||||
User user = user();
|
||||
|
||||
user.setPhone("123");
|
||||
user.setEmail("a@b.com");
|
||||
OpenUserVO vo = OpenUserVO.from(List.of(user), true).get(0);
|
||||
assertEquals("123", vo.getPhone());
|
||||
assertEquals("a***@b.com", vo.getEmail());
|
||||
|
||||
// 非法格式不臆造,原样返回
|
||||
user.setEmail("@example.com");
|
||||
assertEquals("@example.com", OpenUserVO.from(List.of(user), true).get(0).getEmail());
|
||||
user.setEmail("no-at-sign");
|
||||
assertEquals("no-at-sign", OpenUserVO.from(List.of(user), true).get(0).getEmail());
|
||||
|
||||
user.setPhone(null);
|
||||
user.setEmail(null);
|
||||
vo = OpenUserVO.from(List.of(user), true).get(0);
|
||||
assertNull(vo.getPhone());
|
||||
assertNull(vo.getEmail());
|
||||
}
|
||||
|
||||
@Test
|
||||
@DisplayName("空列表返回空集合")
|
||||
void handlesEmptyList() {
|
||||
assertTrue(OpenUserVO.from(null, true).isEmpty());
|
||||
assertTrue(OpenUserVO.from(List.of(), true).isEmpty());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user