From 00252f392f4bab55b3c2e3a64f864ef5edf1598d Mon Sep 17 00:00:00 2001 From: EightMonth Date: Tue, 10 Dec 2024 16:24:31 +0800 Subject: [PATCH 1/4] =?UTF-8?q?keys=E6=9B=BF=E6=8D=A2scan=E9=97=AE?= =?UTF-8?q?=E9=A2=98=20=E4=BF=AE=E5=A4=8D=20#6918=20#6876?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../system/controller/SysDepartController.java | 11 +++++++---- .../system/controller/SysUserOnlineController.java | 2 +- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysDepartController.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysDepartController.java index fafac3014d..5e1b1232f5 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysDepartController.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysDepartController.java @@ -15,6 +15,7 @@ import org.jeecg.common.system.util.JwtUtil; import org.jeecg.common.system.vo.LoginUser; import org.jeecg.common.util.ImportExcelUtil; +import org.jeecg.common.util.RedisUtil; import org.jeecg.common.util.YouBianCodeUtil; import org.jeecg.common.util.oConvertUtils; import org.jeecg.config.mybatis.MybatisPlusSaasConfig; @@ -65,6 +66,8 @@ public class SysDepartController { private ISysUserService sysUserService; @Autowired private ISysUserDepartService sysUserDepartService; + @Autowired + private RedisUtil redisUtil; /** * 查询数据 查出我的部门,并以树结构数据格式响应给前端 * @@ -470,8 +473,8 @@ public Result importExcel(HttpServletRequest request, HttpServletResponse res //update-end---author:wangshuai---date:2023-10-19---for:【QQYUN-5482】系统的部门导入导出也可以改成敲敲云模式的部门路径--- //清空部门缓存 - Set keys3 = redisTemplate.keys(CacheConstant.SYS_DEPARTS_CACHE + "*"); - Set keys4 = redisTemplate.keys(CacheConstant.SYS_DEPART_IDS_CACHE + "*"); + List keys3 = redisUtil.scan(CacheConstant.SYS_DEPARTS_CACHE + "*"); + List keys4 = redisUtil.scan(CacheConstant.SYS_DEPART_IDS_CACHE + "*"); redisTemplate.delete(keys3); redisTemplate.delete(keys4); return ImportExcelUtil.imporReturnRes(errorMessageList.size(), listSysDeparts.size() - errorMessageList.size(), errorMessageList); @@ -664,8 +667,8 @@ public Result appImportExcel(HttpServletRequest request, HttpServletResponse listSysDeparts = ExcelImportUtil.importExcel(file.getInputStream(), ExportDepartVo.class, params); sysDepartService.importExcel(listSysDeparts,errorMessageList); //清空部门缓存 - Set keys3 = redisTemplate.keys(CacheConstant.SYS_DEPARTS_CACHE + "*"); - Set keys4 = redisTemplate.keys(CacheConstant.SYS_DEPART_IDS_CACHE + "*"); + List keys3 = redisUtil.scan(CacheConstant.SYS_DEPARTS_CACHE + "*"); + List keys4 = redisUtil.scan(CacheConstant.SYS_DEPART_IDS_CACHE + "*"); redisTemplate.delete(keys3); redisTemplate.delete(keys4); return ImportExcelUtil.imporReturnRes(errorMessageList.size(), listSysDeparts.size() - errorMessageList.size(), errorMessageList); diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysUserOnlineController.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysUserOnlineController.java index e6aed75313..66abfe6f89 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysUserOnlineController.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/system/controller/SysUserOnlineController.java @@ -52,7 +52,7 @@ public class SysUserOnlineController { @RequestMapping(value = "/list", method = RequestMethod.GET) public Result> list(@RequestParam(name="username", required=false) String username, @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) { - Collection keys = redisTemplate.keys(CommonConstant.PREFIX_USER_TOKEN + "*"); + Collection keys = redisUtil.scan(CommonConstant.PREFIX_USER_TOKEN + "*"); List onlineList = new ArrayList(); for (String key : keys) { String token = (String)redisUtil.get(key); From f7f6d0ff88cc32c7119247d20ef9fe36c0851a18 Mon Sep 17 00:00:00 2001 From: EightMonth Date: Fri, 20 Dec 2024 17:53:45 +0800 Subject: [PATCH 2/4] =?UTF-8?q?openapi=E5=85=A8=E9=87=8F=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/jeecg/config/shiro/ShiroConfig.java | 2 + .../controller/OpenApiAuthController.java | 112 +++++ .../openapi/controller/OpenApiController.java | 463 ++++++++++++++++++ .../controller/OpenApiIndexController.java | 26 + .../controller/OpenApiRecordController.java | 102 ++++ .../jeecg/modules/openapi/entity/OpenApi.java | 103 ++++ .../modules/openapi/entity/OpenApiAuth.java | 63 +++ .../modules/openapi/entity/OpenApiHeader.java | 51 ++ .../modules/openapi/entity/OpenApiParam.java | 50 ++ .../openapi/entity/OpenApiPermission.java | 55 +++ .../modules/openapi/entity/OpenApiRecord.java | 52 ++ .../modules/openapi/filter/ApiAuthFilter.java | 198 ++++++++ .../openapi/filter/ApiFilterConfig.java | 25 + .../openapi/generator/AKSKGenerator.java | 36 ++ .../openapi/generator/PathGenerator.java | 28 ++ .../openapi/mapper/OpenApiAuthMapper.java | 12 + .../openapi/mapper/OpenApiHeaderMapper.java | 12 + .../modules/openapi/mapper/OpenApiMapper.java | 9 + .../openapi/mapper/OpenApiParamMapper.java | 12 + .../mapper/OpenApiPermissionMapper.java | 12 + .../openapi/mapper/OpenApiRecordMapper.java | 12 + .../openapi/service/OpenApiAuthService.java | 11 + .../openapi/service/OpenApiHeaderService.java | 16 + .../openapi/service/OpenApiParamService.java | 15 + .../service/OpenApiPermissionService.java | 13 + .../openapi/service/OpenApiRecordService.java | 10 + .../openapi/service/OpenApiService.java | 8 + .../service/impl/OpenApiAuthServiceImpl.java | 19 + .../impl/OpenApiHeaderServiceImpl.java | 26 + .../service/impl/OpenApiParamServiceImpl.java | 27 + .../impl/OpenApiPermissionServiceImpl.java | 22 + .../impl/OpenApiRecordServiceImpl.java | 14 + .../service/impl/OpenApiServiceImpl.java | 16 + .../src/main/resources/application-dev.yml | 8 +- 34 files changed, 1636 insertions(+), 4 deletions(-) create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiAuthController.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiController.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiIndexController.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiRecordController.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApi.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiAuth.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiHeader.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiParam.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiPermission.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiRecord.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/filter/ApiAuthFilter.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/filter/ApiFilterConfig.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/generator/AKSKGenerator.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/generator/PathGenerator.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiAuthMapper.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiHeaderMapper.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiMapper.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiParamMapper.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiPermissionMapper.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiRecordMapper.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiAuthService.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiHeaderService.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiParamService.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiPermissionService.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiRecordService.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiService.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiAuthServiceImpl.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiHeaderServiceImpl.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiParamServiceImpl.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiPermissionServiceImpl.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiRecordServiceImpl.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiServiceImpl.java diff --git a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroConfig.java b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroConfig.java index 3b5e0e5df6..77b4faf03a 100644 --- a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroConfig.java +++ b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroConfig.java @@ -180,6 +180,8 @@ public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) { // 企业微信证书排除 filterChainDefinitionMap.put("/WW_verify*", "anon"); + filterChainDefinitionMap.put("/openapi/**", "anon"); + // 添加自己的过滤器并且取名为jwt Map filterMap = new HashMap(1); //如果cloudServer为空 则说明是单体 需要加载跨域配置【微服务跨域切换】 diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiAuthController.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiAuthController.java new file mode 100644 index 0000000000..66cf6a4e9f --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiAuthController.java @@ -0,0 +1,112 @@ +package org.jeecg.modules.openapi.controller; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import org.jeecg.common.api.vo.Result; +import org.jeecg.common.system.base.controller.JeecgController; +import org.jeecg.common.system.query.QueryGenerator; +import org.jeecg.modules.openapi.entity.OpenApiAuth; +import org.jeecg.modules.openapi.generator.AKSKGenerator; +import org.jeecg.modules.openapi.service.OpenApiAuthService; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; +import java.util.Arrays; + +/** + * @date 2024/12/10 9:54 + */ +@RestController +@RequestMapping("/openapi/auth") +public class OpenApiAuthController extends JeecgController { + + /** + * 分页列表查询 + * + * @param openApiAuth + * @param pageNo + * @param pageSize + * @param req + * @return + */ + @GetMapping(value = "/list") + public Result queryPageList(OpenApiAuth openApiAuth, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, + @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) { + QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(openApiAuth, req.getParameterMap()); + Page page = new Page<>(pageNo, pageSize); + IPage pageList = service.page(page, queryWrapper); + return Result.ok(pageList); + } + + /** + * 添加 + * + * @param openApiAuth + * @return + */ + @PostMapping(value = "/add") + public Result add(@RequestBody OpenApiAuth openApiAuth) { + service.save(openApiAuth); + return Result.ok("添加成功!"); + } + + /** + * 编辑 + * + * @param openApiAuth + * @return + */ + @PutMapping(value = "/edit") + public Result edit(@RequestBody OpenApiAuth openApiAuth) { + service.updateById(openApiAuth); + return Result.ok("修改成功!"); + + } + + /** + * 通过id删除 + * + * @param id + * @return + */ + @DeleteMapping(value = "/delete") + public Result delete(@RequestParam(name = "id", required = true) String id) { + service.removeById(id); + return Result.ok("删除成功!"); + } + + /** + * 批量删除 + * + * @param ids + * @return + */ + @DeleteMapping(value = "/deleteBatch") + public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) { + + this.service.removeByIds(Arrays.asList(ids.split(","))); + return Result.ok("批量删除成功!"); + } + + /** + * 通过id查询 + * + * @param id + * @return + */ + @GetMapping(value = "/queryById") + public Result queryById(@RequestParam(name = "id", required = true) String id) { + OpenApiAuth openApiAuth = service.getById(id); + return Result.ok(openApiAuth); + } + + /** + * 生成AKSK + * @return + */ + @GetMapping("genAKSK") + public Result genAKSK() { + return Result.ok(AKSKGenerator.genAKSKPair()); + } +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiController.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiController.java new file mode 100644 index 0000000000..099f9bc43f --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiController.java @@ -0,0 +1,463 @@ +package org.jeecg.modules.openapi.controller; + +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.fasterxml.classmate.ResolvedType; +import com.fasterxml.classmate.TypeBindings; +import com.fasterxml.classmate.TypeResolver; +import com.fasterxml.classmate.types.ResolvedObjectType; +import com.fasterxml.jackson.databind.type.ArrayType; +import com.fasterxml.jackson.databind.type.TypeFactory; +import com.google.common.collect.Lists; +import org.jeecg.common.api.vo.Result; +import org.jeecg.common.constant.CommonConstant; +import org.jeecg.common.system.base.controller.JeecgController; +import org.jeecg.common.system.query.QueryGenerator; +import org.jeecg.modules.openapi.entity.OpenApi; +import org.jeecg.modules.openapi.entity.OpenApiHeader; +import org.jeecg.modules.openapi.entity.OpenApiParam; +import org.jeecg.modules.openapi.generator.PathGenerator; +import org.jeecg.modules.openapi.service.OpenApiHeaderService; +import org.jeecg.modules.openapi.service.OpenApiParamService; +import org.jeecg.modules.openapi.service.OpenApiService; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.util.CollectionUtils; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.client.RestTemplate; +import springfox.documentation.schema.*; +import springfox.documentation.service.*; +import springfox.documentation.spring.web.DocumentationCache; + +import javax.servlet.http.HttpServletRequest; +import java.util.*; + +/** + * @date 2024/12/10 9:11 + */ +@RestController +@RequestMapping("/openapi") +public class OpenApiController extends JeecgController { + + @Autowired + private RestTemplate restTemplate; + @Autowired + private OpenApiParamService openApiParamService; + @Autowired + private OpenApiHeaderService openApiHeaderService; + @Autowired + private DocumentationCache documentationCache; + @Autowired + private TypeResolver typeResolver; + + /** + * 分页列表查询 + * + * @param openApi + * @param pageNo + * @param pageSize + * @param req + * @return + */ + @GetMapping(value = "/list") + public Result queryPageList(OpenApi openApi, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, + @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) { + QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(openApi, req.getParameterMap()); + Page page = new Page<>(pageNo, pageSize); + IPage pageList = service.page(page, queryWrapper); + for (OpenApi api : pageList.getRecords()) { + api.setParams(openApiParamService.findByApiId(api.getId())); + api.setHeaders(openApiHeaderService.findByApiId(api.getId())); + } + return Result.ok(pageList); + } + + /** + * 添加 + * + * @param openApi + * @return + */ + @PostMapping(value = "/add") + public Result add(@RequestBody OpenApi openApi) { + if (service.save(openApi)) { + if (!CollectionUtils.isEmpty(openApi.getHeaders())) { + openApiHeaderService.saveBatch(openApi.getHeaders()); + } + + if (!CollectionUtils.isEmpty(openApi.getParams())) { + openApiParamService.saveBatch(openApi.getParams()); + } + } + + return Result.ok("添加成功!"); + } + + /** + * 编辑 + * + * @param openApi + * @return + */ + @PutMapping(value = "/edit") + public Result edit(@RequestBody OpenApi openApi) { + if (service.updateById(openApi)) { + openApiHeaderService.deleteByApiId(openApi.getId()); + openApiParamService.deleteByApiId(openApi.getId()); + + if (!CollectionUtils.isEmpty(openApi.getHeaders())) { + openApiHeaderService.saveBatch(openApi.getHeaders()); + } + + if (!CollectionUtils.isEmpty(openApi.getParams())) { + openApiParamService.saveBatch(openApi.getParams()); + } + } + return Result.ok("修改成功!"); + + } + + /** + * 通过id删除 + * + * @param id + * @return + */ + @DeleteMapping(value = "/delete") + public Result delete(@RequestParam(name = "id", required = true) String id) { + service.removeById(id); + return Result.ok("删除成功!"); + } + + /** + * 批量删除 + * + * @param ids + * @return + */ + @DeleteMapping(value = "/deleteBatch") + public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) { + + this.service.removeByIds(Arrays.asList(ids.split(","))); + return Result.ok("批量删除成功!"); + } + + /** + * 通过id查询 + * + * @param id + * @return + */ + @GetMapping(value = "/queryById") + public Result queryById(@RequestParam(name = "id", required = true) String id) { + OpenApi OpenApi = service.getById(id); + return Result.ok(OpenApi); + } + + /** + * 接口调用 + * @param path + * @return + */ + @RequestMapping(value = "/call/{path}", method = {RequestMethod.GET,RequestMethod.POST}) + public Result call(@PathVariable String path, @RequestBody(required = false) String json, HttpServletRequest request) { + OpenApi openApi = service.findByPath(path); + if (Objects.isNull(openApi)) { + Map result = new HashMap<>(); + result.put("code", 404); + result.put("data", null); + return Result.error("失败", result); + } + List headers = openApiHeaderService.findByApiId(openApi.getId()); + + String url = openApi.getOriginUrl(); + String method = openApi.getRequestMethod(); + + HttpHeaders httpHeaders = new HttpHeaders(); + for (OpenApiHeader header : headers) { + httpHeaders.put(header.getKey(), Lists.newArrayList(request.getHeader(header.getKey()))); + } + + HttpEntity httpEntity = new HttpEntity<>(json, httpHeaders); + + return restTemplate.exchange(url, HttpMethod.resolve(method), httpEntity, Result.class, request.getParameterMap()).getBody(); + } + + @GetMapping("/call/add") + public void addSwagger() { + Documentation documentation = documentationCache.documentationByGroup("default"); + + List apis = new ArrayList<>(); + + Set produces = new HashSet<>(); + produces.add("application/json"); + Set consumes = new HashSet<>(); + consumes.add("application/json"); + + Set tags = new HashSet<>(); + Tag tag = new Tag("openapi", "openapi"); + tags.add("openapi"); + Set apiTags = new HashSet<>(); + apiTags.add(tag); + documentation.getTags().add(tag); + + Model resultModel = documentation.getApiListings().get("login-controller").get(0).getModels().get("接口返回对象«JSONObject»"); + + ModelRef stringModelRef = new ModelRef("string"); + + ResolvedType stringResolvedType = typeResolver.resolve(String.class); + + + HashMap propertyMap = new HashMap<>(); + ModelProperty modelProperty = new ModelProperty("abc", stringResolvedType, "java.lang.String", 0, true, false, false, false, "姓名", null, null, null, null, null, new ArrayList<>()); + propertyMap.put("abc", modelProperty); + ModelProperty modelProperty1 = new ModelProperty("bcd", stringResolvedType, "java.lang.String", 0, true, false, false, false, "姓名", null, null, null, null, null, new ArrayList<>()); + propertyMap.put("bcd", modelProperty1); + + ResolvedType mapResolvedType = typeResolver.resolve(HashMap.class); + + List subTypes = new ArrayList<>(); + subTypes.add(stringModelRef); + + Model bodyModel = new Model("path_body", "body", mapResolvedType, "java.util.HashMap", propertyMap, "请求体结构", "", "", subTypes, null, null); + ModelRef bodyRef = new ModelRef("bodyModel", "bodyModel", null, null, "path_body"); + + Set responseMessages = documentation.getApiListings().get("login-controller").get(0).getApis().get(2).getOperations().get(0).getResponseMessages(); + + List parameters = new ArrayList<>(); + // header-->请求参数的获取:@RequestHeader() + // query-->请求参数的获取:@RequestParam() + // path-->请求参数的获取:@PathVariable() + // body-->请求参数的获取:@RequestBody() + // form(不常用) + Parameter body = new Parameter("body", + "请求体", + "", + true, + false, + true, + bodyRef, + Optional.of(mapResolvedType), + null, + "body", + null, + false, + null, + null, + 2147483647, + null, + new HashMap<>(), + new ArrayList<>()); + + parameters.add(body); + + Parameter parameter = new Parameter("name", + "姓名", + "张三", + false, + false, + true, + stringModelRef, + Optional.empty(), + null, + "query", + null, + false, + null, + null, + 2147483647, + null, + new HashMap<>(), + new ArrayList<>()); + + parameters.add(parameter); + + ModelRef modelRef = new ModelRef(resultModel.getType().getTypeName(), + resultModel.getQualifiedType(), + null, + null, + false, + resultModel.getId()); + + Operation operation = new Operation(HttpMethod.resolve("GET"), + "模拟第一个openapi接口", + "模拟第一个openapi接口", + modelRef, + "abcUsingGET", + 0, tags, produces, consumes, + new LinkedHashSet<>(), + new ArrayList<>(), + parameters, + responseMessages, + "false", + false, + new ArrayList<>()); + + List operations = new ArrayList<>(); + operations.add(operation); + + ApiDescription apiDescription = new ApiDescription("openapi", + "/jeecg-boot/openapi/call/abc", "openapi", operations, false); + + List apiList = new ArrayList<>(); + apiList.add(apiDescription); + + Map responseModel = new HashMap<>(); + responseModel.put("接口返回对象«JSONObject»", resultModel); + responseModel.put("bodyModel", bodyModel); + + ApiListing api = new ApiListing("1.0", + "/", + "/openapi/call/abc", + produces, consumes, + "", new HashSet<>(), new ArrayList<>(), apiList, responseModel, "abc", 0, apiTags); + + apis.add(api); + + documentation.getApiListings().put("openapi", apis); + } + + /** + * 生成接口路径 + * @return + */ + @GetMapping("genPath") + public Result genPath() { + Result r = new Result(); + r.setSuccess(true); + r.setCode(CommonConstant.SC_OK_200); + r.setResult(PathGenerator.genPath()); + return r; + } + + public void resetOpenapiSwagger() { + List openapis = service.list(); + Documentation documentation = documentationCache.documentationByGroup("default"); + List apis = new ArrayList<>(); + + // --------------------- swagger common -------------- + + // http参数 + Set produces = new HashSet<>(); + produces.add("application/json"); + Set consumes = new HashSet<>(); + consumes.add("application/json"); + + // 标题栏 + Set tags = new HashSet<>(); + Tag tag = new Tag("openapi", "openapi"); + tags.add("openapi"); + Set apiTags = new HashSet<>(); + apiTags.add(tag); + documentation.getTags().add(tag); + ModelRef stringModelRef = new ModelRef("string", null, false); + + // 响应文档 + Model resultModel = documentation.getApiListings().get("login-controller").get(0).getModels().get("接口返回对象«JSONObject»"); + Set responseMessages = documentation.getApiListings().get("login-controller").get(0).getApis().get(2).getOperations().get(0).getResponseMessages(); + ModelRef modelRef = new ModelRef(resultModel.getType().getTypeName(), + resultModel.getQualifiedType(), + null, + null, + false, + resultModel.getId()); + // --------------------- swagger common -------------- + + for (OpenApi openapi : openapis) { + openapi.setParams(openApiParamService.findByApiId(openapi.getId())); + openapi.setHeaders(openApiHeaderService.findByApiId(openapi.getId())); + + // 参数,包含header\query\path\body\form 五类数据 + List parameters = new ArrayList<>(); + // header-->请求参数的获取:@RequestHeader() + // query-->请求参数的获取:@RequestParam() + // path-->请求参数的获取:@PathVariable() + // body-->请求参数的获取:@RequestBody() + // form(不常用) + for (OpenApiHeader openApiHeader : openapi.getHeaders()) { + Parameter parameter = new Parameter(openApiHeader.getKey(), + openApiHeader.getNote(), + openApiHeader.getDefaultValue(), + openApiHeader.getIsBlank() == 1, + false, + true, + stringModelRef, + Optional.empty(), + null, + "header", + null, + false, + null, + null, + 2147483647, + null, + new HashMap<>(), + new ArrayList<>()); + parameters.add(parameter); + } + + for (OpenApiParam openApiParam : openapi.getParams()) { + Parameter parameter = new Parameter(openApiParam.getKey(), + openApiParam.getNote(), + openApiParam.getDefaultValue(), + openApiParam.getIsBlank() == 1, + false, + true, + stringModelRef, + Optional.empty(), + null, + "query", + null, + false, + null, + null, + 2147483647, + null, + new HashMap<>(), + new ArrayList<>()); + + parameters.add(parameter); + } + + Operation operation = new Operation(HttpMethod.resolve("GET"), + "模拟第一个openapi接口", + "模拟第一个openapi接口", + modelRef, + "abcUsingGET", + 0, tags, produces, consumes, + new LinkedHashSet<>(), + new ArrayList<>(), + parameters, + responseMessages, + "false", + false, + new ArrayList<>()); + + List operations = new ArrayList<>(); + operations.add(operation); + + ApiDescription apiDescription = new ApiDescription("openapi", + "/jeecg-boot/openapi/call/abc", "openapi", operations, false); + + List apiList = new ArrayList<>(); + apiList.add(apiDescription); + + Map responseModel = new HashMap<>(); + responseModel.put("接口返回对象«JSONObject»", resultModel); + + ApiListing api = new ApiListing("1.0", + "/", + "/openapi/call/abc", + produces, consumes, + "", new HashSet<>(), new ArrayList<>(), apiList, responseModel, "abc", 0, apiTags); + + apis.add(api); + } + + documentation.getApiListings().put("openapi", apis); + } +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiIndexController.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiIndexController.java new file mode 100644 index 0000000000..36fcba8ab7 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiIndexController.java @@ -0,0 +1,26 @@ +package org.jeecg.modules.openapi.controller; + +import org.jeecg.common.api.vo.Result; +import org.jeecg.config.shiro.IgnoreAuth; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.HashMap; +import java.util.Map; + +/** + * @date 2024/12/20 14:04 + */ +@RestController +@RequestMapping("/openapi/demo") +public class OpenApiIndexController { + + @GetMapping("index") + @IgnoreAuth + public Result> index() { + Map result = new HashMap<>(); + result.put("first", "Hello World"); + return Result.ok(result); + } +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiRecordController.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiRecordController.java new file mode 100644 index 0000000000..72a8732ca0 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiRecordController.java @@ -0,0 +1,102 @@ +package org.jeecg.modules.openapi.controller; + +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import org.jeecg.common.api.vo.Result; +import org.jeecg.common.system.base.controller.JeecgController; +import org.jeecg.common.system.query.QueryGenerator; +import org.jeecg.modules.openapi.entity.OpenApiRecord; +import org.jeecg.modules.openapi.service.OpenApiRecordService; +import org.springframework.web.bind.annotation.*; + +import javax.servlet.http.HttpServletRequest; +import java.util.Arrays; + +/** + * @date 2024/12/10 9:57 + */ +@RestController +@RequestMapping("/openapi/record") +public class OpenApiRecordController extends JeecgController { + + /** + * 分页列表查询 + * + * @param openApiRecord + * @param pageNo + * @param pageSize + * @param req + * @return + */ + @GetMapping(value = "/list") + public Result queryPageList(OpenApiRecord openApiRecord, @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo, + @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize, HttpServletRequest req) { + QueryWrapper queryWrapper = QueryGenerator.initQueryWrapper(openApiRecord, req.getParameterMap()); + Page page = new Page<>(pageNo, pageSize); + IPage pageList = service.page(page, queryWrapper); + return Result.ok(pageList); + } + + /** + * 添加 + * + * @param openApiRecord + * @return + */ + @PostMapping(value = "/add") + public Result add(@RequestBody OpenApiRecord openApiRecord) { + service.save(openApiRecord); + return Result.ok("添加成功!"); + } + + /** + * 编辑 + * + * @param openApiRecord + * @return + */ + @PutMapping(value = "/edit") + public Result edit(@RequestBody OpenApiRecord openApiRecord) { + service.updateById(openApiRecord); + return Result.ok("修改成功!"); + + } + + /** + * 通过id删除 + * + * @param id + * @return + */ + @DeleteMapping(value = "/delete") + public Result delete(@RequestParam(name = "id", required = true) String id) { + service.removeById(id); + return Result.ok("删除成功!"); + } + + /** + * 批量删除 + * + * @param ids + * @return + */ + @DeleteMapping(value = "/deleteBatch") + public Result deleteBatch(@RequestParam(name = "ids", required = true) String ids) { + + this.service.removeByIds(Arrays.asList(ids.split(","))); + return Result.ok("批量删除成功!"); + } + + /** + * 通过id查询 + * + * @param id + * @return + */ + @GetMapping(value = "/queryById") + public Result queryById(@RequestParam(name = "id", required = true) String id) { + OpenApiRecord openApiRecord = service.getById(id); + return Result.ok(openApiRecord); + } +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApi.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApi.java new file mode 100644 index 0000000000..4eeebe9648 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApi.java @@ -0,0 +1,103 @@ +package org.jeecg.modules.openapi.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableField; +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableLogic; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +import java.io.Serializable; +import java.util.Date; +import java.util.List; + +/** + * 接口表 + */ +@Data +@EqualsAndHashCode(callSuper = false) +@Accessors(chain = true) +public class OpenApi implements Serializable { + + private static final long serialVersionUID = 1L; + + /** + * id + */ + @TableId(type = IdType.ASSIGN_ID) + private String id; + + /** + * 接口名称 + */ + private String name; + + /** + * 请求方式,如POST、GET + */ + private String requestMethod; + + /** + * 对外开放的相对接口路径 + */ + private String requestUrl; + + /** + * IP 黑名单 + */ + private String blackList; + + /** + * 请求头列表 + */ + @TableField(exist = false) + private List headers; + + /** + * 请求参数列表 + */ + @TableField(exist = false) + private List params; + + /** + * 目前仅支持json + */ + private String body; + + /** + * 原始接口路径 + */ + private String originUrl; + + /** + * 状态(1:正常 2:废弃 ) + */ + private Integer status; + + /** + * 删除状态(0,正常,1已删除) + */ + @TableLogic + private Integer delFlag; + + /** + * 创建人 + */ + private String createBy; + + /** + * 创建时间 + */ + private Date createTime; + + /** + * 更新人 + */ + private String updateBy; + + /** + * 更新时间 + */ + private Date updateTime; +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiAuth.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiAuth.java new file mode 100644 index 0000000000..cbf28d3e90 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiAuth.java @@ -0,0 +1,63 @@ +package org.jeecg.modules.openapi.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +import java.io.Serializable; +import java.util.Date; + +/** + * 权限表 + * @date 2024/12/10 9:38 + */ +@Data +@EqualsAndHashCode(callSuper = false) +@Accessors(chain = true) +public class OpenApiAuth implements Serializable { + + private static final long serialVersionUID = -5933153354153738498L; + + /** + * id + */ + @TableId(type = IdType.ASSIGN_ID) + private String id; + + /** + * 受权名称 + */ + private String name; + + /** + * access key + */ + private String ak; + + /** + * secret key + */ + private String sk; + + /** + * 创建人 + */ + private String createBy; + + /** + * 创建时间 + */ + private Date createTime; + + /** + * 更新人 + */ + private String updateBy; + + /** + * 更新时间 + */ + private Date updateTime; +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiHeader.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiHeader.java new file mode 100644 index 0000000000..8584b55bfc --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiHeader.java @@ -0,0 +1,51 @@ +package org.jeecg.modules.openapi.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +import java.io.Serializable; + +/** + * 请求头表 + * @date 2024/12/10 14:37 + */ +@Data +@EqualsAndHashCode(callSuper = false) +@Accessors(chain = true) +public class OpenApiHeader implements Serializable { + private static final long serialVersionUID = 5032708503120184683L; + + /** + * id + */ + @TableId(type = IdType.ASSIGN_ID) + private String id; + + /** + * 接口ID + */ + private String apiId; + + /** + * key + */ + private String key; + + /** + * 是否必填(0:否,1:是) + */ + private Integer isBlank; + + /** + * 默认值 + */ + private String defaultValue; + + /** + * 说明 + */ + private String note; +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiParam.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiParam.java new file mode 100644 index 0000000000..2d5ae98929 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiParam.java @@ -0,0 +1,50 @@ +package org.jeecg.modules.openapi.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +import java.io.Serializable; + +/** + * query部分参数表 + * @date 2024/12/10 14:37 + */ +@Data +@EqualsAndHashCode(callSuper = false) +@Accessors(chain = true) +public class OpenApiParam implements Serializable { + private static final long serialVersionUID = -6174831468578022357L; + /** + * id + */ + @TableId(type = IdType.ASSIGN_ID) + private String id; + + /** + * 接口ID + */ + private String apiId; + + /** + * key + */ + private String key; + + /** + * 是否必填(0:否,1:是) + */ + private Integer isBlank; + + /** + * 默认值 + */ + private String defaultValue; + + /** + * 说明 + */ + private String note; +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiPermission.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiPermission.java new file mode 100644 index 0000000000..661b14940d --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiPermission.java @@ -0,0 +1,55 @@ +package org.jeecg.modules.openapi.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +import java.io.Serializable; +import java.util.Date; + +/** + * + * @date 2024/12/19 17:41 + */ +@Data +@EqualsAndHashCode(callSuper = false) +@Accessors(chain = true) +public class OpenApiPermission implements Serializable { + /** + * id + */ + @TableId(type = IdType.ASSIGN_ID) + private String id; + + /** + * 接口ID + */ + private String apiId; + + /** + * 认证ID + */ + private String apiAuthId; + + /** + * 创建人 + */ + private String createBy; + + /** + * 创建时间 + */ + private Date createTime; + + /** + * 更新人 + */ + private String updateBy; + + /** + * 更新时间 + */ + private Date updateTime; +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiRecord.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiRecord.java new file mode 100644 index 0000000000..6cfc59b68a --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiRecord.java @@ -0,0 +1,52 @@ +package org.jeecg.modules.openapi.entity; + +import com.baomidou.mybatisplus.annotation.IdType; +import com.baomidou.mybatisplus.annotation.TableId; +import lombok.Data; +import lombok.EqualsAndHashCode; +import lombok.experimental.Accessors; + +import java.io.Serializable; +import java.util.Date; + +/** + * 调用记录表 + * @date 2024/12/10 9:41 + */ +@Data +@EqualsAndHashCode(callSuper = false) +@Accessors(chain = true) +public class OpenApiRecord implements Serializable { + private static final long serialVersionUID = -5870384488947863579L; + + /** + * id + */ + @TableId(type = IdType.ASSIGN_ID) + private String id; + + /** + * 接口ID + */ + private String apiId; + + /** + * 调用ID + */ + private String callAuthId; + + /** + * 调用时间 + */ + private Date callTime; + + /** + * 耗时 + */ + private Long usedTime; + + /** + * 响应时间 + */ + private Date responseTime; +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/filter/ApiAuthFilter.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/filter/ApiAuthFilter.java new file mode 100644 index 0000000000..e345a5060e --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/filter/ApiAuthFilter.java @@ -0,0 +1,198 @@ +package org.jeecg.modules.openapi.filter; + +import lombok.extern.slf4j.Slf4j; +import org.jeecg.common.exception.JeecgBootException; +import org.jeecg.modules.openapi.entity.OpenApi; +import org.jeecg.modules.openapi.entity.OpenApiAuth; +import org.jeecg.modules.openapi.entity.OpenApiPermission; +import org.jeecg.modules.openapi.entity.OpenApiRecord; +import org.jeecg.modules.openapi.service.OpenApiAuthService; +import org.jeecg.modules.openapi.service.OpenApiPermissionService; +import org.jeecg.modules.openapi.service.OpenApiRecordService; +import org.jeecg.modules.openapi.service.OpenApiService; +import org.springframework.util.StringUtils; +import org.springframework.web.context.WebApplicationContext; + +import javax.servlet.*; +import javax.servlet.http.HttpServletRequest; +import java.io.IOException; +import java.security.MessageDigest; +import java.util.Arrays; +import java.util.Date; +import java.util.List; + +/** + * @date 2024/12/19 16:55 + */ +@Slf4j +public class ApiAuthFilter implements Filter { + + private OpenApiRecordService openApiRecordService; + private OpenApiAuthService openApiAuthService; + private OpenApiPermissionService openApiPermissionService; + private OpenApiService openApiService; + + @Override + public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException { + long startTime = System.currentTimeMillis(); + Date callTime = new Date(); + + HttpServletRequest request = (HttpServletRequest)servletRequest; + String ip = request.getRemoteAddr(); + + String appkey = request.getHeader("appkey"); + String signature = request.getHeader("signature"); + String timestamp = request.getHeader("timestamp"); + + OpenApi openApi = findOpenApi(request); + + // IP 黑名单核验 + checkBlackList(openApi, ip); + + // 签名核验 + checkSignValid(appkey, signature, timestamp); + + OpenApiAuth openApiAuth = openApiAuthService.getByAppkey(appkey); + // 认证信息核验 + checkSignature(appkey, signature, timestamp, openApiAuth); + // 业务核验 + checkPermission(openApi, openApiAuth); + + filterChain.doFilter(servletRequest, servletResponse); + long endTime = System.currentTimeMillis(); + + OpenApiRecord record = new OpenApiRecord(); + record.setApiId(openApi.getId()); + record.setCallAuthId(openApiAuth.getId()); + record.setCallTime(callTime); + record.setUsedTime(endTime - startTime); + record.setResponseTime(new Date()); + openApiRecordService.save(record); + } + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + ServletContext servletContext = filterConfig.getServletContext(); + WebApplicationContext applicationContext = (WebApplicationContext)servletContext.getAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE); + this.openApiService = applicationContext.getBean(OpenApiService.class); + this.openApiRecordService = applicationContext.getBean(OpenApiRecordService.class); + this.openApiAuthService = applicationContext.getBean(OpenApiAuthService.class); + this.openApiPermissionService = applicationContext.getBean(OpenApiPermissionService.class); + } + + /** + * IP 黑名单核验 + * @param openApi + * @param ip + */ + protected void checkBlackList(OpenApi openApi, String ip) { + if (!StringUtils.hasText(openApi.getBlackList())) { + return; + } + + List blackList = Arrays.asList(openApi.getBlackList().split(",")); + if (blackList.contains(ip)) { + throw new JeecgBootException("目标接口限制IP[" + ip + "]进行访问,IP已记录,请停止访问"); + } + } + + /** + * 签名验证 + * @param appkey + * @param signature + * @param timestamp + * @return + */ + protected void checkSignValid(String appkey, String signature, String timestamp) { + if (!StringUtils.hasText(appkey)) { + throw new JeecgBootException("appkey为空"); + } + if (!StringUtils.hasText(signature)) { + throw new JeecgBootException("signature为空"); + } + if (!StringUtils.hasText(timestamp)) { + throw new JeecgBootException("timastamp时间戳为空"); + } + if (!timestamp.matches("[0-9]*")) { + throw new JeecgBootException("timastamp时间戳不合法"); + } + if (System.currentTimeMillis() - Long.parseLong(timestamp) > 5 * 60 * 1000) { + throw new JeecgBootException("signature签名已过期(超过五分钟)"); + } + } + + /** + * 认证信息核验 + * @param appKey + * @param signature + * @param timestamp + * @param openApiAuth + * @return + * @throws Exception + */ + protected void checkSignature(String appKey, String signature, String timestamp, OpenApiAuth openApiAuth) { + if(openApiAuth==null){ + throw new JeecgBootException("不存在认证信息"); + } + + if(!appKey.equals(openApiAuth.getAk())){ + throw new JeecgBootException("appkey错误"); + } + + if (!signature.equals(md5(appKey + openApiAuth.getSk() + timestamp))) { + throw new JeecgBootException("signature签名错误"); + } + } + + protected void checkPermission(OpenApi openApi, OpenApiAuth openApiAuth) { + List permissionList = openApiPermissionService.findByAuthId(openApiAuth.getId()); + + boolean hasPermission = false; + for (OpenApiPermission permission : permissionList) { + if (permission.getApiId().equals(openApi.getId())) { + hasPermission = true; + break; + } + } + + if (!hasPermission) { + throw new JeecgBootException("该appKey未授权当前接口"); + } + } + + /** + * @return String 返回类型 + * @Title: MD5 + * @Description: 【MD5加密】 + */ + protected static String md5(String sourceStr) { + String result = ""; + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + md.update(sourceStr.getBytes("utf-8")); + byte[] hash = md.digest(); + int i; + StringBuffer buf = new StringBuffer(32); + for (int offset = 0; offset < hash.length; offset++) { + i = hash[offset]; + if (i < 0) { + i += 256; + } + if (i < 16) { + buf.append("0"); + } + buf.append(Integer.toHexString(i)); + } + result = buf.toString(); + } catch (Exception e) { + log.error("sign签名错误", e); + } + return result; + } + + protected OpenApi findOpenApi(HttpServletRequest request) { + String uri = request.getRequestURI(); + String path = uri.substring(uri.lastIndexOf("/") + 1); + return openApiService.findByPath(path); + } +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/filter/ApiFilterConfig.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/filter/ApiFilterConfig.java new file mode 100644 index 0000000000..bac823fe12 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/filter/ApiFilterConfig.java @@ -0,0 +1,25 @@ +package org.jeecg.modules.openapi.filter; + +import org.springframework.boot.web.servlet.FilterRegistrationBean; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +/** + * @date 2024/12/19 17:09 + */ +@Configuration +public class ApiFilterConfig { + + /** + * + * @Description: 【注册api加密过滤器】 + */ + @Bean + public FilterRegistrationBean authFilter() { + FilterRegistrationBean registration = new FilterRegistrationBean<>(); + registration.setFilter(new ApiAuthFilter()); + registration.setName("apiAuthFilter"); + registration.addUrlPatterns("/openapi/call/*"); + return registration; + } +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/generator/AKSKGenerator.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/generator/AKSKGenerator.java new file mode 100644 index 0000000000..c93069c09d --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/generator/AKSKGenerator.java @@ -0,0 +1,36 @@ +package org.jeecg.modules.openapi.generator; + +import java.security.SecureRandom; + +/** + * AK/SK生成器 + */ +public class AKSKGenerator { + private static final String CHAR_POOL = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + private static final int AK_LENGTH = 16; // Adjust as per requirements + private static final int SK_LENGTH = 32; + + public static String[] genAKSKPair() { + return new String[]{genAK(), genSK()}; + } + + public static String genAK() { + return "ak-" + generateRandomString(AK_LENGTH); + } + + public static String genSK() { + return generateRandomString(SK_LENGTH); + } + + + private static String generateRandomString(int length) { + SecureRandom random = new SecureRandom(); + StringBuilder sb = new StringBuilder(length); + + for (int i = 0; i < length; i++) { + sb.append(CHAR_POOL.charAt(random.nextInt(CHAR_POOL.length()))); + } + + return sb.toString(); + } +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/generator/PathGenerator.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/generator/PathGenerator.java new file mode 100644 index 0000000000..eaf5e5327a --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/generator/PathGenerator.java @@ -0,0 +1,28 @@ +package org.jeecg.modules.openapi.generator; + +import lombok.experimental.UtilityClass; + +import java.util.Random; + +/** + * @date 2024/12/10 10:00 + */ +@UtilityClass +public class PathGenerator { + + // Base62字符集 + private static final String BASE62 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; + + /** + * 生成随机路径 + * @return + */ + public static String genPath() { + StringBuilder result = new StringBuilder(); + Random random = new Random(); + for (int i=0; i<8; i++) { + result.append(BASE62.charAt(random.nextInt(62))); + } + return result.toString(); + } +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiAuthMapper.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiAuthMapper.java new file mode 100644 index 0000000000..4ec70f1327 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiAuthMapper.java @@ -0,0 +1,12 @@ +package org.jeecg.modules.openapi.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; +import org.jeecg.modules.openapi.entity.OpenApiAuth; + +/** + * @date 2024/12/10 9:49 + */ +@Mapper +public interface OpenApiAuthMapper extends BaseMapper { +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiHeaderMapper.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiHeaderMapper.java new file mode 100644 index 0000000000..3a1edddad0 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiHeaderMapper.java @@ -0,0 +1,12 @@ +package org.jeecg.modules.openapi.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; +import org.jeecg.modules.openapi.entity.OpenApiHeader; + +/** + * @date 2024/12/10 14:47 + */ +@Mapper +public interface OpenApiHeaderMapper extends BaseMapper { +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiMapper.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiMapper.java new file mode 100644 index 0000000000..f9a4b67585 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiMapper.java @@ -0,0 +1,9 @@ +package org.jeecg.modules.openapi.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; +import org.jeecg.modules.openapi.entity.OpenApi; + +@Mapper +public interface OpenApiMapper extends BaseMapper { +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiParamMapper.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiParamMapper.java new file mode 100644 index 0000000000..a993e379b2 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiParamMapper.java @@ -0,0 +1,12 @@ +package org.jeecg.modules.openapi.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; +import org.jeecg.modules.openapi.entity.OpenApiParam; + +/** + * @date 2024/12/10 14:48 + */ +@Mapper +public interface OpenApiParamMapper extends BaseMapper { +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiPermissionMapper.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiPermissionMapper.java new file mode 100644 index 0000000000..18f246c72a --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiPermissionMapper.java @@ -0,0 +1,12 @@ +package org.jeecg.modules.openapi.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; +import org.jeecg.modules.openapi.entity.OpenApiPermission; + +/** + * @date 2024/12/19 17:43 + */ +@Mapper +public interface OpenApiPermissionMapper extends BaseMapper { +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiRecordMapper.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiRecordMapper.java new file mode 100644 index 0000000000..0e72128f1e --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/mapper/OpenApiRecordMapper.java @@ -0,0 +1,12 @@ +package org.jeecg.modules.openapi.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import org.apache.ibatis.annotations.Mapper; +import org.jeecg.modules.openapi.entity.OpenApiRecord; + +/** + * @date 2024/12/10 9:50 + */ +@Mapper +public interface OpenApiRecordMapper extends BaseMapper { +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiAuthService.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiAuthService.java new file mode 100644 index 0000000000..011513f35d --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiAuthService.java @@ -0,0 +1,11 @@ +package org.jeecg.modules.openapi.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import org.jeecg.modules.openapi.entity.OpenApiAuth; + +/** + * @date 2024/12/10 9:50 + */ +public interface OpenApiAuthService extends IService { + OpenApiAuth getByAppkey(String appkey); +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiHeaderService.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiHeaderService.java new file mode 100644 index 0000000000..4a50ea231c --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiHeaderService.java @@ -0,0 +1,16 @@ +package org.jeecg.modules.openapi.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import org.jeecg.modules.openapi.entity.OpenApiHeader; + +import java.util.List; + +/** + * @date 2024/12/10 14:48 + */ +public interface OpenApiHeaderService extends IService { + + boolean deleteByApiId(String apiId); + + List findByApiId(String apiId); +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiParamService.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiParamService.java new file mode 100644 index 0000000000..6a1bfbcac4 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiParamService.java @@ -0,0 +1,15 @@ +package org.jeecg.modules.openapi.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import org.jeecg.modules.openapi.entity.OpenApiParam; + +import java.util.List; + +/** + * @date 2024/12/10 14:49 + */ +public interface OpenApiParamService extends IService { + boolean deleteByApiId(String apiId); + + List findByApiId(String apiId); +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiPermissionService.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiPermissionService.java new file mode 100644 index 0000000000..af541639ef --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiPermissionService.java @@ -0,0 +1,13 @@ +package org.jeecg.modules.openapi.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import org.jeecg.modules.openapi.entity.OpenApiPermission; + +import java.util.List; + +/** + * @date 2024/12/19 17:44 + */ +public interface OpenApiPermissionService extends IService { + List findByAuthId(String authId); +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiRecordService.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiRecordService.java new file mode 100644 index 0000000000..8b4026f6c1 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiRecordService.java @@ -0,0 +1,10 @@ +package org.jeecg.modules.openapi.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import org.jeecg.modules.openapi.entity.OpenApiRecord; + +/** + * @date 2024/12/10 9:51 + */ +public interface OpenApiRecordService extends IService { +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiService.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiService.java new file mode 100644 index 0000000000..8b58889f74 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/OpenApiService.java @@ -0,0 +1,8 @@ +package org.jeecg.modules.openapi.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import org.jeecg.modules.openapi.entity.OpenApi; + +public interface OpenApiService extends IService { + OpenApi findByPath(String path); +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiAuthServiceImpl.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiAuthServiceImpl.java new file mode 100644 index 0000000000..549994a731 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiAuthServiceImpl.java @@ -0,0 +1,19 @@ +package org.jeecg.modules.openapi.service.impl; + +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.jeecg.modules.openapi.entity.OpenApiAuth; +import org.jeecg.modules.openapi.mapper.OpenApiAuthMapper; +import org.jeecg.modules.openapi.service.OpenApiAuthService; +import org.springframework.stereotype.Service; + +/** + * @date 2024/12/10 9:51 + */ +@Service +public class OpenApiAuthServiceImpl extends ServiceImpl implements OpenApiAuthService { + @Override + public OpenApiAuth getByAppkey(String appkey) { + return baseMapper.selectOne(Wrappers.lambdaUpdate(OpenApiAuth.class).eq(OpenApiAuth::getAk, appkey), false); + } +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiHeaderServiceImpl.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiHeaderServiceImpl.java new file mode 100644 index 0000000000..d0ad6c4f5e --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiHeaderServiceImpl.java @@ -0,0 +1,26 @@ +package org.jeecg.modules.openapi.service.impl; + +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.jeecg.modules.openapi.entity.OpenApiHeader; +import org.jeecg.modules.openapi.mapper.OpenApiHeaderMapper; +import org.jeecg.modules.openapi.service.OpenApiHeaderService; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * @date 2024/12/10 14:49 + */ +@Service +public class OpenApiHeaderServiceImpl extends ServiceImpl implements OpenApiHeaderService { + @Override + public boolean deleteByApiId(String apiId) { + return baseMapper.delete(Wrappers.lambdaUpdate(OpenApiHeader.class).eq(OpenApiHeader::getApiId, apiId)) > 0; + } + + @Override + public List findByApiId(String apiId) { + return baseMapper.selectList(Wrappers.lambdaQuery(OpenApiHeader.class).eq(OpenApiHeader::getApiId, apiId)); + } +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiParamServiceImpl.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiParamServiceImpl.java new file mode 100644 index 0000000000..49daa6ab1b --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiParamServiceImpl.java @@ -0,0 +1,27 @@ +package org.jeecg.modules.openapi.service.impl; + +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.jeecg.modules.openapi.entity.OpenApiParam; +import org.jeecg.modules.openapi.mapper.OpenApiParamMapper; +import org.jeecg.modules.openapi.service.OpenApiParamService; +import org.springframework.stereotype.Service; + +import java.util.List; + +/** + * @date 2024/12/10 14:50 + */ +@Service +public class OpenApiParamServiceImpl extends ServiceImpl implements OpenApiParamService { + @Override + public boolean deleteByApiId(String apiId) { + return baseMapper.delete(Wrappers.lambdaUpdate(OpenApiParam.class).eq(OpenApiParam::getApiId, apiId)) > 0; + + } + + @Override + public List findByApiId(String apiId) { + return baseMapper.selectList(Wrappers.lambdaQuery(OpenApiParam.class).eq(OpenApiParam::getApiId, apiId)); + } +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiPermissionServiceImpl.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiPermissionServiceImpl.java new file mode 100644 index 0000000000..f18cba34cf --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiPermissionServiceImpl.java @@ -0,0 +1,22 @@ +package org.jeecg.modules.openapi.service.impl; + +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.jeecg.modules.openapi.entity.OpenApiPermission; +import org.jeecg.modules.openapi.mapper.OpenApiPermissionMapper; +import org.jeecg.modules.openapi.service.OpenApiPermissionService; +import org.springframework.stereotype.Service; + +import java.util.Collections; +import java.util.List; + +/** + * @date 2024/12/19 17:44 + */ +@Service +public class OpenApiPermissionServiceImpl extends ServiceImpl implements OpenApiPermissionService { + @Override + public List findByAuthId(String authId) { + return baseMapper.selectList(Wrappers.lambdaQuery(OpenApiPermission.class).eq(OpenApiPermission::getApiAuthId, authId)); + } +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiRecordServiceImpl.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiRecordServiceImpl.java new file mode 100644 index 0000000000..017794cf5a --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiRecordServiceImpl.java @@ -0,0 +1,14 @@ +package org.jeecg.modules.openapi.service.impl; + +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.jeecg.modules.openapi.entity.OpenApiRecord; +import org.jeecg.modules.openapi.mapper.OpenApiRecordMapper; +import org.jeecg.modules.openapi.service.OpenApiRecordService; +import org.springframework.stereotype.Service; + +/** + * @date 2024/12/10 9:53 + */ +@Service +public class OpenApiRecordServiceImpl extends ServiceImpl implements OpenApiRecordService { +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiServiceImpl.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiServiceImpl.java new file mode 100644 index 0000000000..7c72d528c0 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/service/impl/OpenApiServiceImpl.java @@ -0,0 +1,16 @@ +package org.jeecg.modules.openapi.service.impl; + +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.jeecg.modules.openapi.entity.OpenApi; +import org.jeecg.modules.openapi.mapper.OpenApiMapper; +import org.jeecg.modules.openapi.service.OpenApiService; +import org.springframework.stereotype.Service; + +@Service +public class OpenApiServiceImpl extends ServiceImpl implements OpenApiService { + @Override + public OpenApi findByPath(String path) { + return baseMapper.selectOne(Wrappers.lambdaQuery(OpenApi.class).eq(OpenApi::getRequestUrl, path), false); + } +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-start/src/main/resources/application-dev.yml b/jeecg-boot/jeecg-module-system/jeecg-system-start/src/main/resources/application-dev.yml index 34c7a54f3f..6edbbb4212 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-start/src/main/resources/application-dev.yml +++ b/jeecg-boot/jeecg-module-system/jeecg-system-start/src/main/resources/application-dev.yml @@ -163,9 +163,9 @@ spring: slow-sql-millis: 5000 datasource: master: - url: jdbc:mysql://127.0.0.1:3306/jeecg-boot?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai + url: jdbc:mysql://172.16.10.227:3306/jeecg-boot?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai username: root - password: root + password: root@2023 driver-class-name: com.mysql.cj.jdbc.Driver # 多数据源配置 #multi-datasource1: @@ -176,9 +176,9 @@ spring: #redis 配置 redis: database: 0 - host: 127.0.0.1 + host: 172.16.10.227 port: 6379 - password: '' + password: 'Whdsj@redis0523' #mybatis plus 设置 mybatis-plus: mapper-locations: classpath*:org/jeecg/**/xml/*Mapper.xml From ee382c0a5baa5c322e61b4e2113b04011fc1d2b7 Mon Sep 17 00:00:00 2001 From: EightMonth Date: Sat, 21 Dec 2024 23:50:07 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E5=85=A8=E6=B5=81=E7=A8=8B=E8=B7=91?= =?UTF-8?q?=E9=80=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../openapi/controller/OpenApiController.java | 15 +++++--------- .../OpenApiPermissionController.java | 20 +++++++++++++++++++ .../modules/openapi/entity/OpenApiHeader.java | 4 ++-- .../modules/openapi/entity/OpenApiParam.java | 4 ++-- .../modules/openapi/filter/ApiAuthFilter.java | 6 ++++++ .../jeecg/modules/openapi/sql/open_api.sql | 1 + .../modules/openapi/sql/open_api_auth.sql | 1 + .../modules/openapi/sql/open_api_header.sql | 0 .../modules/openapi/sql/open_api_param.sql | 0 .../openapi/sql/open_api_permission.sql | 1 + .../modules/openapi/sql/open_api_record.sql | 4 ++++ .../src/main/resources/application-dev.yml | 8 ++++---- 12 files changed, 46 insertions(+), 18 deletions(-) create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiPermissionController.java create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api.sql create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_auth.sql create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_header.sql create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_param.sql create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_permission.sql create mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_record.sql diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiController.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiController.java index 099f9bc43f..3ce3e2af0d 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiController.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiController.java @@ -1,15 +1,10 @@ package org.jeecg.modules.openapi.controller; -import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.fasterxml.classmate.ResolvedType; -import com.fasterxml.classmate.TypeBindings; import com.fasterxml.classmate.TypeResolver; -import com.fasterxml.classmate.types.ResolvedObjectType; -import com.fasterxml.jackson.databind.type.ArrayType; -import com.fasterxml.jackson.databind.type.TypeFactory; import com.google.common.collect.Lists; import org.jeecg.common.api.vo.Result; import org.jeecg.common.constant.CommonConstant; @@ -179,7 +174,7 @@ public Result call(@PathVariable String path, @RequestBody(required = false) HttpHeaders httpHeaders = new HttpHeaders(); for (OpenApiHeader header : headers) { - httpHeaders.put(header.getKey(), Lists.newArrayList(request.getHeader(header.getKey()))); + httpHeaders.put(header.getHeaderKey(), Lists.newArrayList(request.getHeader(header.getHeaderKey()))); } HttpEntity httpEntity = new HttpEntity<>(json, httpHeaders); @@ -379,10 +374,10 @@ public void resetOpenapiSwagger() { // body-->请求参数的获取:@RequestBody() // form(不常用) for (OpenApiHeader openApiHeader : openapi.getHeaders()) { - Parameter parameter = new Parameter(openApiHeader.getKey(), + Parameter parameter = new Parameter(openApiHeader.getHeaderKey(), openApiHeader.getNote(), openApiHeader.getDefaultValue(), - openApiHeader.getIsBlank() == 1, + openApiHeader.getRequired() == 1, false, true, stringModelRef, @@ -401,10 +396,10 @@ public void resetOpenapiSwagger() { } for (OpenApiParam openApiParam : openapi.getParams()) { - Parameter parameter = new Parameter(openApiParam.getKey(), + Parameter parameter = new Parameter(openApiParam.getParamKey(), openApiParam.getNote(), openApiParam.getDefaultValue(), - openApiParam.getIsBlank() == 1, + openApiParam.getRequired() == 1, false, true, stringModelRef, diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiPermissionController.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiPermissionController.java new file mode 100644 index 0000000000..727a8d0f58 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiPermissionController.java @@ -0,0 +1,20 @@ +package org.jeecg.modules.openapi.controller; + +import org.jeecg.common.api.vo.Result; +import org.jeecg.common.system.base.controller.JeecgController; +import org.jeecg.modules.openapi.entity.OpenApiPermission; +import org.jeecg.modules.openapi.service.OpenApiPermissionService; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("/openapi/permission") +public class OpenApiPermissionController extends JeecgController { + + @PostMapping("add") + public Result add(@RequestBody OpenApiPermission openApiPermission) { + return Result.ok(service.save(openApiPermission)); + } +} diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiHeader.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiHeader.java index 8584b55bfc..92393e54f1 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiHeader.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiHeader.java @@ -32,12 +32,12 @@ public class OpenApiHeader implements Serializable { /** * key */ - private String key; + private String headerKey; /** * 是否必填(0:否,1:是) */ - private Integer isBlank; + private Integer required; /** * 默认值 diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiParam.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiParam.java index 2d5ae98929..d2839602e3 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiParam.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiParam.java @@ -31,12 +31,12 @@ public class OpenApiParam implements Serializable { /** * key */ - private String key; + private String paramKey; /** * 是否必填(0:否,1:是) */ - private Integer isBlank; + private Integer required; /** * 默认值 diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/filter/ApiAuthFilter.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/filter/ApiAuthFilter.java index e345a5060e..88d65e364a 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/filter/ApiAuthFilter.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/filter/ApiAuthFilter.java @@ -195,4 +195,10 @@ protected OpenApi findOpenApi(HttpServletRequest request) { String path = uri.substring(uri.lastIndexOf("/") + 1); return openApiService.findByPath(path); } + + public static void main(String[] args) { + long timestamp = System.currentTimeMillis(); + System.out.println("timestamp:" + timestamp); + System.out.println("signature:" + md5("ak-eAU25mrMxhtaZsyS" + "rjxMqB6YyUXpSHAz4DCIz8vZ5aozQQiV" + timestamp)); + } } diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api.sql b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api.sql new file mode 100644 index 0000000000..3de4e3ed6a --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api.sql @@ -0,0 +1 @@ +INSERT INTO `open_api` (`id`, `name`, `request_method`, `request_url`, `black_list`, `body`, `origin_url`, `status`, `del_flag`, `create_by`, `create_time`, `update_by`, `update_time`) VALUES ('1870486348755324929', '示例接口', 'GET', 'UrTk8CTk', NULL, NULL, 'http://localhost:8080/jeecg-boot/openapi/demo/index', 1, 0, NULL, '2024-12-21 23:07:52', NULL, NULL); diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_auth.sql b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_auth.sql new file mode 100644 index 0000000000..50d4e0647d --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_auth.sql @@ -0,0 +1 @@ +INSERT INTO `open_api_auth` (`id`, `name`, `ak`, `sk`, `create_by`, `create_time`, `update_by`, `update_time`) VALUES ('1870484839812517890', 'openuser', 'ak-eAU25mrMxhtaZsyS', 'rjxMqB6YyUXpSHAz4DCIz8vZ5aozQQiV', NULL, '2024-12-21 23:01:52', NULL, NULL); diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_header.sql b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_header.sql new file mode 100644 index 0000000000..e69de29bb2 diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_param.sql b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_param.sql new file mode 100644 index 0000000000..e69de29bb2 diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_permission.sql b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_permission.sql new file mode 100644 index 0000000000..a993e02f27 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_permission.sql @@ -0,0 +1 @@ +INSERT INTO `open_api_permission` (`id`, `api_id`, `api_auth_id`, `create_by`, `create_time`, `update_by`, `update_time`) VALUES ('1870491556923179010', '1870486348755324929', '1870484839812517890', NULL, '2024-12-21 23:28:34', NULL, NULL); diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_record.sql b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_record.sql new file mode 100644 index 0000000000..8b5f2e0694 --- /dev/null +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_record.sql @@ -0,0 +1,4 @@ +INSERT INTO `open_api_record` (`id`, `api_id`, `call_auth_id`, `call_time`, `used_time`, `response_time`) VALUES ('1870491654130368513', '1870486348755324929', '1870484839812517890', '2024-12-21 23:28:57', 184, '2024-12-21 23:28:57'); +INSERT INTO `open_api_record` (`id`, `api_id`, `call_auth_id`, `call_time`, `used_time`, `response_time`) VALUES ('1870492694183542786', '1870486348755324929', '1870484839812517890', '2024-12-21 23:31:04', 120717, '2024-12-21 23:33:05'); +INSERT INTO `open_api_record` (`id`, `api_id`, `call_auth_id`, `call_time`, `used_time`, `response_time`) VALUES ('1870493301040680961', '1870486348755324929', '1870484839812517890', '2024-12-21 23:35:26', 3865, '2024-12-21 23:35:30'); +INSERT INTO `open_api_record` (`id`, `api_id`, `call_auth_id`, `call_time`, `used_time`, `response_time`) VALUES ('1870493334939045889', '1870486348755324929', '1870484839812517890', '2024-12-21 23:35:38', 82, '2024-12-21 23:35:38'); diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-start/src/main/resources/application-dev.yml b/jeecg-boot/jeecg-module-system/jeecg-system-start/src/main/resources/application-dev.yml index 6edbbb4212..1f156d3611 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-start/src/main/resources/application-dev.yml +++ b/jeecg-boot/jeecg-module-system/jeecg-system-start/src/main/resources/application-dev.yml @@ -163,9 +163,9 @@ spring: slow-sql-millis: 5000 datasource: master: - url: jdbc:mysql://172.16.10.227:3306/jeecg-boot?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai + url: jdbc:mysql://127.0.0.1:3306/jeecg-boot?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai username: root - password: root@2023 + password: root driver-class-name: com.mysql.cj.jdbc.Driver # 多数据源配置 #multi-datasource1: @@ -176,9 +176,9 @@ spring: #redis 配置 redis: database: 0 - host: 172.16.10.227 + host: 127.0.0.1 port: 6379 - password: 'Whdsj@redis0523' + password: #mybatis plus 设置 mybatis-plus: mapper-locations: classpath*:org/jeecg/**/xml/*Mapper.xml From 2b0bedd2637ae1487c347e74e1d98151b41324c2 Mon Sep 17 00:00:00 2001 From: EightMonth Date: Fri, 27 Dec 2024 14:23:01 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E5=AE=8C=E5=96=84openapi=EF=BC=8C=E7=A7=BB?= =?UTF-8?q?=E9=99=A4=E8=BF=87=E6=9C=9Fsql?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../org/jeecg/config/shiro/ShiroConfig.java | 2 +- .../openapi/controller/OpenApiController.java | 47 ++++++++++++++----- .../modules/openapi/entity/OpenApiAuth.java | 5 ++ .../jeecg/modules/openapi/sql/open_api.sql | 1 - .../modules/openapi/sql/open_api_auth.sql | 1 - .../modules/openapi/sql/open_api_header.sql | 0 .../modules/openapi/sql/open_api_param.sql | 0 .../openapi/sql/open_api_permission.sql | 1 - .../modules/openapi/sql/open_api_record.sql | 4 -- 9 files changed, 41 insertions(+), 20 deletions(-) delete mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api.sql delete mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_auth.sql delete mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_header.sql delete mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_param.sql delete mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_permission.sql delete mode 100644 jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_record.sql diff --git a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroConfig.java b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroConfig.java index 77b4faf03a..02d1fb9a8a 100644 --- a/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroConfig.java +++ b/jeecg-boot/jeecg-boot-base-core/src/main/java/org/jeecg/config/shiro/ShiroConfig.java @@ -180,7 +180,7 @@ public ShiroFilterFactoryBean shiroFilter(SecurityManager securityManager) { // 企业微信证书排除 filterChainDefinitionMap.put("/WW_verify*", "anon"); - filterChainDefinitionMap.put("/openapi/**", "anon"); + filterChainDefinitionMap.put("/openapi/call/**", "anon"); // 添加自己的过滤器并且取名为jwt Map filterMap = new HashMap(1); diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiController.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiController.java index 3ce3e2af0d..4d8fcbc2b4 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiController.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/controller/OpenApiController.java @@ -10,21 +10,30 @@ import org.jeecg.common.constant.CommonConstant; import org.jeecg.common.system.base.controller.JeecgController; import org.jeecg.common.system.query.QueryGenerator; +import org.jeecg.common.system.util.JwtUtil; import org.jeecg.modules.openapi.entity.OpenApi; +import org.jeecg.modules.openapi.entity.OpenApiAuth; import org.jeecg.modules.openapi.entity.OpenApiHeader; import org.jeecg.modules.openapi.entity.OpenApiParam; import org.jeecg.modules.openapi.generator.PathGenerator; +import org.jeecg.modules.openapi.service.OpenApiAuthService; import org.jeecg.modules.openapi.service.OpenApiHeaderService; import org.jeecg.modules.openapi.service.OpenApiParamService; import org.jeecg.modules.openapi.service.OpenApiService; +import org.jeecg.modules.system.entity.SysUser; +import org.jeecg.modules.system.service.ISysUserService; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.CommandLineRunner; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.util.CollectionUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.client.RestTemplate; -import springfox.documentation.schema.*; +import springfox.documentation.schema.Model; +import springfox.documentation.schema.ModelProperty; +import springfox.documentation.schema.ModelRef; +import springfox.documentation.schema.ModelReference; import springfox.documentation.service.*; import springfox.documentation.spring.web.DocumentationCache; @@ -36,7 +45,7 @@ */ @RestController @RequestMapping("/openapi") -public class OpenApiController extends JeecgController { +public class OpenApiController extends JeecgController implements CommandLineRunner { @Autowired private RestTemplate restTemplate; @@ -48,6 +57,10 @@ public class OpenApiController extends JeecgController private DocumentationCache documentationCache; @Autowired private TypeResolver typeResolver; + @Autowired + private ISysUserService sysUserService; + @Autowired + private OpenApiAuthService openApiAuthService; /** * 分页列表查询 @@ -177,6 +190,12 @@ public Result call(@PathVariable String path, @RequestBody(required = false) httpHeaders.put(header.getHeaderKey(), Lists.newArrayList(request.getHeader(header.getHeaderKey()))); } + String appkey = request.getHeader("appkey"); + OpenApiAuth openApiAuth = openApiAuthService.getByAppkey(appkey); + SysUser systemUser = sysUserService.getById(openApiAuth.getSystemUserId()); + String token = JwtUtil.sign(systemUser.getUsername(), systemUser.getPassword()); + httpHeaders.put("X-Access-Token", Lists.newArrayList(token)); + HttpEntity httpEntity = new HttpEntity<>(json, httpHeaders); return restTemplate.exchange(url, HttpMethod.resolve(method), httpEntity, Result.class, request.getParameterMap()).getBody(); @@ -218,8 +237,8 @@ public void addSwagger() { List subTypes = new ArrayList<>(); subTypes.add(stringModelRef); - Model bodyModel = new Model("path_body", "body", mapResolvedType, "java.util.HashMap", propertyMap, "请求体结构", "", "", subTypes, null, null); - ModelRef bodyRef = new ModelRef("bodyModel", "bodyModel", null, null, "path_body"); + Model bodyModel = new Model("path_body", "bodyModel", mapResolvedType, "java.util.HashMap", propertyMap, "请求体结构", "", "", subTypes, null, null); + ModelRef bodyRef = new ModelRef("bodyModel", "path_body", null, null,true, "path_body"); Set responseMessages = documentation.getApiListings().get("login-controller").get(0).getApis().get(2).getOperations().get(0).getResponseMessages(); @@ -229,7 +248,7 @@ public void addSwagger() { // path-->请求参数的获取:@PathVariable() // body-->请求参数的获取:@RequestBody() // form(不常用) - Parameter body = new Parameter("body", + Parameter body = new Parameter("bodyModel", "请求体", "", true, @@ -329,7 +348,11 @@ public Result genPath() { return r; } - public void resetOpenapiSwagger() { + /** + * resetOpenapiSwagger + */ + @Override + public void run(String... args) throws Exception { List openapis = service.list(); Documentation documentation = documentationCache.documentationByGroup("default"); List apis = new ArrayList<>(); @@ -418,11 +441,11 @@ public void resetOpenapiSwagger() { parameters.add(parameter); } - Operation operation = new Operation(HttpMethod.resolve("GET"), - "模拟第一个openapi接口", - "模拟第一个openapi接口", + Operation operation = new Operation(HttpMethod.resolve(openapi.getRequestMethod()), + openapi.getName(), + openapi.getName(), modelRef, - "abcUsingGET", + openapi.getName()+"Using"+openapi.getRequestMethod(), 0, tags, produces, consumes, new LinkedHashSet<>(), new ArrayList<>(), @@ -436,7 +459,7 @@ public void resetOpenapiSwagger() { operations.add(operation); ApiDescription apiDescription = new ApiDescription("openapi", - "/jeecg-boot/openapi/call/abc", "openapi", operations, false); + "/jeecg-boot/openapi/call/"+openapi.getRequestUrl(), openapi.getName(), operations, false); List apiList = new ArrayList<>(); apiList.add(apiDescription); @@ -446,7 +469,7 @@ public void resetOpenapiSwagger() { ApiListing api = new ApiListing("1.0", "/", - "/openapi/call/abc", + "/openapi/call/"+openapi.getRequestUrl(), produces, consumes, "", new HashSet<>(), new ArrayList<>(), apiList, responseModel, "abc", 0, apiTags); diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiAuth.java b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiAuth.java index cbf28d3e90..15a7c9a09b 100644 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiAuth.java +++ b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/entity/OpenApiAuth.java @@ -41,6 +41,11 @@ public class OpenApiAuth implements Serializable { */ private String sk; + /** + * 系统用户ID + */ + private String systemUserId; + /** * 创建人 */ diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api.sql b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api.sql deleted file mode 100644 index 3de4e3ed6a..0000000000 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api.sql +++ /dev/null @@ -1 +0,0 @@ -INSERT INTO `open_api` (`id`, `name`, `request_method`, `request_url`, `black_list`, `body`, `origin_url`, `status`, `del_flag`, `create_by`, `create_time`, `update_by`, `update_time`) VALUES ('1870486348755324929', '示例接口', 'GET', 'UrTk8CTk', NULL, NULL, 'http://localhost:8080/jeecg-boot/openapi/demo/index', 1, 0, NULL, '2024-12-21 23:07:52', NULL, NULL); diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_auth.sql b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_auth.sql deleted file mode 100644 index 50d4e0647d..0000000000 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_auth.sql +++ /dev/null @@ -1 +0,0 @@ -INSERT INTO `open_api_auth` (`id`, `name`, `ak`, `sk`, `create_by`, `create_time`, `update_by`, `update_time`) VALUES ('1870484839812517890', 'openuser', 'ak-eAU25mrMxhtaZsyS', 'rjxMqB6YyUXpSHAz4DCIz8vZ5aozQQiV', NULL, '2024-12-21 23:01:52', NULL, NULL); diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_header.sql b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_header.sql deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_param.sql b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_param.sql deleted file mode 100644 index e69de29bb2..0000000000 diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_permission.sql b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_permission.sql deleted file mode 100644 index a993e02f27..0000000000 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_permission.sql +++ /dev/null @@ -1 +0,0 @@ -INSERT INTO `open_api_permission` (`id`, `api_id`, `api_auth_id`, `create_by`, `create_time`, `update_by`, `update_time`) VALUES ('1870491556923179010', '1870486348755324929', '1870484839812517890', NULL, '2024-12-21 23:28:34', NULL, NULL); diff --git a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_record.sql b/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_record.sql deleted file mode 100644 index 8b5f2e0694..0000000000 --- a/jeecg-boot/jeecg-module-system/jeecg-system-biz/src/main/java/org/jeecg/modules/openapi/sql/open_api_record.sql +++ /dev/null @@ -1,4 +0,0 @@ -INSERT INTO `open_api_record` (`id`, `api_id`, `call_auth_id`, `call_time`, `used_time`, `response_time`) VALUES ('1870491654130368513', '1870486348755324929', '1870484839812517890', '2024-12-21 23:28:57', 184, '2024-12-21 23:28:57'); -INSERT INTO `open_api_record` (`id`, `api_id`, `call_auth_id`, `call_time`, `used_time`, `response_time`) VALUES ('1870492694183542786', '1870486348755324929', '1870484839812517890', '2024-12-21 23:31:04', 120717, '2024-12-21 23:33:05'); -INSERT INTO `open_api_record` (`id`, `api_id`, `call_auth_id`, `call_time`, `used_time`, `response_time`) VALUES ('1870493301040680961', '1870486348755324929', '1870484839812517890', '2024-12-21 23:35:26', 3865, '2024-12-21 23:35:30'); -INSERT INTO `open_api_record` (`id`, `api_id`, `call_auth_id`, `call_time`, `used_time`, `response_time`) VALUES ('1870493334939045889', '1870486348755324929', '1870484839812517890', '2024-12-21 23:35:38', 82, '2024-12-21 23:35:38');