diff --git a/pigx-auth/src/main/resources/logback-spring.xml b/pigx-auth/src/main/resources/logback-spring.xml new file mode 100644 index 000000000..00fc55116 --- /dev/null +++ b/pigx-auth/src/main/resources/logback-spring.xml @@ -0,0 +1,87 @@ + + + + + + + + + + + + + + + + ${CONSOLE_LOG_PATTERN} + + + + + + ${log.path}/debug.log + + ${log.path}/%d{yyyy-MM, aux}/debug.%d{yyyy-MM-dd}.%i.log.gz + 50MB + 30 + + + %date [%thread] %-5level [%logger{50}] %file:%line - %msg%n + + + + + + ${log.path}/error.log + + ${log.path}/%d{yyyy-MM}/error.%d{yyyy-MM-dd}.%i.log.gz + 50MB + 30 + + + %date [%thread] %-5level [%logger{50}] %file:%line - %msg%n + + + ERROR + + + + + + + + + + + + + + + + + diff --git a/pigx-common/pigx-common-bom/pom.xml b/pigx-common/pigx-common-bom/pom.xml index c10094b83..bf7e9a338 100644 --- a/pigx-common/pigx-common-bom/pom.xml +++ b/pigx-common/pigx-common-bom/pom.xml @@ -26,6 +26,11 @@ + + org.springframework.cloud + pigx-common-bom + 3.2 + com.pig4cloud pigx-common-core diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/Sensitive.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/Sensitive.java new file mode 100644 index 000000000..a78f4422a --- /dev/null +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/Sensitive.java @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.core.sensitive; + +import com.fasterxml.jackson.annotation.JacksonAnnotationsInside; +import com.fasterxml.jackson.databind.annotation.JsonSerialize; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +/** + * 对象脱敏注解 + * + * @author mayee + * @version v1.0 + **/ +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.FIELD) +@JacksonAnnotationsInside +@JsonSerialize(using = SensitiveSerialize.class) +public @interface Sensitive { + + /** + * 脱敏数据类型, 非Customer时, 将忽略 refixNoMaskLen 和 suffixNoMaskLen 和 maskStr + */ + SensitiveTypeEnum type() default SensitiveTypeEnum.CUSTOMER; + + /** + * 前置不需要打码的长度 + */ + int prefixNoMaskLen() default 0; + + /** + * 后置不需要打码的长度 + */ + int suffixNoMaskLen() default 0; + + /** + * 用什么打码 + */ + String maskStr() default "*"; + +} diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/SensitiveSerialize.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/SensitiveSerialize.java new file mode 100644 index 000000000..a1b683ebd --- /dev/null +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/SensitiveSerialize.java @@ -0,0 +1,107 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.core.sensitive; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.BeanProperty; +import com.fasterxml.jackson.databind.JsonMappingException; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; +import com.fasterxml.jackson.databind.ser.ContextualSerializer; +import com.google.common.base.Preconditions; +import com.pig4cloud.pigx.common.core.util.DesensitizedUtils; +import lombok.AllArgsConstructor; +import lombok.NoArgsConstructor; + +import java.io.IOException; +import java.util.Objects; + +/** + * @author lengleng + * @date 2019-08-13 + *

+ * 脱敏序列化 + */ +@NoArgsConstructor +@AllArgsConstructor +public class SensitiveSerialize extends JsonSerializer implements ContextualSerializer { + + private SensitiveTypeEnum type; + private Integer prefixNoMaskLen; + private Integer suffixNoMaskLen; + private String maskStr; + + @Override + public void serialize(final String origin, final JsonGenerator jsonGenerator, + final SerializerProvider serializerProvider) throws IOException { + Preconditions.checkNotNull(type, "Sensitive type enum should not be null."); + switch (type) { + case CHINESE_NAME: + jsonGenerator.writeString(DesensitizedUtils.chineseName(origin)); + break; + case ID_CARD: + jsonGenerator.writeString(DesensitizedUtils.idCardNum(origin)); + break; + case FIXED_PHONE: + jsonGenerator.writeString(DesensitizedUtils.fixedPhone(origin)); + break; + case MOBILE_PHONE: + jsonGenerator.writeString(DesensitizedUtils.mobilePhone(origin)); + break; + case ADDRESS: + jsonGenerator.writeString(DesensitizedUtils.address(origin)); + break; + case EMAIL: + jsonGenerator.writeString(DesensitizedUtils.email(origin)); + break; + case BANK_CARD: + jsonGenerator.writeString(DesensitizedUtils.bankCard(origin)); + break; + case PASSWORD: + jsonGenerator.writeString(DesensitizedUtils.password(origin)); + break; + case KEY: + jsonGenerator.writeString(DesensitizedUtils.key(origin)); + break; + case CUSTOMER: + jsonGenerator.writeString(DesensitizedUtils.desValue(origin, prefixNoMaskLen, suffixNoMaskLen, maskStr)); + break; + default: + throw new IllegalArgumentException("Unknow sensitive type enum " + type); + } + } + + @Override + public JsonSerializer createContextual(final SerializerProvider serializerProvider, + final BeanProperty beanProperty) throws JsonMappingException { + if (beanProperty != null) { + if (Objects.equals(beanProperty.getType().getRawClass(), String.class)) { + Sensitive sensitive = beanProperty.getAnnotation(Sensitive.class); + if (sensitive == null) { + sensitive = beanProperty.getContextAnnotation(Sensitive.class); + } + if (sensitive != null) { + return new SensitiveSerialize(sensitive.type(), sensitive.prefixNoMaskLen(), sensitive.suffixNoMaskLen(), sensitive.maskStr()); + } + } + return serializerProvider.findValueSerializer(beanProperty.getType(), beanProperty); + } + return serializerProvider.findNullValueSerializer(null); + } +} + diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/SensitiveTypeEnum.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/SensitiveTypeEnum.java new file mode 100644 index 000000000..afb212840 --- /dev/null +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/sensitive/SensitiveTypeEnum.java @@ -0,0 +1,70 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.core.sensitive; + +/** + * 敏感信息枚举类 + * + * @author mayee + * @version v1.0 + **/ +public enum SensitiveTypeEnum { + + /** + * 自定义 + */ + CUSTOMER, + /** + * 用户名, 刘*华, 徐* + */ + CHINESE_NAME, + /** + * 身份证号, 110110********1234 + */ + ID_CARD, + /** + * 座机号, ****1234 + */ + FIXED_PHONE, + /** + * 手机号, 176****1234 + */ + MOBILE_PHONE, + /** + * 地址, 北京******** + */ + ADDRESS, + /** + * 电子邮件, s*****o@xx.com + */ + EMAIL, + /** + * 银行卡, 622202************1234 + */ + BANK_CARD, + /** + * 密码, 永远是 ******, 与长度无关 + */ + PASSWORD, + /** + * 密钥, 永远是 ******, 与长度无关 + */ + KEY; + + +} diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/DesensitizedUtils.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/DesensitizedUtils.java new file mode 100644 index 000000000..3724496e2 --- /dev/null +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/util/DesensitizedUtils.java @@ -0,0 +1,181 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.core.util; + + +import org.apache.commons.lang.StringUtils; + +/** + * 脱敏工具类 + * + * @author mayee + * @version v1.0 + **/ +public class DesensitizedUtils { + + /** + * 对字符串进行脱敏操作 + * + * @param origin 原始字符串 + * @param prefixNoMaskLen 左侧需要保留几位明文字段 + * @param suffixNoMaskLen 右侧需要保留几位明文字段 + * @param maskStr 用于遮罩的字符串, 如'*' + * @return 脱敏后结果 + */ + public static String desValue(String origin, int prefixNoMaskLen, int suffixNoMaskLen, String maskStr) { + if (origin == null) { + return null; + } + + StringBuilder sb = new StringBuilder(); + for (int i = 0, n = origin.length(); i < n; i++) { + if (i < prefixNoMaskLen) { + sb.append(origin.charAt(i)); + continue; + } + if (i > (n - suffixNoMaskLen - 1)) { + sb.append(origin.charAt(i)); + continue; + } + sb.append(maskStr); + } + return sb.toString(); + } + + /** + * 【中文姓名】只显示最后一个汉字,其他隐藏为星号,比如:**梦 + * + * @param fullName 姓名 + * @return 结果 + */ + public static String chineseName(String fullName) { + if (fullName == null) { + return null; + } + return desValue(fullName, 0, 1, "*"); + } + + /** + * 【身份证号】显示前六位, 四位,其他隐藏。共计18位或者15位,比如:340304*******1234 + * + * @param id 身份证号码 + * @return 结果 + */ + public static String idCardNum(String id) { + return desValue(id, 6, 4, "*"); + } + + /** + * 【固定电话】后四位,其他隐藏,比如 ****1234 + * + * @param num 固定电话 + * @return 结果 + */ + public static String fixedPhone(String num) { + return desValue(num, 0, 4, "*"); + } + + /** + * 【手机号码】前三位,后四位,其他隐藏,比如135****6810 + * + * @param num 手机号码 + * @return 结果 + */ + public static String mobilePhone(String num) { + return desValue(num, 3, 4, "*"); + } + + /** + * 【地址】只显示到地区,不显示详细地址,比如:北京市海淀区**** + * + * @param address 地址 + * @return 结果 + */ + public static String address(String address) { + return desValue(address, 6, 0, "*"); + } + + /** + * 【电子邮箱 邮箱前缀仅显示第一个字母,前缀其他隐藏,用星号代替,@及后面的地址显示,比如:d**@126.com + * + * @param email 电子邮箱 + * @return 结果 + */ + public static String email(String email) { + if (email == null) { + return null; + } + int index = StringUtils.indexOf(email, "@"); + if (index <= 1) { + return email; + } + String preEmail = desValue(email.substring(0, index), 1, 0, "*"); + return preEmail + email.substring(index); + + } + + /** + * 【银行卡号】前六位,后四位,其他用星号隐藏每位1个星号,比如:622260**********1234 + * + * @param cardNum 银行卡号 + * @return 结果 + */ + public static String bankCard(String cardNum) { + return desValue(cardNum, 6, 4, "*"); + } + + /** + * 【密码】密码的全部字符都用*代替,比如:****** + * + * @param password 密码 + * @return 结果 + */ + public static String password(String password) { + if (password == null) { + return null; + } + return "******"; + } + + /** + * 【密钥】密钥除了最后三位,全部都用*代替,比如:***xdS + * 脱敏后长度为6,如果明文长度不足三位,则按实际长度显示,剩余位置补* + * + * @param key 密钥 + * @return 结果 + */ + public static String key(String key) { + if (key == null) { + return null; + } + int viewLength = 6; + StringBuilder tmpKey = new StringBuilder(desValue(key, 0, 3, "*")); + if (tmpKey.length() > viewLength) { + return tmpKey.substring(tmpKey.length() - viewLength); + } else if (tmpKey.length() < viewLength) { + int buffLength = viewLength - tmpKey.length(); + for (int i = 0; i < buffLength; i++) { + tmpKey.insert(0, "*"); + } + return tmpKey.toString(); + } else { + return tmpKey.toString(); + } + } + +} diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/xss/RequestWrapper.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/xss/RequestWrapper.java new file mode 100644 index 000000000..3ab28bf8b --- /dev/null +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/xss/RequestWrapper.java @@ -0,0 +1,141 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.core.xss; + +import lombok.extern.slf4j.Slf4j; +import org.springframework.util.ObjectUtils; +import org.springframework.util.StreamUtils; +import org.springframework.web.util.HtmlUtils; + +import javax.servlet.ReadListener; +import javax.servlet.ServletInputStream; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletRequestWrapper; +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStreamReader; + +/** + * @author lengleng + * @date 2019-08-13 + *

+ * 1. XSS过滤 + * 2. body重复读 + */ +@Slf4j +public class RequestWrapper extends HttpServletRequestWrapper { + private final byte[] body; + + RequestWrapper(HttpServletRequest request) { + super(request); + this.body = getByteBody(request); + } + + @Override + public BufferedReader getReader() { + return ObjectUtils.isEmpty(body) ? null + : new BufferedReader(new InputStreamReader(getInputStream())); + } + + @Override + public ServletInputStream getInputStream() { + final ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(body); + return new ServletInputStream() { + @Override + public boolean isFinished() { + return false; + } + + @Override + public boolean isReady() { + return false; + } + + @Override + public void setReadListener(ReadListener readListener) { + + } + + @Override + public int read() { + return byteArrayInputStream.read(); + } + }; + } + + @Override + public String[] getParameterValues(String name) { + String[] values = super.getParameterValues(name); + if (values == null) { + return null; + } + int count = values.length; + String[] encodedValues = new String[count]; + for (int i = 0; i < count; i++) { + encodedValues[i] = HtmlUtils.htmlEscape(values[i]); + } + return encodedValues; + } + + @Override + public String getParameter(String name) { + String value = super.getParameter(name); + if (value == null) { + return null; + } + return HtmlUtils.htmlEscape(value); + } + + @Override + public Object getAttribute(String name) { + Object value = super.getAttribute(name); + if (value instanceof String) { + HtmlUtils.htmlEscape((String) value); + } + return value; + } + + @Override + public String getHeader(String name) { + String value = super.getHeader(name); + if (value == null) { + return null; + } + return HtmlUtils.htmlEscape(value); + } + + @Override + public String getQueryString() { + String value = super.getQueryString(); + if (value == null) { + return null; + } + return HtmlUtils.htmlEscape(value); + } + + private static byte[] getByteBody(HttpServletRequest request) { + byte[] body = new byte[0]; + try { + body = StreamUtils.copyToByteArray(request.getInputStream()); + } catch (IOException e) { + log.error("解析流中数据异常", e); + } + return body; + } +} diff --git a/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/xss/RequestWrapperFilter.java b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/xss/RequestWrapperFilter.java new file mode 100644 index 000000000..a85833b4d --- /dev/null +++ b/pigx-common/pigx-common-core/src/main/java/com/pig4cloud/pigx/common/core/xss/RequestWrapperFilter.java @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.core.xss; + +import org.springframework.lang.NonNull; +import org.springframework.web.filter.OncePerRequestFilter; + +import javax.servlet.FilterChain; +import javax.servlet.ServletException; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; + +/** + * @author lengleng + * @date 2019-08-13 + *

+ * request 处理过滤器 + */ +public class RequestWrapperFilter extends OncePerRequestFilter { + + @Override + protected void doFilterInternal(@NonNull HttpServletRequest request, + @NonNull HttpServletResponse response, + @NonNull FilterChain filterChain) + throws ServletException, IOException { + filterChain.doFilter(new RequestWrapper(request), response); + } +} \ No newline at end of file diff --git a/pigx-common/pigx-common-core/src/main/resources/META-INF/spring.factories b/pigx-common/pigx-common-core/src/main/resources/META-INF/spring.factories index 8bdf5ad3b..9499b6652 100644 --- a/pigx-common/pigx-common-core/src/main/resources/META-INF/spring.factories +++ b/pigx-common/pigx-common-core/src/main/resources/META-INF/spring.factories @@ -2,4 +2,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ com.pig4cloud.pigx.common.core.config.JacksonConfig,\ com.pig4cloud.pigx.common.core.config.MessageSourceConfig,\ com.pig4cloud.pigx.common.core.config.RestTemplateConfig,\ - com.pig4cloud.pigx.common.core.util.SpringContextHolder + com.pig4cloud.pigx.common.core.util.SpringContextHolder,\ + com.pig4cloud.pigx.common.core.xss.RequestWrapperFilter diff --git a/pigx-common/pigx-common-datasource/src/main/java/com/pig4cloud/pigx/common/datasource/support/DataSourceConstants.java b/pigx-common/pigx-common-datasource/src/main/java/com/pig4cloud/pigx/common/datasource/support/DataSourceConstants.java index 7c9502ad6..af57b82fc 100644 --- a/pigx-common/pigx-common-datasource/src/main/java/com/pig4cloud/pigx/common/datasource/support/DataSourceConstants.java +++ b/pigx-common/pigx-common-datasource/src/main/java/com/pig4cloud/pigx/common/datasource/support/DataSourceConstants.java @@ -10,7 +10,7 @@ public interface DataSourceConstants { /** * 查询数据源的SQL */ - String QUERY_DS_SQL = "select * from sys_datasource_conf where del_flag = 0"; + String QUERY_DS_SQL = "select * from gen_datasource_conf where del_flag = 0"; /** * 动态路由KEY diff --git a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/cache/DynamicRouteCacheListener.java b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/cache/DynamicRouteCacheListener.java new file mode 100644 index 000000000..71a30ac04 --- /dev/null +++ b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/cache/DynamicRouteCacheListener.java @@ -0,0 +1,50 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.gateway.cache; + +import lombok.AllArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.data.redis.connection.Message; +import org.springframework.data.redis.connection.MessageListener; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +/** + * @author lengleng + * @date 2019-08-16 + *

+ * redis 消息订阅 + */ +@Slf4j +@Component +@AllArgsConstructor +public class DynamicRouteCacheListener implements MessageListener { + private final RedisTemplate redisTemplate; + + @Override + public void onMessage(Message message, byte[] bytes) { + byte[] body = message.getBody(); + byte[] channel = message.getChannel(); + String msg = redisTemplate.getStringSerializer().deserialize(body); + String topic = redisTemplate.getStringSerializer().deserialize(channel); + + log.info("监听到 {} , 消息内容为 {}", msg, topic); + RouteCacheUtil.removeRouteList(); + } + +} diff --git a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/cache/RouteCacheUtil.java b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/cache/RouteCacheUtil.java new file mode 100644 index 000000000..e71cc2a64 --- /dev/null +++ b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/cache/RouteCacheUtil.java @@ -0,0 +1,67 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.common.gateway.cache; + +import cn.hutool.cache.Cache; +import cn.hutool.cache.CacheUtil; +import com.pig4cloud.pigx.common.gateway.vo.RouteDefinitionVo; +import lombok.experimental.UtilityClass; + +import java.util.ArrayList; +import java.util.List; + +/** + * @author lengleng + * @date 2019-08-16 + *

+ * 路由缓存 + */ +@UtilityClass +public class RouteCacheUtil { + private Cache cache = CacheUtil.newLFUCache(50); + + /** + * 获取缓存的全部对象 + * + * @return routeList + */ + public List getRouteList() { + List routeList = new ArrayList<>(); + cache.forEach(route -> routeList.add(route)); + return routeList; + } + + /** + * 更新缓存 + * + * @param routeList 缓存列表 + */ + public void setRouteList(List routeList) { + routeList.forEach(route -> { + cache.put(route.getId(), route); + }); + } + + /** + * 清空缓存 + */ + public void removeRouteList() { + cache.clear(); + } + +} diff --git a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/DynamicRouteAutoConfiguration.java b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/DynamicRouteAutoConfiguration.java index 80a3a9865..ac51a8198 100644 --- a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/DynamicRouteAutoConfiguration.java +++ b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/configuration/DynamicRouteAutoConfiguration.java @@ -17,11 +17,16 @@ package com.pig4cloud.pigx.common.gateway.configuration; +import com.pig4cloud.pigx.common.core.constant.CacheConstants; +import com.pig4cloud.pigx.common.gateway.cache.DynamicRouteCacheListener; import org.springframework.cloud.gateway.config.GatewayProperties; import org.springframework.cloud.gateway.config.PropertiesRouteDefinitionLocator; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.listener.ChannelTopic; +import org.springframework.data.redis.listener.RedisMessageListenerContainer; /** * @author lengleng @@ -43,4 +48,25 @@ public class DynamicRouteAutoConfiguration { return new PropertiesRouteDefinitionLocator(new GatewayProperties()); } + /** + * redis 监听配置 + * + * @param redisConnectionFactory redis 配置 + * @param cacheListener 业务处理 + * @return + */ + @Bean + public RedisMessageListenerContainer redisContainer(RedisConnectionFactory redisConnectionFactory + , DynamicRouteCacheListener cacheListener) { + RedisMessageListenerContainer container + = new RedisMessageListenerContainer(); + container.setConnectionFactory(redisConnectionFactory); + container.addMessageListener(cacheListener, topic()); + return container; + } + + @Bean + public ChannelTopic topic() { + return new ChannelTopic(CacheConstants.ROUTE_KEY); + } } diff --git a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/support/RedisRouteDefinitionWriter.java b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/support/RedisRouteDefinitionWriter.java index 90cf47c56..2fb7e06bc 100644 --- a/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/support/RedisRouteDefinitionWriter.java +++ b/pigx-common/pigx-common-gateway/src/main/java/com/pig4cloud/pigx/common/gateway/support/RedisRouteDefinitionWriter.java @@ -17,7 +17,9 @@ package com.pig4cloud.pigx.common.gateway.support; +import cn.hutool.core.collection.CollUtil; import com.pig4cloud.pigx.common.core.constant.CacheConstants; +import com.pig4cloud.pigx.common.gateway.cache.RouteCacheUtil; import com.pig4cloud.pigx.common.gateway.vo.RouteDefinitionVo; import lombok.AllArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -31,7 +33,6 @@ import org.springframework.stereotype.Component; import reactor.core.publisher.Flux; import reactor.core.publisher.Mono; -import java.util.ArrayList; import java.util.List; /** @@ -71,21 +72,27 @@ public class RedisRouteDefinitionWriter implements RouteDefinitionRepository { /** * 动态路由入口 + *

+ * 1. 先从内存中获取 + * 2. 为空加载Redis中数据 + * 3. 更新内存 * * @return */ @Override public Flux getRouteDefinitions() { + List routeList = RouteCacheUtil.getRouteList(); + if (CollUtil.isNotEmpty(routeList)) { + log.debug("内存 中路由定义条数: {}, {}", routeList.size(), routeList); + return Flux.fromIterable(routeList); + } + redisTemplate.setKeySerializer(new StringRedisSerializer()); redisTemplate.setHashValueSerializer(new Jackson2JsonRedisSerializer<>(RouteDefinitionVo.class)); List values = redisTemplate.opsForHash().values(CacheConstants.ROUTE_KEY); - List definitionList = new ArrayList<>(); - values.forEach(vo -> { - RouteDefinition routeDefinition = new RouteDefinition(); - BeanUtils.copyProperties(vo, routeDefinition); - definitionList.add(vo); - }); - log.debug("redis 中路由定义条数: {}, {}", definitionList.size(), definitionList); - return Flux.fromIterable(definitionList); + log.debug("redis 中路由定义条数: {}, {}", values.size(), values); + + RouteCacheUtil.setRouteList(values); + return Flux.fromIterable(values); } } diff --git a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/vo/UserVO.java b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/vo/UserVO.java index 3a3f30b78..a3543e856 100644 --- a/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/vo/UserVO.java +++ b/pigx-upms/pigx-upms-api/src/main/java/com/pig4cloud/pigx/admin/api/vo/UserVO.java @@ -20,6 +20,8 @@ package com.pig4cloud.pigx.admin.api.vo; import com.pig4cloud.pigx.admin.api.entity.SysRole; +import com.pig4cloud.pigx.common.core.sensitive.Sensitive; +import com.pig4cloud.pigx.common.core.sensitive.SensitiveTypeEnum; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; @@ -90,6 +92,7 @@ public class UserVO implements Serializable { /** * 手机号 */ + @Sensitive(type = SensitiveTypeEnum.MOBILE_PHONE) @ApiModelProperty(value = "手机号") private String phone; /** diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/PigxAdminApplication.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/PigxAdminApplication.java index e2c6481c9..0c4518277 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/PigxAdminApplication.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/PigxAdminApplication.java @@ -29,6 +29,7 @@ import org.springframework.cloud.client.SpringCloudApplication; /** * @author lengleng * @date 2018年06月21日 + *

* 用户统一管理系统 */ @EnablePigxSwagger2 diff --git a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/SysLogController.java b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/SysLogController.java index dacf29fa4..9bbf16ebd 100644 --- a/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/SysLogController.java +++ b/pigx-upms/pigx-upms-biz/src/main/java/com/pig4cloud/pigx/admin/controller/SysLogController.java @@ -25,10 +25,12 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.pig4cloud.pigx.admin.api.entity.SysLog; import com.pig4cloud.pigx.admin.api.vo.PreLogVo; import com.pig4cloud.pigx.admin.service.SysLogService; +import com.pig4cloud.pigx.common.core.constant.CacheConstants; import com.pig4cloud.pigx.common.core.util.R; import com.pig4cloud.pigx.common.security.annotation.Inner; import io.swagger.annotations.Api; import lombok.AllArgsConstructor; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.security.access.prepost.PreAuthorize; import org.springframework.web.bind.annotation.*; @@ -49,6 +51,7 @@ import java.util.List; @Api(value = "log", tags = "日志管理模块") public class SysLogController { private final SysLogService sysLogService; + private final RedisTemplate redisTemplate; /** * 简单分页查询 @@ -71,6 +74,7 @@ public class SysLogController { @DeleteMapping("/{id}") @PreAuthorize("@pms.hasPermission('sys_log_del')") public R removeById(@PathVariable Long id) { + redisTemplate.convertAndSend(CacheConstants.ROUTE_KEY, id); return R.ok(sysLogService.removeById(id)); } diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenFormConfController.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenFormConfController.java new file mode 100755 index 000000000..09d765366 --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/GenFormConfController.java @@ -0,0 +1,126 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.codegen.controller; + +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.pig4cloud.pigx.codegen.entity.GenFormConf; +import com.pig4cloud.pigx.codegen.service.GenFormConfService; +import com.pig4cloud.pigx.common.core.util.R; +import com.pig4cloud.pigx.common.log.annotation.SysLog; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import lombok.AllArgsConstructor; +import org.springframework.security.access.prepost.PreAuthorize; +import org.springframework.web.bind.annotation.*; + + +/** + * 表单管理 + * + * @author lengleng + * @date 2019-08-12 15:55:35 + */ +@RestController +@AllArgsConstructor +@RequestMapping("/form") +@Api(value = "form", tags = "表单管理") +public class GenFormConfController { + private final GenFormConfService genRecordService; + + /** + * 分页查询 + * + * @param page 分页对象 + * @param genRecord 生成记录 + * @return + */ + @ApiOperation(value = "分页查询", notes = "分页查询") + @GetMapping("/page") + public R getGenFormConfPage(Page page, GenFormConf genRecord) { + return R.ok(genRecordService.page(page, Wrappers.query(genRecord))); + } + + + /** + * 通过id查询生成记录 + * + * @param id id + * @return R + */ + @ApiOperation(value = "通过id查询", notes = "通过id查询") + @GetMapping("/{id}") + public R getById(@PathVariable("id") Integer id) { + return R.ok(genRecordService.getById(id)); + } + + /** + * 通过id查询生成记录 + * + * @param tableName tableName + * @return R + */ + @ApiOperation(value = "通过tableName查询表单信息") + @GetMapping("/info/{tableName}") + public R form(@PathVariable String tableName) { + return R.ok(genRecordService.getForm(tableName)); + } + + /** + * 新增生成记录 + * + * @param genRecord 生成记录 + * @return R + */ + @ApiOperation(value = "新增生成记录", notes = "新增生成记录") + @SysLog("新增生成记录") + @PostMapping + @PreAuthorize("@pms.hasPermission('gen_form_add')") + public R save(@RequestBody GenFormConf genRecord) { + return R.ok(genRecordService.save(genRecord)); + } + + /** + * 修改生成记录 + * + * @param genRecord 生成记录 + * @return R + */ + @ApiOperation(value = "修改生成记录", notes = "修改生成记录") + @SysLog("修改生成记录") + @PutMapping + @PreAuthorize("@pms.hasPermission('gen_form_edit')") + public R updateById(@RequestBody GenFormConf genRecord) { + return R.ok(genRecordService.updateById(genRecord)); + } + + /** + * 通过id删除生成记录 + * + * @param id id + * @return R + */ + @ApiOperation(value = "通过id删除生成记录", notes = "通过id删除生成记录") + @SysLog("通过id删除生成记录") + @DeleteMapping("/{id}") + @PreAuthorize("@pms.hasPermission('gen_form_del')") + public R removeById(@PathVariable Integer id) { + return R.ok(genRecordService.removeById(id)); + } + +} diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/SysDsConfController.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/SysDsConfController.java index 6ff0ddb19..c19e3853a 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/SysDsConfController.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/SysDsConfController.java @@ -18,8 +18,8 @@ package com.pig4cloud.pigx.codegen.controller; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; -import com.pig4cloud.pigx.codegen.entity.SysDatasourceConf; -import com.pig4cloud.pigx.codegen.service.SysDatasourceConfService; +import com.pig4cloud.pigx.codegen.entity.GenDatasourceConf; +import com.pig4cloud.pigx.codegen.service.GenDatasourceConfService; import com.pig4cloud.pigx.common.core.util.R; import com.pig4cloud.pigx.common.log.annotation.SysLog; import lombok.AllArgsConstructor; @@ -29,25 +29,25 @@ import org.springframework.web.bind.annotation.*; /** * 数据源管理 * - * @author pigx code generator + * @author lengleng * @date 2019-03-31 16:00:20 */ @RestController @AllArgsConstructor @RequestMapping("/dsconf") public class SysDsConfController { - private final SysDatasourceConfService sysDatasourceConfService; + private final GenDatasourceConfService datasourceConfService; /** * 分页查询 * - * @param page 分页对象 - * @param sysDatasourceConf 数据源表 + * @param page 分页对象 + * @param datasourceConf 数据源表 * @return */ @GetMapping("/page") - public R getSysDatasourceConfPage(Page page, SysDatasourceConf sysDatasourceConf) { - return R.ok(sysDatasourceConfService.page(page, Wrappers.query(sysDatasourceConf))); + public R getSysDatasourceConfPage(Page page, GenDatasourceConf datasourceConf) { + return R.ok(datasourceConfService.page(page, Wrappers.query(datasourceConf))); } /** @@ -57,7 +57,7 @@ public class SysDsConfController { */ @GetMapping("/list") public R list() { - return R.ok(sysDatasourceConfService.list()); + return R.ok(datasourceConfService.list()); } @@ -69,19 +69,19 @@ public class SysDsConfController { */ @GetMapping("/{id}") public R getById(@PathVariable("id") Integer id) { - return R.ok(sysDatasourceConfService.getById(id)); + return R.ok(datasourceConfService.getById(id)); } /** * 新增数据源表 * - * @param sysDatasourceConf 数据源表 + * @param datasourceConf 数据源表 * @return R */ @SysLog("新增数据源表") @PostMapping - public R save(@RequestBody SysDatasourceConf sysDatasourceConf) { - return R.ok(sysDatasourceConfService.saveDsByEnc(sysDatasourceConf)); + public R save(@RequestBody GenDatasourceConf datasourceConf) { + return R.ok(datasourceConfService.saveDsByEnc(datasourceConf)); } /** @@ -92,8 +92,8 @@ public class SysDsConfController { */ @SysLog("修改数据源表") @PutMapping - public R updateById(@RequestBody SysDatasourceConf sysDatasourceConf) { - return R.ok(sysDatasourceConfService.updateDsByEnc(sysDatasourceConf)); + public R updateById(@RequestBody GenDatasourceConf sysDatasourceConf) { + return R.ok(datasourceConfService.updateDsByEnc(sysDatasourceConf)); } /** @@ -105,7 +105,7 @@ public class SysDsConfController { @SysLog("删除数据源表") @DeleteMapping("/{id}") public R removeById(@PathVariable Integer id) { - return R.ok(sysDatasourceConfService.removeById(id)); + return R.ok(datasourceConfService.removeById(id)); } } diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/SysGeneratorController.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/SysGeneratorController.java index ef695d687..7eb603072 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/SysGeneratorController.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/controller/SysGeneratorController.java @@ -20,7 +20,7 @@ package com.pig4cloud.pigx.codegen.controller; import cn.hutool.core.io.IoUtil; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.pig4cloud.pigx.codegen.entity.GenConfig; -import com.pig4cloud.pigx.codegen.service.SysGeneratorService; +import com.pig4cloud.pigx.codegen.service.GeneratorService; import com.pig4cloud.pigx.common.core.util.R; import lombok.AllArgsConstructor; import lombok.SneakyThrows; @@ -39,7 +39,7 @@ import javax.servlet.http.HttpServletResponse; @AllArgsConstructor @RequestMapping("/generator") public class SysGeneratorController { - private final SysGeneratorService sysGeneratorService; + private final GeneratorService GeneratorService; /** * 列表 @@ -50,7 +50,7 @@ public class SysGeneratorController { */ @GetMapping("/page") public R getPage(Page page, String tableName, Integer id) { - return R.ok(sysGeneratorService.getPage(page, tableName, id)); + return R.ok(GeneratorService.getPage(page, tableName, id)); } /** @@ -59,7 +59,7 @@ public class SysGeneratorController { @SneakyThrows @PostMapping("/code") public void generatorCode(@RequestBody GenConfig genConfig, HttpServletResponse response) { - byte[] data = sysGeneratorService.generatorCode(genConfig); + byte[] data = GeneratorService.generatorCode(genConfig); response.reset(); response.setHeader(HttpHeaders.CONTENT_DISPOSITION, String.format("attachment; filename=%s.zip", genConfig.getTableName())); response.addHeader(HttpHeaders.CONTENT_LENGTH, String.valueOf(data.length)); diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/SysDatasourceConf.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenDatasourceConf.java similarity index 93% rename from pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/SysDatasourceConf.java rename to pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenDatasourceConf.java index 218d8c969..e47696200 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/SysDatasourceConf.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenDatasourceConf.java @@ -28,13 +28,13 @@ import java.time.LocalDateTime; /** * 数据源表 * - * @author pigx code generator + * @author lengleng * @date 2019-03-31 16:00:20 */ @Data -@TableName("sys_datasource_conf") +@TableName("gen_datasource_conf") @EqualsAndHashCode(callSuper = true) -public class SysDatasourceConf extends Model { +public class GenDatasourceConf extends Model { private static final long serialVersionUID = 1L; /** diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenFormConf.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenFormConf.java new file mode 100755 index 000000000..94eaf4fa6 --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/entity/GenFormConf.java @@ -0,0 +1,84 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.codegen.entity; + +import com.baomidou.mybatisplus.annotation.TableId; +import com.baomidou.mybatisplus.annotation.TableName; +import com.baomidou.mybatisplus.extension.activerecord.Model; +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; +import lombok.EqualsAndHashCode; + +import java.time.LocalDateTime; + +/** + * 生成记录 + * + * @author lengleng + * @date 2019-08-12 15:55:35 + */ +@Data +@TableName("gen_form_conf") +@EqualsAndHashCode(callSuper = true) +@ApiModel(value = "生成记录") +public class GenFormConf extends Model { + private static final long serialVersionUID = 1L; + + /** + * ID + */ + @TableId + @ApiModelProperty(value = "ID") + private Integer id; + /** + * 所属数据源 + */ + @ApiModelProperty(value = "所属数据源") + private String dsName; + /** + * 表名称 + */ + @ApiModelProperty(value = "表名称") + private String tableName; + /** + * 表单信息 + */ + @ApiModelProperty(value = "表单信息") + private String formInfo; + /** + * 创建时间 + */ + @ApiModelProperty(value = "创建时间") + private LocalDateTime createTime; + /** + * 修改时间 + */ + @ApiModelProperty(value = "修改时间") + private LocalDateTime updateTime; + /** + * 删除标记 + */ + @ApiModelProperty(value = "删除标记") + private String delFlag; + /** + * 所属租户 + */ + @ApiModelProperty(value = "所属租户", hidden = true) + private Integer tenantId; +} diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/SysDatasourceConfMapper.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GenDatasourceConfMapper.java similarity index 86% rename from pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/SysDatasourceConfMapper.java rename to pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GenDatasourceConfMapper.java index c44231e48..acc2e8586 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/SysDatasourceConfMapper.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GenDatasourceConfMapper.java @@ -17,14 +17,14 @@ package com.pig4cloud.pigx.codegen.mapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; -import com.pig4cloud.pigx.codegen.entity.SysDatasourceConf; +import com.pig4cloud.pigx.codegen.entity.GenDatasourceConf; /** * 数据源表 * - * @author pigx code generator + * @author lengleng * @date 2019-03-31 16:00:20 */ -public interface SysDatasourceConfMapper extends BaseMapper { +public interface GenDatasourceConfMapper extends BaseMapper { } diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GenFormConfMapper.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GenFormConfMapper.java new file mode 100755 index 000000000..74fa89326 --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GenFormConfMapper.java @@ -0,0 +1,31 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.codegen.mapper; + +import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.pig4cloud.pigx.codegen.entity.GenFormConf; + +/** + * 生成记录 + * + * @author lengleng + * @date 2019-08-12 15:55:35 + */ +public interface GenFormConfMapper extends BaseMapper { + +} diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/SysGeneratorMapper.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GeneratorMapper.java similarity index 97% rename from pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/SysGeneratorMapper.java rename to pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GeneratorMapper.java index 89fc35f93..2476633de 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/SysGeneratorMapper.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/mapper/GeneratorMapper.java @@ -30,7 +30,7 @@ import java.util.Map; * @author lengleng * @date 2018-07-30 */ -public interface SysGeneratorMapper { +public interface GeneratorMapper { /** * 分页查询表格 diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/SysDatasourceConfService.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GenDatasourceConfService.java similarity index 76% rename from pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/SysDatasourceConfService.java rename to pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GenDatasourceConfService.java index 8a2100605..c5e7e63a9 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/SysDatasourceConfService.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GenDatasourceConfService.java @@ -17,28 +17,28 @@ package com.pig4cloud.pigx.codegen.service; import com.baomidou.mybatisplus.extension.service.IService; -import com.pig4cloud.pigx.codegen.entity.SysDatasourceConf; +import com.pig4cloud.pigx.codegen.entity.GenDatasourceConf; /** * 数据源表 * - * @author pigx code generator + * @author lengleng * @date 2019-03-31 16:00:20 */ -public interface SysDatasourceConfService extends IService { +public interface GenDatasourceConfService extends IService { /** * 保存数据源并且加密 * - * @param sysDatasourceConf + * @param genDatasourceConf * @return */ - Boolean saveDsByEnc(SysDatasourceConf sysDatasourceConf); + Boolean saveDsByEnc(GenDatasourceConf genDatasourceConf); /** * 更新数据源 * - * @param sysDatasourceConf + * @param genDatasourceConf * @return */ - Boolean updateDsByEnc(SysDatasourceConf sysDatasourceConf); + Boolean updateDsByEnc(GenDatasourceConf genDatasourceConf); } diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GenFormConfService.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GenFormConfService.java new file mode 100755 index 000000000..f62efcde6 --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GenFormConfService.java @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ + +package com.pig4cloud.pigx.codegen.service; + +import com.baomidou.mybatisplus.extension.service.IService; +import com.pig4cloud.pigx.codegen.entity.GenFormConf; + +/** + * 表单管理 + * + * @author lengleng + * @date 2019-08-12 15:55:35 + */ +public interface GenFormConfService extends IService { + + /** + * 获取表单信息 + * + * @param tableName + * @return + */ + GenFormConf getForm(String tableName); +} diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/SysGeneratorService.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GeneratorService.java similarity index 97% rename from pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/SysGeneratorService.java rename to pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GeneratorService.java index 4e5644235..2013da9c5 100644 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/SysGeneratorService.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/GeneratorService.java @@ -28,7 +28,7 @@ import java.util.Map; * @author lengleng * @date 2018/7/29 */ -public interface SysGeneratorService { +public interface GeneratorService { /** * 生成代码 * diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/SysDatasourceConfServiceImpl.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenDatasourceConfServiceImpl.java similarity index 67% rename from pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/SysDatasourceConfServiceImpl.java rename to pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenDatasourceConfServiceImpl.java index 384ce0479..1a5a2969d 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/SysDatasourceConfServiceImpl.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenDatasourceConfServiceImpl.java @@ -18,9 +18,9 @@ package com.pig4cloud.pigx.codegen.service.impl; import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import com.pig4cloud.pigx.codegen.entity.SysDatasourceConf; -import com.pig4cloud.pigx.codegen.mapper.SysDatasourceConfMapper; -import com.pig4cloud.pigx.codegen.service.SysDatasourceConfService; +import com.pig4cloud.pigx.codegen.entity.GenDatasourceConf; +import com.pig4cloud.pigx.codegen.mapper.GenDatasourceConfMapper; +import com.pig4cloud.pigx.codegen.service.GenDatasourceConfService; import com.pig4cloud.pigx.common.datasource.config.DynamicDataSourceConfig; import lombok.AllArgsConstructor; import org.jasypt.encryption.StringEncryptor; @@ -29,40 +29,40 @@ import org.springframework.stereotype.Service; /** * 数据源表 * - * @author pigx code generator + * @author lengleng * @date 2019-03-31 16:00:20 */ @Service @AllArgsConstructor -public class SysDatasourceConfServiceImpl extends ServiceImpl implements SysDatasourceConfService { +public class GenDatasourceConfServiceImpl extends ServiceImpl implements GenDatasourceConfService { private final DynamicDataSourceConfig dynamicDataSourceConfig; private final StringEncryptor stringEncryptor; /** * 保存数据源并且加密 * - * @param sysDatasourceConf + * @param genDatasourceConf * @return */ @Override - public Boolean saveDsByEnc(SysDatasourceConf sysDatasourceConf) { - sysDatasourceConf.setPassword(stringEncryptor.encrypt(sysDatasourceConf.getPassword())); - this.baseMapper.insert(sysDatasourceConf); + public Boolean saveDsByEnc(GenDatasourceConf genDatasourceConf) { + genDatasourceConf.setPassword(stringEncryptor.encrypt(genDatasourceConf.getPassword())); + this.baseMapper.insert(genDatasourceConf); return dynamicDataSourceConfig.reload(); } /** * 更新数据源 * - * @param sysDatasourceConf + * @param genDatasourceConf * @return */ @Override - public Boolean updateDsByEnc(SysDatasourceConf sysDatasourceConf) { - if (StrUtil.isNotBlank(sysDatasourceConf.getPassword())) { - sysDatasourceConf.setPassword(stringEncryptor.encrypt(sysDatasourceConf.getPassword())); + public Boolean updateDsByEnc(GenDatasourceConf genDatasourceConf) { + if (StrUtil.isNotBlank(genDatasourceConf.getPassword())) { + genDatasourceConf.setPassword(stringEncryptor.encrypt(genDatasourceConf.getPassword())); } - this.baseMapper.updateById(sysDatasourceConf); + this.baseMapper.updateById(genDatasourceConf); return dynamicDataSourceConfig.reload(); } } diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenFormConfServiceImpl.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenFormConfServiceImpl.java new file mode 100755 index 000000000..9ea91edf1 --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GenFormConfServiceImpl.java @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2018-2025, lengleng All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * + * Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * Neither the name of the pig4cloud.com developer nor the names of its + * contributors may be used to endorse or promote products derived from + * this software without specific prior written permission. + * Author: lengleng (wangiegie@gmail.com) + */ +package com.pig4cloud.pigx.codegen.service.impl; + +import com.baomidou.mybatisplus.core.toolkit.Wrappers; +import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import com.pig4cloud.pigx.codegen.entity.GenFormConf; +import com.pig4cloud.pigx.codegen.mapper.GenFormConfMapper; +import com.pig4cloud.pigx.codegen.service.GenFormConfService; +import org.springframework.stereotype.Service; + +/** + * 表单管理 + * + * @author lengleng + * @date 2019-08-12 15:55:35 + */ +@Service +public class GenFormConfServiceImpl extends ServiceImpl implements GenFormConfService { + + @Override + public GenFormConf getForm(String tableName) { + return getOne(Wrappers.lambdaQuery() + .eq(GenFormConf::getTableName, tableName) + .orderByDesc(GenFormConf::getCreateTime), false); + } +} diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/SysGeneratorServiceImpl.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java similarity index 64% rename from pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/SysGeneratorServiceImpl.java rename to pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java index 6ca497974..939b6fd48 100755 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/SysGeneratorServiceImpl.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/service/impl/GeneratorServiceImpl.java @@ -18,11 +18,15 @@ package com.pig4cloud.pigx.codegen.service.impl; import cn.hutool.core.io.IoUtil; +import cn.hutool.core.util.StrUtil; import com.baomidou.mybatisplus.core.metadata.IPage; +import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.pig4cloud.pigx.codegen.entity.GenConfig; -import com.pig4cloud.pigx.codegen.mapper.SysGeneratorMapper; -import com.pig4cloud.pigx.codegen.service.SysGeneratorService; +import com.pig4cloud.pigx.codegen.entity.GenFormConf; +import com.pig4cloud.pigx.codegen.mapper.GenFormConfMapper; +import com.pig4cloud.pigx.codegen.mapper.GeneratorMapper; +import com.pig4cloud.pigx.codegen.service.GeneratorService; import com.pig4cloud.pigx.codegen.util.GenUtils; import com.pig4cloud.pigx.common.datasource.support.DynamicDataSourceContextHolder; import lombok.AllArgsConstructor; @@ -34,15 +38,16 @@ import java.util.Map; import java.util.zip.ZipOutputStream; /** - * 代码生成器 - * * @author lengleng * @date 2018-07-30 + *

+ * 代码生成器 */ @Service @AllArgsConstructor -public class SysGeneratorServiceImpl implements SysGeneratorService { - private final SysGeneratorMapper sysGeneratorMapper; +public class GeneratorServiceImpl implements GeneratorService { + private final GeneratorMapper generatorMapper; + private final GenFormConfMapper genRecordMapper; /** * 分页查询表 @@ -54,7 +59,7 @@ public class SysGeneratorServiceImpl implements SysGeneratorService { @Override public IPage>> getPage(Page page, String tableName, Integer id) { DynamicDataSourceContextHolder.setDataSourceType(id); - return sysGeneratorMapper.queryList(page, tableName); + return generatorMapper.queryList(page, tableName); } /** @@ -69,21 +74,28 @@ public class SysGeneratorServiceImpl implements SysGeneratorService { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); ZipOutputStream zip = new ZipOutputStream(outputStream); - //查询表信息 - Map table = queryTable(genConfig.getTableName()); - //查询列信息 - List> columns = queryColumns(genConfig.getTableName()); - //生成代码 - GenUtils.generatorCode(genConfig, table, columns, zip); + String tableNames = genConfig.getTableName(); + for (String tableName : StrUtil.split(tableNames, StrUtil.DASHED)) { + //查询表信息 + Map table = queryTable(tableName); + //查询列信息 + List> columns = queryColumns(tableName); + //根据tableName 查询最新的表单配置 + GenFormConf record = genRecordMapper.selectOne(Wrappers.lambdaQuery() + .eq(GenFormConf::getTableName, tableName) + .orderByDesc(GenFormConf::getCreateTime)); + //生成代码 + GenUtils.generatorCode(genConfig, table, columns, zip, record); + } IoUtil.close(zip); return outputStream.toByteArray(); } private Map queryTable(String tableName) { - return sysGeneratorMapper.queryTable(tableName); + return generatorMapper.queryTable(tableName); } private List> queryColumns(String tableName) { - return sysGeneratorMapper.queryColumns(tableName); + return generatorMapper.queryColumns(tableName); } } diff --git a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/GenUtils.java b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/GenUtils.java index 8bc51278a..50364c51b 100644 --- a/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/GenUtils.java +++ b/pigx-visual/pigx-codegen/src/main/java/com/pig4cloud/pigx/codegen/util/GenUtils.java @@ -23,9 +23,11 @@ import cn.hutool.core.util.CharsetUtil; import cn.hutool.core.util.StrUtil; import com.pig4cloud.pigx.codegen.entity.ColumnEntity; import com.pig4cloud.pigx.codegen.entity.GenConfig; +import com.pig4cloud.pigx.codegen.entity.GenFormConf; import com.pig4cloud.pigx.codegen.entity.TableEntity; import com.pig4cloud.pigx.common.core.constant.CommonConstants; import com.pig4cloud.pigx.common.core.exception.CheckedException; +import lombok.SneakyThrows; import lombok.experimental.UtilityClass; import lombok.extern.slf4j.Slf4j; import org.apache.commons.configuration.Configuration; @@ -38,7 +40,6 @@ import org.apache.velocity.VelocityContext; import org.apache.velocity.app.Velocity; import java.io.File; -import java.io.IOException; import java.io.StringWriter; import java.nio.charset.StandardCharsets; import java.util.*; @@ -85,8 +86,9 @@ public class GenUtils { /** * 生成代码 */ + @SneakyThrows public void generatorCode(GenConfig genConfig, Map table, - List> columns, ZipOutputStream zip) { + List> columns, ZipOutputStream zip, GenFormConf record) { //配置信息 Configuration config = getConfig(); boolean hasBigDecimal = false; @@ -124,9 +126,9 @@ public class GenUtils { columnEntity.setNullable("NO".equals(column.get("isNullable"))); columnEntity.setColumnType(column.get("columnType")); //隐藏不需要的在接口文档中展示的字段 - if(hiddenColumns.contains(column.get("columnName"))){ + if (hiddenColumns.contains(column.get("columnName"))) { columnEntity.setHidden(Boolean.TRUE); - }else{ + } else { columnEntity.setHidden(Boolean.FALSE); } //列名转换成Java属性名 @@ -199,22 +201,29 @@ public class GenUtils { //获取模板列表 List templates = getTemplates(); for (String template : templates) { + // 如果是crud + if (template.contains(CRUD_JS_VM) && record != null) { + zip.putNextEntry(new ZipEntry(Objects + .requireNonNull(getFileName(template, tableEntity.getCaseClassName() + , map.get("package").toString(), map.get("moduleName").toString())))); + IoUtil.write(zip, StandardCharsets.UTF_8, false, record.getFormInfo()); + zip.closeEntry(); + continue; + } + + //渲染模板 StringWriter sw = new StringWriter(); Template tpl = Velocity.getTemplate(template, CharsetUtil.UTF_8); tpl.merge(context, sw); - try { - //添加到zip - zip.putNextEntry(new ZipEntry(Objects - .requireNonNull(getFileName(template, tableEntity.getCaseClassName() - , map.get("package").toString(), map.get("moduleName").toString())))); - IoUtil.write(zip, StandardCharsets.UTF_8, false, sw.toString()); - IoUtil.close(sw); - zip.closeEntry(); - } catch (IOException e) { - throw new CheckedException("渲染模板失败,表名:" + tableEntity.getTableName(), e); - } + //添加到zip + zip.putNextEntry(new ZipEntry(Objects + .requireNonNull(getFileName(template, tableEntity.getCaseClassName() + , map.get("package").toString(), map.get("moduleName").toString())))); + IoUtil.write(zip, StandardCharsets.UTF_8, false, sw.toString()); + IoUtil.close(sw); + zip.closeEntry(); } } diff --git a/pigx-visual/pigx-codegen/src/main/resources/mapper/GenFormConfMapper.xml b/pigx-visual/pigx-codegen/src/main/resources/mapper/GenFormConfMapper.xml new file mode 100755 index 000000000..661bb3a78 --- /dev/null +++ b/pigx-visual/pigx-codegen/src/main/resources/mapper/GenFormConfMapper.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + diff --git a/pigx-visual/pigx-codegen/src/main/resources/mapper/SysGeneratorMapper.xml b/pigx-visual/pigx-codegen/src/main/resources/mapper/GeneratorMapper.xml similarity index 93% rename from pigx-visual/pigx-codegen/src/main/resources/mapper/SysGeneratorMapper.xml rename to pigx-visual/pigx-codegen/src/main/resources/mapper/GeneratorMapper.xml index 50eb0b8c4..58c36ecbf 100755 --- a/pigx-visual/pigx-codegen/src/main/resources/mapper/SysGeneratorMapper.xml +++ b/pigx-visual/pigx-codegen/src/main/resources/mapper/GeneratorMapper.xml @@ -1,7 +1,7 @@ - +