Commit 3088ec43 authored by zhangc's avatar zhangc

添加api制作,api认证,组织管理相关接口

parent 41f0aa68
package com.jz.dm.common.base;
import com.jz.common.bean.SysUserDto;
import com.jz.common.constant.RedisMessageConstant;
import org.springframework.data.redis.core.RedisTemplate;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.common.base
* @PROJECT_NAME: jz-dm-parent
* @NAME: BaseCurrentUserObject
* @DATE: 2020-12-30/16:05
* @DAY_NAME_SHORT: 周三
* @Description: 获取当前登录用户
**/
public class BaseCurrentUserObject {
private static RedisTemplate<String, Object> REDIS = new RedisTemplate();
private volatile static SysUserDto sysUserDto;
private BaseCurrentUserObject() {
}
/**
* 获取登录用户
*
* @return
*/
public static SysUserDto getUserInfo() {
if (sysUserDto == null) {
synchronized (SysUserDto.class) {
if (sysUserDto == null) {
sysUserDto = (SysUserDto) REDIS.opsForValue().get("USER" + RedisMessageConstant.SENDTYPE_LOGIN_SYS);
}
}
}
return sysUserDto;
}
}
package com.jz.dm.common.constant;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.common.constant
* @PROJECT_NAME: jz-dm-parent
* @NAME: TagConstants
* @DATE: 2020-12-28/18:40
* @DAY_NAME_SHORT: 周一
* @Description:
**/
public class TagConstants {
//远程shell脚本存放路径
public static final String remoteShellFilePath = "/opt/shell";
//远程sql脚本存放路径
public static final String remoteSqlFilePath = "/opt/sql";
//kafka handler 相关
public static final String OPEN_API_HANDLER_LIST_INFO_NAMESPACE = "jz:openapi:handler:list";
public static final String OPEN_API_HANDLER_INFO_NAMESPACE = "jz:openapi:handler:";
public static final String OPEN_API_SCHEMA_INFO_NAMESPACE = "jz:openapi:schema:";
public static final String OPEN_API_SERVERS_INFO_NAMESPACE = "jz:openapi:kafka:servers:";
// es handler 相关
public static final String OPEN_API_ESTAG_INFO_NAMESPACE = "jz:openapi:handler:estag:";
public static final String OPEN_API_ESTAG_JDBC_INFO_NAMESPACE = "jz:openapi:handler:estag:jdbc:";
}
package com.jz.dm.common.enums.apiInterface;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.common.constant
* @PROJECT_NAME: jz-dm-parent
* @NAME: ApiStatusEnum
* @DATE: 2020-12-29/14:28
* @DAY_NAME_SHORT: 周二
* @Description:
**/
public enum ApiStatusEnum {
/**
* 草稿
*/
DRAFT("草稿"),
/**
* 发布'
*/
ISSUE("发布"),
/**
* 过时
*/
OUTMODED("过时"),
/**
* 冻结
*/
FREEZE("冻结"),
/**
* 失效'
*/
EXPIRY("失效");
private String text;
ApiStatusEnum(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
package com.jz.dm.common.enums.auth;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.common.enums.auth
* @PROJECT_NAME: jz-dm-parent
* @NAME: AuthModeEnum
* @DATE: 2020-12-26/23:53
* @DAY_NAME_SHORT: 周六
* @Description: 认证方式枚举
**/
public enum AuthModeEnum {
/**
* 按次调用
*/
POWER_CALL_MODE("按次调用"),
/**
* 按时间调用'
*/
RECORD_TIME_MODE("按时间调用"),
/**
* 永久有效'
*/
PERMANENT_TIME_MODE("永久有效");
private String text;
AuthModeEnum(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
package com.jz.dm.common.enums.auth;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.common.enums.auth
* @PROJECT_NAME: jz-dm-parent
* @NAME: AuthTypeEnum
* @DATE: 2020-12-26/23:47
* @DAY_NAME_SHORT: 周六
* @Description: 授权认证枚举类
**/
public enum AuthTypeEnum {
/**
* 数据银行
*/
DATA_BANK_AUTH("数据银行授权"),
/**
* dmp授权'
*/
DMP_AUTH("DMP授权");
private String text;
AuthTypeEnum(String text) {
this.text = text;
}
public String getText() {
return text;
}
}
package com.jz.dm.common.exception;
import com.jz.dm.common.util.ResultCode;
/**
* 信息摘要异常
*
*/
public class MessageDigestException extends OpenApiException {
private static final long serialVersionUID = 477249046784548217L;
/**
* 构造函数
* @param resultCode
*/
public MessageDigestException(ResultCode resultCode) {
super(resultCode);
}
/**
* 构造函数
* @param resultCode
* @param detailMessage
*/
public MessageDigestException(ResultCode resultCode, String detailMessage) {
super(resultCode, detailMessage);
}
/**
* 构造函数
* @param resultCode
* @param cause
*/
public MessageDigestException(ResultCode resultCode, Throwable cause) {
super(resultCode, cause);
}
/**
* 构造函数
* @param resultCode
* @param detailMessage
* @param cause
*/
public MessageDigestException(ResultCode resultCode, String detailMessage, Throwable cause) {
super(resultCode, detailMessage, cause);
}
}
package com.jz.dm.common.util;
import java.io.UnsupportedEncodingException;
import java.util.Random;
import java.util.regex.Pattern;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.common.util
* @PROJECT_NAME: jz-dm-parent
* @NAME: RandomUtil
* @DATE: 2020-12-30/9:50
* @DAY_NAME_SHORT: 周三
* @Description: 生成随机编码工具类
**/
public class RandomUtil {
private final static int[] li_SecPosValue = { 1601, 1637, 1833, 2078, 2274,
2302, 2433, 2594, 2787, 3106, 3212, 3472, 3635, 3722, 3730, 3858,
4027, 4086, 4390, 4558, 4684, 4925, 5249, 5590 };
private final static String[] lc_FirstLetter = { "a", "b", "c", "d", "e",
"f", "g", "h", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
"t", "w", "x", "y", "z" };
/**
* 生成指定长度字母+数字的随机数
* @param length 长度
* @return
*/
public static String getStringRandom(int length) {
String val = "";
Random random = new Random();
//参数length,表示生成几位随机数
for (int i = 0; i < length; i++) {
String charOrNum = random.nextInt(2) % 2 == 0 ? "char" : "num";
//输出字母还是数字
if ("char".equalsIgnoreCase(charOrNum)) {
//输出是大写字母还是小写字母
int temp = random.nextInt(2) % 2 == 0 ? 65 : 97;
val += (char) (random.nextInt(26) + temp);
} else if ("num".equalsIgnoreCase(charOrNum)) {
val += String.valueOf(random.nextInt(10));
}
}
return val;
}
/**
* 取得给定汉字串的首字母串,即声母串
* @param str 给定汉字串
* @return 声母串
*/
public static String getAllFirstLetter(String str) {
if (str == null || str.trim().length() == 0) {
return "";
}
String _str = "";
for (int i = 0; i < str.length(); i++) {
_str = _str + getFirstLetter(str.substring(i, i + 1));
}
return _str;
}
/**
* 取得给定汉字的首字母,即声母
* @param chinese 给定的汉字
* @return 给定汉字的声母
*/
public static String getFirstLetter(String chinese) {
if (chinese == null || chinese.trim().length() == 0) {
return "";
}
chinese = conversionStr(chinese, "GB2312", "ISO8859-1");
if (chinese.length() > 1) // 判断是不是汉字
{
int li_SectorCode = (int) chinese.charAt(0); // 汉字区码
int li_PositionCode = (int) chinese.charAt(1); // 汉字位码
li_SectorCode = li_SectorCode - 160;
li_PositionCode = li_PositionCode - 160;
int li_SecPosCode = li_SectorCode * 100 + li_PositionCode; // 汉字区位码
if (li_SecPosCode > 1600 && li_SecPosCode < 5590) {
for (int i = 0; i < 23; i++) {
if (li_SecPosCode >= li_SecPosValue[i]
&& li_SecPosCode < li_SecPosValue[i + 1]) {
chinese = lc_FirstLetter[i];
break;
}
}
} else // 非汉字字符,如图形符号或ASCII码
{
chinese = conversionStr(chinese, "ISO8859-1", "GB2312");
chinese = chinese.substring(0, 1);
}
}
return chinese;
}
/**
* 字符串编码转换
* @param str 要转换编码的字符串
* @param charsetName 原来的编码
* @param toCharsetName 转换后的编码
* @return 经过编码转换后的字符串
*/
private static String conversionStr(String str, String charsetName,String toCharsetName) {
try {
str = new String(str.getBytes(charsetName), toCharsetName);
} catch (UnsupportedEncodingException ex) {
System.out.println("字符串编码转换异常:" + ex.getMessage());
}
return str;
}
/**
* 限制只能输入中文
*/
public static boolean isChineseWord(String str){
String pattern = "[\u4e00-\u9fa5]+";
boolean isMatch = Pattern.matches(pattern, str);
return isMatch;
}
public static void main(String[] args) {
//System.out.println(getStringRandom(8));
// System.out.println("获取拼音首字母:"+getAllFirstLetter("大中国南昌中大china"));
System.out.println(isChineseWord("大中国南昌中大china"));
}
}
/**
* Copyright (c) 2011-2019 All Rights Reserved.
*/
package com.jz.dm.config;
import com.jz.common.utils.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import javax.annotation.Resource;
/**
* @author Admin
* @version $Id: MapLock.java 2019年10月26日 下午5:27:52 $
*/
@Component
public class ReentrantRedisLock {
//获取尝试时间
private final String LOCK = "lock:";
@Resource
private RedisTemplate<String, String> redisTemplate;
@Value("${server.port}")
private String port;
@Autowired
private RedisUtils redisUtils;
/**
* 判断是否存在
*
* @return
*/
public synchronized boolean tryLock(String value) {
return redisUtils.setIfAbsent(LOCK + value, value, 2);
}
public synchronized boolean tryLock(String value, long minutes) {
return redisUtils.setIfAbsent(LOCK + value, value, minutes);
}
/**
* 处理完成清除
*/
public void unlock(String value) {
redisTemplate.delete(LOCK + value);
}
}
package com.jz.dm.config;
import com.jz.dm.web.interceptor.AccessLimitInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.config
* @PROJECT_NAME: jz-dm-parent
* @NAME: WebAppConfigurer
* @DATE: 2020-12-26/18:53
* @DAY_NAME_SHORT: 周六
* @Description:
**/
@Configuration
public class WebAppConfigurer extends WebMvcConfigurerAdapter {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 可添加多个,这里选择拦截所有请求地址,进入后判断是否有加注解即可
registry.addInterceptor(getAccessLimitInterceptor())
.addPathPatterns("/api/organization/**")
.excludePathPatterns(
"/**/*.html", //html静态资源
"/**/*.js", //js静态资源
"/**/*.css" //css静态资源
);
}
@Bean
public AccessLimitInterceptor getAccessLimitInterceptor() {
return new AccessLimitInterceptor();
}
}
package com.jz.dm.mapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.jz.dm.models.domian.ApiInterfaceCustom;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.mapper
* @PROJECT_NAME: jz-dm-parent
* @NAME: ApiInterfaceCustomMapper
* @DATE: 2020-12-30/18:00
* @DAY_NAME_SHORT: 周三
* @Description:
**/
public interface ApiInterfaceCustomMapper extends BaseMapper<ApiInterfaceCustom> {
}
package com.jz.dm.models.domian;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableName;
import com.jz.dm.common.base.BaseObject;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;
import java.io.Serializable;
/**
* @Description:
* @Author: Mr.zhang
* @Date: 2020-12-23
*/
@Data
@EqualsAndHashCode(callSuper = false)
@NoArgsConstructor
@Accessors(chain = true)
@TableName("t_api_interface_custom")
public class ApiInterfaceCustom extends BaseObject implements Serializable {
/**
* api唯一标识
*/
@TableField("api_key")
private String apiKey;
/**
* apiInterfaceId
*/
@TableField("api_interface_id")
private Long apiInterfaceId;
/**
* api类型:1 实时接入 2 数据银行制作API, 3 数据银行制作数据包 6标签查询 9自定义'
*/
@TableField("api_type")
private String apiType;
/**
* api请求参数
*/
@TableField("request_param")
private String requestParam;
/**
* api响应参数
*/
@TableField("response_param")
private String responseParam;
/**
* 请求头
*/
@TableField("req_headers")
private String reqHeaders;
/**
* 响应状态码
*/
@TableField("resp_code")
private String respCode;
/**
* api返回样例
*/
@TableField("api_example")
private String apiExample;
}
package com.jz.dm.models.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import java.io.Serializable;
import java.util.Date;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.dto
* @PROJECT_NAME: jz-dm-parent
* @NAME: AuthInfoDto
* @DATE: 2020-12-27/0:31
* @DAY_NAME_SHORT: 周日
* @Description:
**/
@Data
@ApiModel("授权信息返回体")
public class AuthInfoDto implements Serializable {
@ApiModelProperty(value = "id")
private Long id;
@ApiModelProperty(value = "授权组织id")
private Long apiOrgId;
@ApiModelProperty(value = "api应用id")
private Long apiInterfaceId;
@ApiModelProperty(value = "授权组织名称")
private String orgName;
@ApiModelProperty(value = "apiKey")
private String apiKey;
@ApiModelProperty(value = "授权方式:1.按次调用 2.按时间调用,3永久有效")
private String authMode;
@ApiModelProperty(value = "开始时间")
private Date validStartTime;
@ApiModelProperty(value = "结束时间")
private Date validEndTime;
@ApiModelProperty(value = "状态(1-正常 2-作废")
private String status;
@ApiModelProperty(value = "备注")
private String remark;
@ApiModelProperty(value = "创建时间")
private Date createTime;
@ApiModelProperty(value = "创建用户")
private String createUser;
}
package com.jz.dm.models.req;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.req
* @PROJECT_NAME: jz-dm-parent
* @NAME: AuthDetailInfoReq
* @DATE: 2020-12-26/15:56
* @DAY_NAME_SHORT: 周六
* @Description:
**/
@Data
@ApiModel("认证详情信息查询")
public class AuthDetailInfoReq implements Serializable {
@ApiModelProperty(value = "id",required = true)
@NotNull(message = "id不能为空")
private Long id;
}
package com.jz.dm.models.req;
import com.jz.dm.common.enums.auth.AuthModeEnum;
import com.jz.dm.common.enums.auth.AuthTypeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.req
* @PROJECT_NAME: jz-dm-parent
* @NAME: AuthMallUserApiReq
* @DATE: 2020-12-26/15:52
* @DAY_NAME_SHORT: 周六
* @Description:
**/
@Data
@ApiModel("DMP用户API认证")
public class AuthDmpUserApiReq implements Serializable {
@ApiModelProperty(value = "apiKey唯一标识",required = true)
@NotNull(message = "apiKey唯一标识不能为空")
private String apiKey;
@ApiModelProperty(value = "组织id",required = true)
@NotNull(message = "组织id不能为空")
private Long orgId;
@ApiModelProperty(value = "授权类型:DATA_BANK_AUTH 数据银行 , DMP_AUTH dmp授权",required = true)
@NotNull(message = "授权类型不能为空")
private AuthTypeEnum authType;
@ApiModelProperty(value = "授权方式:POWER_CALL_MODE 按次调用 ,RECORD_TIME_MODE 按时间调用,PERMANENT_TIME_MODE 永久有效",required = true)
@NotNull(message = "授权方式不能为空")
private AuthModeEnum authMode;
@ApiModelProperty(value = "开始时间",required = false)
private String validStartTime;
@ApiModelProperty(value = "结束时间",required = false)
private String validEndTime;
@ApiModelProperty(value = "备注",required = false)
private String remark;
}
package com.jz.dm.models.req;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.req
* @PROJECT_NAME: jz-dm-parent
* @NAME: AuthDetailInfoReq
* @DATE: 2020-12-26/15:56
* @DAY_NAME_SHORT: 周六
* @Description:
**/
@Data
@ApiModel("认证信息修改")
public class AuthInfoUpdateReq implements Serializable {
@ApiModelProperty(value = "id",required = true)
@NotNull(message = "id不能为空")
private Long id;
}
package com.jz.dm.models.req;
import com.jz.common.bean.BasePageBean;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import java.io.Serializable;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.req
* @PROJECT_NAME: jz-dm-parent
* @NAME: AuthListInfoReq
* @DATE: 2020-12-27/0:22
* @DAY_NAME_SHORT: 周日
* @Description:
**/
@Data
@ApiModel("认证信息列表请求体")
public class AuthListInfoReq extends BasePageBean implements Serializable {
}
package com.jz.dm.models.req;
import com.jz.dm.common.enums.auth.AuthModeEnum;
import com.jz.dm.common.enums.auth.AuthTypeEnum;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.req
* @PROJECT_NAME: jz-dm-parent
* @NAME: AuthMallUserApiReq
* @DATE: 2020-12-26/15:52
* @DAY_NAME_SHORT: 周六
* @Description:
**/
@Data
@ApiModel("商城用户API认证")
public class AuthMallUserApiReq implements Serializable {
@ApiModelProperty(value = "apiKey唯一标识",required = true)
@NotNull(message = "apiKey唯一标识不能为空")
private String apiKey;
@ApiModelProperty(value = "组织id",required = true)
@NotNull(message = "组织id不能为空")
private Long orgId;
@ApiModelProperty(value = "授权类型:DATA_BANK_AUTH 数据银行 , DMP_AUTHdmp 授权",required = true)
@NotNull(message = "授权类型不能为空")
private AuthTypeEnum authType;
@ApiModelProperty(value = "授权方式:POWER_CALL_MODE 按次调用 ,RECORD_TIME_MODE 按时间调用,PERMANENT_TIME_MODE 永久有效",required = true)
@NotNull(message = "授权方式不能为空")
private AuthModeEnum authMode;
@ApiModelProperty(value = "开始时间",required = false)
private String validStartTime;
@ApiModelProperty(value = "结束时间",required = false)
private String validEndTime;
@ApiModelProperty(value = "备注",required = false)
private String remark;
}
package com.jz.dm.models.req;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.req
* @PROJECT_NAME: jz-dm-parent
* @NAME: SaltResetReq
* @DATE: 2020-12-26/19:16
* @DAY_NAME_SHORT: 周六
* @Description:
**/
@Data
@ApiModel("盐值重置请求体")
public class SaltResetReq implements Serializable {
@ApiModelProperty(value = "id",required = true)
@NotNull(message = "id不能为空")
private Long id;
@ApiModelProperty(value = "盐值",required = true)
@NotNull(message = "盐值不能为空")
private String salt;
}
package com.jz.dm.models.req.make;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.req.make
* @PROJECT_NAME: jz-dm-parent
* @NAME: ApiInterfaceReq
* @DATE: 2020-12-29/10:48
* @DAY_NAME_SHORT: 周二
* @Description:
**/
@Data
@ApiModel(value = "api基本信息")
public class ApiInterfaceReq {
@ApiModelProperty(value = "api类型",required = true)
@NotNull(message = "api类型不能为空")
public String apiInterfaceType;
@ApiModelProperty(value = "apiId 等价于apiKey",required = true)
@NotNull(message = "apiId不能为空")
public String apiId;
@ApiModelProperty(value = "接口描述",required = false)
public String apiDesc;
@ApiModelProperty(value = "api版本",required = false)
public String version;
@ApiModelProperty(value = "传输方式",required = true)
@NotNull(message = "传输方式不能为空")
public String transMode;
@ApiModelProperty(value = "加密方式0 无,1:MD5 2:RSA",required = true)
@NotNull(message = "加密方式不能为空")
public String type;
@ApiModelProperty(value = "目标地址",required = true)
@NotNull(message="目标地址不能为空")
public String targetUrl;
@ApiModelProperty(value = "超时时间",required = true)
@NotNull(message="超时时间不能为空")
public String timeout;
@ApiModelProperty(value = "api描述",required = false)
public String apiFunction;
@ApiModelProperty(value = "列",required = false)
public String columnCode;
@ApiModelProperty(value = "状态",required = false)
public String status;
@ApiModelProperty(value = "更新时传入api自增id",required = false)
public Long id;
}
package com.jz.dm.models.req.make;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.req.make
* @PROJECT_NAME: jz-dm-parent
* @NAME: MakeApiSaveInfoReq
* @DATE: 2020-12-29/10:00
* @DAY_NAME_SHORT: 周二
* @Description:
**/
@Data
@ApiModel("保存API基本信息请求体")
public class MakeApiSaveInfoReq implements Serializable {
@ApiModelProperty(value = "api类型",required = true)
@NotNull(message = "api类型不能为空")
public String apiType;
@ApiModelProperty(value = "apiId 等价于apiKey",required = true)
@NotNull(message = "apiId不能为空")
public String apiId;
@ApiModelProperty(value = "接口描述",required = false)
public String apiDesc;
@ApiModelProperty(value = "api版本",required = false)
public String version;
@ApiModelProperty(value = "加密方式",required = true)
@NotNull(message = "加密方式不能为空")
public String transMode;
@ApiModelProperty(value = "传输方式",required = true)
@NotNull(message="传输方式不能为空")
public String type;
@ApiModelProperty(value = "目标地址",required = true)
@NotNull(message="目标地址不能为空")
public String targetUrl;
@ApiModelProperty(value = "超时时间",required = true)
@NotNull(message="超时时间不能为空")
public String timeout;
@ApiModelProperty(value = "api描述",required = false)
public String apiFunction;
@ApiModelProperty(value = "项目编号",required = true)
@NotNull(message="项目编号不能为空")
public String projectId;
@ApiModelProperty(value = "列",required = false)
public String columnCode;
@ApiModelProperty(value = "状态",required = false)
public String status;
}
package com.jz.dm.models.req;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import java.io.Serializable;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.req
* @PROJECT_NAME: jz-dm-parent
* @NAME: MakeBigDataApiReq
* @DATE: 2020-12-26/13:08
* @DAY_NAME_SHORT: 周六
* @Description:
**/
@Data
@ApiModel("Api制作大数据查询/标签查询请求体")
public class MakeBigDataApiReq implements Serializable {
}
package com.jz.dm.models.req;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import java.io.Serializable;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.req
* @PROJECT_NAME: jz-dm-parent
* @NAME: MakeCustomApiReq
* @DATE: 2020-12-26/13:01
* @DAY_NAME_SHORT: 周六
* @Description:
**/
@Data
@ApiModel("制作自定义Api")
public class MakeCustomApiReq implements Serializable {
}
package com.jz.dm.models.req.make;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.req.make
* @PROJECT_NAME: jz-dm-parent
* @NAME: MakeDataBankApiReq
* @DATE: 2020-12-30/15:32
* @DAY_NAME_SHORT: 周三
* @Description:
**/
@Data
@ApiModel("数据银行api制作")
public class MakeDataBankApiReq implements Serializable {
@ApiModelProperty(value = "api类型:1 实时接入 2 数据银行制作API, 3 数据银行制作数据包 6标签查询 9自定义",required = true)
@NotNull(message = "api类型不能为空")
public String apiType;
@ApiModelProperty(value = "apiId 等价于apiKey",required = true)
@NotNull(message = "apiId不能为空")
public String apiId;
@ApiModelProperty(value = "接口描述",required = false)
public String apiDesc;
@ApiModelProperty(value = "api版本",required = false)
public String version;
@ApiModelProperty(value = "传输方式(1为HTTPS,2为HTTP)",required = true)
@NotNull(message = "传输方式不能为空")
public String transMode;
@ApiModelProperty(value = "加密方式0 无,1:MD5 2:RSA",required = true)
@NotNull(message = "加密方式不能为空")
public String type;
@ApiModelProperty(value = "目标地址",required = false)
public String targetUrl;
@ApiModelProperty(value = "超时时间",required = false)
public String timeout;
@ApiModelProperty(value = "api描述",required = false)
public String apiFunction;
@ApiModelProperty(value = "请求头",required = false)
public String reqHeaders;
@ApiModelProperty(value = "请求参数",required = false)
public String requestParam;
@ApiModelProperty(value = "响应参数",required = false)
public String responseParam;
@ApiModelProperty(value = "响应状态码",required = false)
public String respCode;
@ApiModelProperty(value = "api样例",required = false)
public String apiExample;
}
package com.jz.dm.models.req;
import io.swagger.annotations.ApiModel;
import lombok.Data;
import java.io.Serializable;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.req
* @PROJECT_NAME: jz-dm-parent
* @NAME: MakeRealCustomApiReq
* @DATE: 2020-12-26/13:06
* @DAY_NAME_SHORT: 周六
* @Description:
**/
@Data
@ApiModel("API制作(实时接入)")
public class MakeRealCustomApiReq implements Serializable {
}
package com.jz.dm.models.req.make;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
import javax.validation.constraints.NotNull;
import java.io.Serializable;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.models.req.make
* @PROJECT_NAME: jz-dm-parent
* @NAME: SourceConfigReq
* @DATE: 2020-12-27/1:44
* @DAY_NAME_SHORT: 周日
* @Description:
**/
@Data
@ApiModel("数据源配置请求体")
public class SourceConfigReq implements Serializable {
@ApiModelProperty(value = "发布id",required = false)
private String releaseId;
@ApiModelProperty(value = "apiKey",required = true)
@NotNull(message = "apiKey不能为空")
private String apiKey;
}
package com.jz.dm.web.annotation;
import java.lang.annotation.*;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.web.annotation
* @PROJECT_NAME: jz-dm-parent
* @NAME: AccessLimit
* @DATE: 2020-12-26/17:38
* @DAY_NAME_SHORT: 周六
* @Description: 限流拦截器
**/
@Inherited
@Documented
@Target({ElementType.FIELD,ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface AccessLimit {
//标识 指定sec时间段内的访问次数限制
int limit() default 5;
//标识 时间段(秒)
int sec() default 5;
}
package com.jz.dm.web.annotation;
import java.lang.annotation.*;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.web.annotation
* @PROJECT_NAME: jz-dm-parent
* @NAME: ApiReturnLog
* @DATE: 2020-12-25/17:43
* @DAY_NAME_SHORT: 周五
* @Description:
**/
@Target({ElementType.PARAMETER, ElementType.METHOD})//作用在参数和方法上
@Retention(RetentionPolicy.RUNTIME)//运行时注解
@Documented//表明这个注解应该被 javadoc工具记录
public @interface ApiReturnLog {
String description() default "";
}
...@@ -7,8 +7,7 @@ import com.jz.common.utils.UrlUtil; ...@@ -7,8 +7,7 @@ import com.jz.common.utils.UrlUtil;
import com.jz.dm.common.util.SignType; import com.jz.dm.common.util.SignType;
import com.jz.dm.models.domian.ApiReqLog; import com.jz.dm.models.domian.ApiReqLog;
import com.jz.dm.service.ApiLogService; import com.jz.dm.service.ApiLogService;
import com.jz.dm.web.annotation.ApiBeforeLog; import com.jz.dm.web.annotation.ApiLogAspect;
import com.jz.dm.web.annotation.ApiReturnLog;
import javassist.*; import javassist.*;
import javassist.bytecode.CodeAttribute; import javassist.bytecode.CodeAttribute;
import javassist.bytecode.LocalVariableAttribute; import javassist.bytecode.LocalVariableAttribute;
...@@ -51,11 +50,11 @@ public class SystemLogAspect { ...@@ -51,11 +50,11 @@ public class SystemLogAspect {
private ApiLogService apiLogService; private ApiLogService apiLogService;
//Service层切点 //Service层切点
@Pointcut("@annotation(com.jz.dm.web.annotation.ApiBeforeLog)") @Pointcut("@annotation(com.jz.dm.web.annotation.ApiLogAspect)")
public void serviceAspect() { public void serviceAspect() {
} }
//Controller层切点 //Controller层切点
@Pointcut("@annotation(com.jz.dm.web.annotation.ApiReturnLog)") @Pointcut("@annotation(com.jz.dm.web.annotation.ApiLogAspect)")
public void controllerAspect(){ public void controllerAspect(){
} }
...@@ -84,7 +83,7 @@ public class SystemLogAspect { ...@@ -84,7 +83,7 @@ public class SystemLogAspect {
ApiReqLog reqLog = new ApiReqLog(); ApiReqLog reqLog = new ApiReqLog();
reqLog.setRequestIp(ip); reqLog.setRequestIp(ip);
// reqLog.setApiKey(jsonObject.getString("apiKey")); // reqLog.setApiKey(jsonObject.getString("apiKey"));
// reqLog.setRequestParams(params); reqLog.setRequestParams(params);
reqLog.setResponseParams("");//响应参数 reqLog.setResponseParams("");//响应参数
reqLog.setRequestUrl(url); reqLog.setRequestUrl(url);
reqLog.setRequestMethod(contextPath); reqLog.setRequestMethod(contextPath);
...@@ -94,17 +93,18 @@ public class SystemLogAspect { ...@@ -94,17 +93,18 @@ public class SystemLogAspect {
reqLog.setRequestTime(new Date()); reqLog.setRequestTime(new Date());
reqLog.setRemark(getServiceMethodDescription(joinPoint)); reqLog.setRemark(getServiceMethodDescription(joinPoint));
System.out.println(reqLog); System.out.println(reqLog);
//if (null != reqLog){ if (null != reqLog){
// // apiLogService.insetLogInfo(reqLog); apiLogService.insetLogInfo(reqLog);
// //} }
} catch(Exception e) { } catch(Exception e) {
log.error("~~~~~~~~~~~~~~~~~~~~~~~前置通知异常~~~~~~~~~~~~~~~~~~~~~~~"); log.error("~~~~~~~~~~~~~~~~~~~~~~~前置通知异常~~~~~~~~~~~~~~~~~~~~~~~");
log.error("异常信息{}",e.getMessage()); log.error("异常信息{}",e.getMessage());
} }
} }
/**返回异常通知,返回抛出异常的时候执行的通知,可以获得返回的异常 /**
可以访问到异常对象,且可以指定在出现特定异常的时候再执行通知代码 * 返回异常通知,返回抛出异常的时候执行的通知,可以获得返回的异常
* 可以访问到异常对象,且可以指定在出现特定异常的时候再执行通知代码
*/ */
@AfterReturning(value = "controllerAspect()",returning = "result") @AfterReturning(value = "controllerAspect()",returning = "result")
public void afterReturn(JoinPoint joinPoint,Object result){ public void afterReturn(JoinPoint joinPoint,Object result){
...@@ -130,10 +130,12 @@ public class SystemLogAspect { ...@@ -130,10 +130,12 @@ public class SystemLogAspect {
Map mapResult = (Map) jsonObject; Map mapResult = (Map) jsonObject;
//将返回的result参数取出 //将返回的result参数取出
Map<String, Object> res = (Map<String, Object>) mapResult.get("data"); Map<String, Object> res = (Map<String, Object>) mapResult.get("data");
Long id = (Long)res.get("id"); Integer id = (Integer) res.get("id");
apiLogService.updateLog(id,jsonObject); JSONObject data = jsonObject.getJSONObject("data");
// apiLogService.updateLog(id.longValue(),data);
} catch(Exception e) { } catch(Exception e) {
e.printStackTrace(); log.error("~~~~~~~~~~~~~~~~~~~~~~~最终通知异常~~~~~~~~~~~~~~~~~~~~~~~");
log.error("异常信息{}",e.getMessage());
} }
} }
...@@ -155,7 +157,7 @@ public class SystemLogAspect { ...@@ -155,7 +157,7 @@ public class SystemLogAspect {
if (method.getName().equals(methodName)) { if (method.getName().equals(methodName)) {
Class[] clazzs = method.getParameterTypes(); Class[] clazzs = method.getParameterTypes();
if (clazzs.length == arguments.length) { if (clazzs.length == arguments.length) {
description = method.getAnnotation(ApiBeforeLog.class).description(); description = method.getAnnotation(ApiLogAspect.class).description();
break; break;
} }
} }
...@@ -163,30 +165,6 @@ public class SystemLogAspect { ...@@ -163,30 +165,6 @@ public class SystemLogAspect {
return description; return description;
} }
/**
* 获取注解中对方法的描述信息 用于Controller层注解
* @param joinPoint
* @return
* @throws Exception
*/
public static String getControllerMethodDescription(JoinPoint joinPoint) throws Exception {
String targetName = joinPoint.getTarget().getClass().getName();
String methodName = joinPoint.getSignature().getName();//目标方法名
Object[] arguments = joinPoint.getArgs();
Class targetClass = Class.forName(targetName);
Method[] methods = targetClass.getMethods();
String description = "";
for (Method method:methods) {
if (method.getName().equals(methodName)){
Class[] clazzs = method.getParameterTypes();
if (clazzs.length==arguments.length){
description = method.getAnnotation(ApiReturnLog.class).description();
break;
}
}
}
return description;
}
public static Map<String, Object> getFieldsName(Class cls, String clazzName, String methodName, Object[] args)throws Exception { public static Map<String, Object> getFieldsName(Class cls, String clazzName, String methodName, Object[] args)throws Exception {
Map<String, Object> map = new HashMap<String, Object>(); Map<String, Object> map = new HashMap<String, Object>();
......
package com.jz.dm.web.interceptor;
import com.jz.common.utils.IpUtils;
import com.jz.dm.web.annotation.AccessLimit;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.Method;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.web.aspect
* @PROJECT_NAME: jz-dm-parent
* @NAME: AccessLimitInterceptor
* @DATE: 2020-12-26/17:39
* @DAY_NAME_SHORT: 周六
* @Description:
**/
public class AccessLimitInterceptor implements HandlerInterceptor {
//使用RedisTemplate操作redis
/* @Autowired
private RedisTemplate<String, Integer> redisTemplate;*/
@Override
public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
if (!method.isAnnotationPresent(AccessLimit.class)) {
return true;
}
AccessLimit accessLimit = method.getAnnotation(AccessLimit.class);
if (accessLimit == null) {
return true;
}
int limit = accessLimit.limit();
int sec = accessLimit.sec();
String key = IpUtils.getIpAddr(request) + request.getRequestURI();
/*Integer maxLimit = redisTemplate.opsForValue().get(key);
if (maxLimit == null) {
//set时一定要加过期时间
redisTemplate.opsForValue().set(key, 1, sec, TimeUnit.SECONDS);
} else if (maxLimit < limit) {
redisTemplate.opsForValue().set(key, maxLimit + 1, sec, TimeUnit.SECONDS);
} else {
output(response, "请求太频繁~!");
return false;
}*/
}
return true;
}
public void output(HttpServletResponse response, String msg) throws IOException {
response.setContentType("application/json;charset=UTF-8");
ServletOutputStream outputStream = null;
try {
outputStream = response.getOutputStream();
outputStream.write(msg.getBytes("UTF-8"));
} catch (IOException e) {
e.printStackTrace();
} finally {
outputStream.flush();
outputStream.close();
}
}
@Override
public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
}
@Override
public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.jz.dm.mapper.ApiInterfaceMapper">
</mapper>
\ No newline at end of file
package com.jz.dm.gateway.org;
import com.jz.dm.gateway.SpringTestCase;
import com.jz.dm.service.AuthService;
import org.springframework.beans.factory.annotation.Autowired;
/**
* @author ZC
* @PACKAGE_NAME: com.jz.dm.gateway.org
* @PROJECT_NAME: jz-dm-parent
* @NAME: TestAuth
* @DATE: 2020-12-30/15:16
* @DAY_NAME_SHORT: 周三
* @Description:
**/
public class TestAuth extends SpringTestCase {
@Autowired
private AuthService authService;
}
/**
* Copyright (c) 2011-2019 All Rights Reserved.
*/
package com.jz.common.utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;
/**
*
*
* @author Admin
* @version $Id: RedisUtils.java 2019年11月2日 上午8:36:41 $
*/
@Component("redisUtils")
public class RedisUtils {
@Autowired
private RedisTemplate<String, Object> redisTemplate;
/**
* 实现命令:TTL key,以秒为单位,返回给定 key的剩余生存时间(TTL, time to live)。
*
* @param key
* @return
*/
public long ttl(String key) {
return redisTemplate.getExpire(key);
}
/**
* 实现命令:expire 设置过期时间,单位秒
*
* @param key
* @return
*/
public void expire(String key, long timeout) {
redisTemplate.expire(key, timeout, TimeUnit.SECONDS);
}
/**
* 实现命令:INCR key,增加key一次
*
* @param key
* @return
*/
public long incr(String key, long delta) {
return redisTemplate.opsForValue().increment(key, delta);
}
/**
* 实现命令: key,减少key一次
*
* @param key
* @return
*/
public long decr(String key, long delta) {
if (delta < 0) {
del(key);
return 0;
}
return redisTemplate.opsForValue().increment(key, -delta);
}
/**
* 实现命令:KEYS pattern,查找所有符合给定模式 pattern的 key
*/
public Set<String> keys(String pattern) {
return redisTemplate.keys(pattern);
}
/**
* 实现命令:DEL key,删除一个key
*
* @param key
*/
public void del(String key) {
redisTemplate.delete(key);
}
/**
* 实现命令:SET key value,设置一个key-value(将字符串值 value关联到 key)
*
* @param key
* @param value
*/
public void set(String key, Object value) {
redisTemplate.opsForValue().set(key, value);
}
/**
* 实现命令:SET key value EX seconds,设置key-value和超时时间(秒)
*
* @param key
* @param value
* @param timeout (以秒为单位)
*/
public void set(String key, Object value, long timeout) {
redisTemplate.opsForValue().set(key, value, timeout, TimeUnit.SECONDS);
}
/**
* 实现命令:GET key,返回 key所关联的字符串值。
*
* @param key
* @return value
*/
public String get(String key) {
return (String) redisTemplate.opsForValue().get(key);
}
/**
* Hash(哈希表)
* 实现命令:HSET key field value,将哈希表 key中的域 field的值设为 value
*
* @param key
* @param field
* @param value
*/
public void hset(String key, String field, Object value) {
redisTemplate.opsForHash().put(key, field, value);
}
/**
* Hash(哈希表)
* 实现命令:HMSET key map 批量插入
*
* @param key
* @param m
*/
public void hmset(String key, Map<Object, Object> m) {
redisTemplate.opsForHash().putAll(key, m);
}
/**
* 实现命令:HGET key field,返回哈希表 key中给定域 field的值
*
* @param key
* @param field
* @return
*/
public String hget(String key, String field) {
return (String) redisTemplate.opsForHash().get(key, field);
}
/**
* 实现命令:HDEL key field [field ...],删除哈希表 key 中的一个或多个指定域,不存在的域将被忽略。
*
* @param key
* @param fields
*/
public void hdel(String key, Object... fields) {
redisTemplate.opsForHash().delete(key, fields);
}
/**
* 实现命令:HGETALL key,返回哈希表 key中,所有的域和值。
*
* @param key
* @return
*/
public Map<Object, Object> hgetall(String key) {
return redisTemplate.opsForHash().entries(key);
}
/**
* List(列表)
* 实现命令:LPUSH key value,将一个值 value插入到列表 key的表头
*
* @param key
* @param value
* @return 执行 LPUSH命令后,列表的长度。
*/
public long lpush(String key, Object value) {
return redisTemplate.opsForList().leftPush(key, value);
}
/**
* 实现命令:LPOP key,移除并返回列表 key的头元素。
*
* @param key
* @return 列表key的头元素。
*/
public String lpop(String key) {
return (String) redisTemplate.opsForList().leftPop(key);
}
/**
* 实现命令:RPUSH key value,将一个值 value插入到列表 key的表尾(最右边)。
*
* @param key
* @param value
* @return 执行 LPUSH命令后,列表的长度。
*/
public long rpush(String key, Object value) {
return redisTemplate.opsForList().rightPush(key, value);
}
/**
*
*
* @param <T>
* @param script
* @param keys
* @param args
* @return
*/
public <T> T execute(RedisScript<T> script, List<String> keys, Object... args) {
return redisTemplate.execute(script, keys, args);
}
/**
*
* @param rid
*/
public List<Object> range(String key) {
return redisTemplate.opsForList().range(key, 0, 500);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment