新增imes-token获取组织id接口

This commit is contained in:
gaoqr
2026-07-22 17:30:09 +08:00
parent 0c062e4d37
commit 8f8db544df
11 changed files with 70 additions and 11 deletions
@@ -22,6 +22,7 @@ logging:
chenfeng:
gateway:
ignore-token-urls:
- /admin-api/system/auth/app-token/organ-id
- /admin-api/executor/plate/wechat/miniprogram/cadview/info/*
- /admin-api/executor/plate/wechat/miniprogram/cadview/order/info/*
- /admin-api/executor/plate/wechat/miniprogram/cadview/order/details/*
@@ -14,9 +14,12 @@ import com.cf.imes.framework.security.config.SecurityProperties;
import com.cf.imes.framework.security.core.LoginUser;
import com.cf.imes.framework.security.core.util.SecurityFrameworkUtils;
import com.cf.imes.module.system.api.sms.dto.code.SmsCodeSendReqDTO;
import com.cf.imes.module.system.controller.admin.auth.vo.AuthAppTokenReqVO;
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginReqVO;
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginSmsCheckReqVO;
import com.cf.imes.module.system.controller.admin.auth.vo.AuthPermissionInfoRespVO;
import com.cf.imes.module.system.controller.admin.auth.vo.AuthSmsSendReqVO;
import com.cf.imes.module.system.convert.auth.AuthConvert;
import com.cf.imes.module.system.dal.dataobject.organ.OrganizationDO;
import com.cf.imes.module.system.dal.dataobject.permission.MenuDO;
@@ -29,9 +32,8 @@ import com.cf.imes.module.system.service.permission.MenuService;
import com.cf.imes.module.system.service.permission.PermissionService;
import com.cf.imes.module.system.service.permission.RoleService;
import com.cf.imes.module.system.service.sms.SmsCodeService;
import com.cf.imes.module.system.service.tokenconfig.TokenConfigService;
import com.cf.imes.module.system.service.user.AdminUserService;
import com.cf.imes.module.system.controller.admin.auth.vo.AuthLoginRespVO;
import com.cf.imes.module.system.controller.admin.auth.vo.AuthSmsSendReqVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
@@ -86,6 +88,9 @@ public class AuthController {
@Resource
private IPQueryService ipQueryService;
@Resource
private TokenConfigService tokenConfigService;
@PostMapping("/manage/login")
@PermitAll
@Operation(summary = "使用账号密码登录")
@@ -120,6 +125,20 @@ public class AuthController {
return success(authService.loginSmsCheck(reqVO));
}
/**
* 根据应用 Token 获取所属组织编号,供外部应用在未登录场景下识别组织。
*
* @param reqVO 应用 Token 请求参数
* @return 组织编号
*/
@PostMapping("/app-token/organ-id")
@PermitAll
@Operation(summary = "根据应用 Token 获取组织编号")
@OperateLog(enable = false)
public CommonResult<Long> getOrganIdByAppToken(@Valid @RequestBody AuthAppTokenReqVO reqVO) {
return success(tokenConfigService.getOrganIdByAppToken(reqVO.getAppToken()));
}
@PostMapping("/logout")
@PermitAll
@Operation(summary = "登出系统")
@@ -47,7 +47,8 @@ public interface TokenConfigMapper extends BaseMapperX<TokenConfigDO> {
default TokenConfigDO selectByToken(String token) {
return selectOne(new LambdaQueryWrapperX<TokenConfigDO>()
.eq(TokenConfigDO::getAppToken,token));
.eq(TokenConfigDO::getAppToken, token)
.eq(TokenConfigDO::getDeleted, false));
}
void deleteTokenConfig(@Param("id") Long id);
@@ -57,6 +57,14 @@ public interface TokenConfigService {
*/
Long checkTokenConfig(String token);
/**
* 根据数据库中存储的应用 Token 获取所属组织编号。
*
* @param appToken 应用 Token
* @return 组织编号
*/
Long getOrganIdByAppToken(String appToken);
/**
* 复制token
*
@@ -207,8 +207,13 @@ public class TokenConfigServiceImpl implements TokenConfigService{
@Override
public Long checkTokenConfig(String token) {
return getOrganIdByAppToken(token.substring(FIELD_IMES.length()));
}
TokenConfigDO tokenConfigDO = tokenConfigMapper.selectByToken(token.substring(5));
@Override
public Long getOrganIdByAppToken(String appToken) {
// 应用 Token 必须存在且处于有效期内,避免失效配置继续访问外部接口。
TokenConfigDO tokenConfigDO = tokenConfigMapper.selectByToken(appToken);
if(tokenConfigDO == null){
throw new ServiceException(TOKEN_CONFIG_NOT_EXISTS);
@@ -219,7 +224,7 @@ public class TokenConfigServiceImpl implements TokenConfigService{
}
Long organId = tokenConfigDO.getOrganId();
// 同步校验组织状态和有效期,保证返回的组织编号当前可用。
OrganizationDO organizationDO = organMapper.selectById(organId);
if (organizationDO == null) {
@@ -187,6 +187,7 @@ chenfeng:
- /admin-api/system/user/reset-password
- /admin-api/system/auth/login/check
- /admin-api/system/auth/login/sms/check
- /admin-api/system/auth/app-token/organ-id
- /admin-api/system/token/check
- /rpc-api/**
- /admin-api/system/pay/**
@@ -202,6 +202,7 @@ chenfeng:
- /admin-api/system/user/reset-password
- /admin-api/system/auth/login/check
- /admin-api/system/auth/login/sms/check
- /admin-api/system/auth/app-token/organ-id
- /admin-api/system/token/check
- /rpc-api/**
- /admin-api/system/pay/**
@@ -109,3 +109,5 @@ assembly.config.save.id.notNull=裝備設定編號不能為空
assembly.config.save.structure.config.notNull=裝備設定結構配寘不能為空
common.status.invalid=狀態不合法
auth.appToken.notBlank=應用 Token 不能為空
auth.appToken.size=應用 Token 長度不能超過 255 個字元
@@ -109,3 +109,5 @@ assembly.config.save.id.notNull=Assembly setting number cannot be empty
assembly.config.save.structure.config.notNull=Assembly setting structure configuration cannot be empty
common.status.invalid=The status is illegal
auth.appToken.notBlank=Application Token cannot be blank
auth.appToken.size=Application Token cannot exceed 255 characters
@@ -109,3 +109,5 @@ assembly.config.save.id.notNull=装备设置编号不能为空
assembly.config.save.structure.config.notNull=装备设置结构配置不能为空
common.status.invalid=状态不合法
auth.appToken.notBlank=应用 Token 不能为空
auth.appToken.size=应用 Token 长度不能超过 255 个字符
@@ -28,6 +28,7 @@ import com.cf.imes.module.system.service.permission.MenuService;
import com.cf.imes.module.system.service.permission.PermissionService;
import com.cf.imes.module.system.service.permission.RoleService;
import com.cf.imes.module.system.service.sms.SmsCodeService;
import com.cf.imes.module.system.service.tokenconfig.TokenConfigService;
import com.cf.imes.module.system.service.user.AdminUserService;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.servlet.http.HttpServletRequest;
@@ -115,6 +116,9 @@ class AuthControllerTest extends BaseWebUnitTest {
@MockitoBean
private ApiErrorLogApi apiErrorLogApi;
@MockitoBean
private TokenConfigService tokenConfigService;
@Autowired
private ObjectMapper objectMapper;
@@ -202,6 +206,19 @@ class AuthControllerTest extends BaseWebUnitTest {
verify(authService, times(1)).loginSmsCheck(smsCheckReq);
}
@Test
void testGetOrganIdByAppToken() throws Exception {
when(tokenConfigService.getOrganIdByAppToken("test-app-token")).thenReturn(100L);
mockMvc.perform(post("/admin-api/system/auth/app-token/organ-id")
.contentType(MediaType.APPLICATION_JSON)
.content("{\"appToken\":\"test-app-token\"}"))
.andExpect(status().isOk())
.andExpect(jsonPath("$.data").value(100L));
verify(tokenConfigService, times(1)).getOrganIdByAppToken("test-app-token");
}
@Test
void testLogout_withToken() throws Exception {
// mock token header/parameter