Commit e4ca7afc authored by dqjdda's avatar dqjdda
Browse files

阿里巴巴代码规范

parent 6d941c09
......@@ -8,7 +8,19 @@
<version>2.3</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<properties>
<hutool.version>[4.1.12,)</hutool.version>
</properties>
<artifactId>eladmin-common</artifactId>
<name>公共模块</name>
<dependencies>
<!--工具包-->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>${hutool.version}</version>
</dependency>
</dependencies>
</project>
\ No newline at end of file
......@@ -19,6 +19,9 @@ import org.springframework.stereotype.Component;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
/**
* @author /
*/
@Aspect
@Component
public class LimitAspect {
......
......@@ -18,4 +18,13 @@ public class BaseDTO implements Serializable {
private Timestamp createTime;
private Timestamp updateTime;
@Override
public String toString() {
return "BaseDTO{" +
"isDelete=" + isDelete +
", createTime=" + createTime +
", updateTime=" + updateTime +
'}';
}
}
......@@ -19,7 +19,7 @@ import java.lang.reflect.Field;
@MappedSuperclass
public class BaseEntity implements Serializable {
// 删除标识
/** 删除标识 **/
@Column(name = "is_delete", columnDefinition = "bit default 0")
private Boolean isDelete = false;
......
......@@ -10,21 +10,29 @@ public interface BaseMapper<D, E> {
/**
* DTO转Entity
* @param dto /
* @return /
*/
E toEntity(D dto);
/**
* Entity转DTO
* @param entity /
* @return /
*/
D toDto(E entity);
/**
* DTO集合转Entity集合
* @param dtoList /
* @return /
*/
List <E> toEntity(List<D> dtoList);
/**
* Entity集合转DTO集合
* @param entityList /
* @return /
*/
List <D> toDto(List<E> entityList);
}
......@@ -7,6 +7,9 @@ import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;
/**
* @author Zheng Jie
*/
@Service(value = "el")
public class ElPermissionConfig {
......
......@@ -53,6 +53,7 @@ public class RedisConfig extends CachingConfigurerSupport {
return configuration;
}
@SuppressWarnings("all")
@Bean(name = "redisTemplate")
@ConditionalOnMissingBean(name = "redisTemplate")
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
......@@ -65,13 +66,7 @@ public class RedisConfig extends CachingConfigurerSupport {
// 全局开启AutoType,这里方便开发,使用全局的方式
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
// 建议使用这种方式,小范围指定白名单
// ParserConfig.getGlobalInstance().addAccept("me.zhengjie.domain");
// ParserConfig.getGlobalInstance().addAccept("me.zhengjie.modules.system.service.dto");
// ParserConfig.getGlobalInstance().addAccept("me.zhengjie.service.dto");
// ParserConfig.getGlobalInstance().addAccept("me.zhengjie.modules.system.domain");
// ParserConfig.getGlobalInstance().addAccept("me.zhengjie.modules.quartz.domain");
// ParserConfig.getGlobalInstance().addAccept("me.zhengjie.modules.monitor.domain");
// ParserConfig.getGlobalInstance().addAccept("me.zhengjie.modules.security.security");
// ParserConfig.getGlobalInstance().addAccept("me.zhengjie.domain");
// key的序列化采用StringRedisSerializer
template.setKeySerializer(new StringRedisSerializer());
template.setHashKeySerializer(new StringRedisSerializer());
......@@ -86,7 +81,7 @@ public class RedisConfig extends CachingConfigurerSupport {
@Override
public KeyGenerator keyGenerator() {
return (target, method, params) -> {
Map<String,Object> container = new HashMap<>();
Map<String,Object> container = new HashMap<>(3);
Class<?> targetClassClass = target.getClass();
// 类地址
container.put("class",targetClassClass.toGenericString());
......
......@@ -81,7 +81,8 @@ public class GlobalExceptionHandler {
log.error(ThrowableUtil.getStackTrace(e));
String[] str = Objects.requireNonNull(e.getBindingResult().getAllErrors().get(0).getCodes())[1].split("\\.");
String message = e.getBindingResult().getAllErrors().get(0).getDefaultMessage();
if("不能为空".equals(message)){
String msg = "不能为空";
if(msg.equals(message)){
message = str[1] + ":" + message;
}
return buildResponseEntity(ApiError.error(message));
......
......@@ -15,25 +15,46 @@ import java.nio.charset.StandardCharsets;
*/
public class EncryptUtils {
private static String strKey = "Passw0rd", strParam = "Passw0rd";
private static String strParam = "Passw0rd";
private static Cipher cipher;
private static IvParameterSpec iv = new IvParameterSpec(strParam.getBytes(StandardCharsets.UTF_8));
private static DESKeySpec getDesKeySpec(String source) throws Exception {
if (source == null || source.length() == 0){
return null;
}
cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
String strKey = "Passw0rd";
return new DESKeySpec(strKey.getBytes(StandardCharsets.UTF_8));
}
/**
* 对称加密
*/
public static String desEncrypt(String source) throws Exception {
if (source == null || source.length() == 0){
return null;
}
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(strKey.getBytes(StandardCharsets.UTF_8));
DESKeySpec desKeySpec = getDesKeySpec(source);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(strParam.getBytes(StandardCharsets.UTF_8));
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
return byte2hex(
cipher.doFinal(source.getBytes(StandardCharsets.UTF_8))).toUpperCase();
}
/**
* 对称解密
*/
public static String desDecrypt(String source) throws Exception {
byte[] src = hex2byte(source.getBytes());
DESKeySpec desKeySpec = getDesKeySpec(source);
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] retByte = cipher.doFinal(src);
return new String(retByte);
}
private static String byte2hex(byte[] inStr) {
String stmp;
StringBuilder out = new StringBuilder(inStr.length * 2);
......@@ -50,35 +71,18 @@ public class EncryptUtils {
}
private static byte[] hex2byte(byte[] b) {
if ((b.length % 2) != 0){
int size = 2;
if ((b.length % size) != 0){
throw new IllegalArgumentException("长度不是偶数");
}
byte[] b2 = new byte[b.length / 2];
for (int n = 0; n < b.length; n += 2) {
for (int n = 0; n < b.length; n += size) {
String item = new String(b, n, 2);
b2[n / 2] = (byte) Integer.parseInt(item, 16);
}
return b2;
}
/**
* 对称解密
*/
public static String desDecrypt(String source) throws Exception {
if (source == null || source.length() == 0){
return null;
}
byte[] src = hex2byte(source.getBytes());
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
DESKeySpec desKeySpec = new DESKeySpec(strKey.getBytes(StandardCharsets.UTF_8));
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
IvParameterSpec iv = new IvParameterSpec(strParam.getBytes(StandardCharsets.UTF_8));
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
byte[] retByte = cipher.doFinal(src);
return new String(retByte);
}
/**
* 密码加密
*/
......
......@@ -119,8 +119,9 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
}
OutputStream os = new FileOutputStream(file);
int bytesRead;
byte[] buffer = new byte[8192];
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
int len = 8192;
byte[] buffer = new byte[len];
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.close();
......@@ -210,7 +211,9 @@ public class FileUtil extends cn.hutool.core.io.FileUtil {
}
public static void checkSize(long maxSize, long size) {
if(size > (maxSize * 1024 * 1024)){
// 1M
int len = 1024 * 1024;
if(size > (maxSize * len)){
throw new BadRequestException("文件超出规定大小");
}
}
......
......@@ -15,7 +15,7 @@ import java.util.*;
@Slf4j
public class QueryHelp {
@SuppressWarnings("unchecked")
@SuppressWarnings("all")
public static <R, Q> Predicate getPredicate(Root<R> root, Q query, CriteriaBuilder cb) {
List<Predicate> list = new ArrayList<>();
......
......@@ -15,12 +15,15 @@ import java.util.Calendar;
import java.util.Date;
/**
* @author Zheng Jie
* 字符串工具类, 继承org.apache.commons.lang3.StringUtils类
*/
public class StringUtils extends org.apache.commons.lang3.StringUtils {
private static final char SEPARATOR = '_';
private static final String UNKNOWN = "unknown";
/**
* 驼峰命名法工具
*
......@@ -111,19 +114,21 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
*/
public static String getIp(HttpServletRequest request) {
String ip = request.getHeader("x-forwarded-for");
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getHeader("WL-Proxy-Client-IP");
}
if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
if (ip == null || ip.length() == 0 || UNKNOWN.equalsIgnoreCase(ip)) {
ip = request.getRemoteAddr();
}
if (ip.contains(",")) {
String comma = ",";
String localhost = "127.0.0.1";
if (ip.contains(comma)) {
ip = ip.split(",")[0];
}
if ("127.0.0.1".equals(ip)) {
if (localhost.equals(ip)) {
// 获取本机真正的ip地址
try {
ip = InetAddress.getLocalHost().getHostAddress();
......@@ -149,7 +154,8 @@ public class StringUtils extends org.apache.commons.lang3.StringUtils {
DataBlock dataBlock;
dataBlock = (DataBlock) method.invoke(searcher, ip);
String address = dataBlock.getRegion().replace("0|","");
if(address.charAt(address.length()-1) == '|'){
char symbol = '|';
if(address.charAt(address.length()-1) == symbol){
address = address.substring(0,address.length() - 1);
}
return address.equals(ElAdminConstant.REGION)?"内网IP":address;
......
......@@ -7,6 +7,10 @@ import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
/**
* @author Zheng Jie
* 翻译工具类
*/
public class TranslatorUtil {
public static String translate(String word){
......
......@@ -23,40 +23,40 @@ public class ColumnInfo {
private String tableName;
// 数据库字段名称
/** 数据库字段名称 */
private String columnName;
// 数据库字段类型
/** 数据库字段类型 */
private String columnType;
// 数据库字段键类型
/** 数据库字段键类型 */
private String keyType;
// 字段额外的参数
/** 字段额外的参数 */
private String extra;
// 数据库字段描述
/** 数据库字段描述 */
private String remark;
// 必填
/** 必填 */
private Boolean notNull;
// 是否在列表显示
/** 是否在列表显示 */
private Boolean listShow;
// 是否表单显示
/** 是否表单显示 */
private Boolean formShow;
// 表单类型
/** 表单类型 */
private String formType;
// 查询 1:模糊 2:精确
/** 查询 1:模糊 2:精确 */
private String queryType;
// 字典名称
/** 字典名称 */
private String dictName;
// 日期注解
/** 日期注解 */
private String dateAnnotation;
public ColumnInfo(String tableName, String columnName, Boolean notNull, String columnType, String remark, String keyType, String extra) {
......
......@@ -30,29 +30,29 @@ public class GenConfig {
@NotBlank
private String tableName;
// 包路径
/** 包路径 */
@NotBlank
private String pack;
// 模块名
/** 模块名 */
@Column(name = "module_name")
@NotBlank
private String moduleName;
// 前端文件路径
/** 前端文件路径 */
@NotBlank
private String path;
// 前端文件路径
/** 前端文件路径 */
@Column(name = "api_path")
private String apiPath;
// 作者
/** 作者 */
private String author;
// 表前缀
/** 表前缀 */
private String prefix;
// 是否覆盖
/** 是否覆盖 */
private Boolean cover;
}
......@@ -14,19 +14,19 @@ import lombok.NoArgsConstructor;
@NoArgsConstructor
public class TableInfo {
// 表名称
/** 表名称 */
private Object tableName;
// 创建日期
/** 创建日期 */
private Object createTime;
// 数据库引擎
/** 数据库引擎 */
private Object engine;
// 编码集
/** 编码集 */
private Object coding;
// 备注
/** 备注 */
private Object remark;
......
......@@ -10,5 +10,10 @@ import java.util.List;
*/
public interface ColumnInfoRepository extends JpaRepository<ColumnInfo,Long> {
/**
* 查询表信息
* @param tableName 表格名
* @return 表信息
*/
List<ColumnInfo> findByTableNameOrderByIdAsc(String tableName);
}
......@@ -9,5 +9,10 @@ import org.springframework.data.jpa.repository.JpaRepository;
*/
public interface GenConfigRepository extends JpaRepository<GenConfig,Long> {
/**
* 查询表配置
* @param tableName 表名
* @return /
*/
GenConfig findByTableName(String tableName);
}
......@@ -8,7 +8,18 @@ import me.zhengjie.domain.GenConfig;
*/
public interface GenConfigService {
/**
* 查询表配置
* @param tableName 表名
* @return 表配置
*/
GenConfig find(String tableName);
/**
* 更新表配置
* @param tableName 表名
* @param genConfig 表配置
* @return 表配置
*/
GenConfig update(String tableName, GenConfig genConfig);
}
......@@ -39,14 +39,17 @@ public class GenConfigServiceImpl implements GenConfigService {
// 自动设置Api路径,注释掉前需要同步取消前端的注释
String separator = File.separator;
String[] paths;
if (separator.equals("\\")) {
String symbol = "\\";
if (symbol.equals(separator)) {
paths = genConfig.getPath().split("\\\\");
} else paths = genConfig.getPath().split(File.separator);
} else {
paths = genConfig.getPath().split(File.separator);
}
StringBuilder api = new StringBuilder();
for (String path : paths) {
api.append(path);
api.append(separator);
if (path.equals("src")) {
if ("src".equals(path)) {
api.append("api");
break;
}
......
Supports Markdown
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