Skip to content

Commit e4ca7af

Browse files
committed
阿里巴巴代码规范
1 parent 6d941c0 commit e4ca7af

160 files changed

Lines changed: 1600 additions & 738 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

eladmin-common/pom.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,19 @@
88
<version>2.3</version>
99
</parent>
1010
<modelVersion>4.0.0</modelVersion>
11+
<properties>
12+
<hutool.version>[4.1.12,)</hutool.version>
13+
</properties>
1114

1215
<artifactId>eladmin-common</artifactId>
1316
<name>公共模块</name>
17+
18+
<dependencies>
19+
<!--工具包-->
20+
<dependency>
21+
<groupId>cn.hutool</groupId>
22+
<artifactId>hutool-all</artifactId>
23+
<version>${hutool.version}</version>
24+
</dependency>
25+
</dependencies>
1426
</project>

eladmin-common/src/main/java/me/zhengjie/aspect/LimitAspect.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
import javax.servlet.http.HttpServletRequest;
2020
import java.lang.reflect.Method;
2121

22+
/**
23+
* @author /
24+
*/
2225
@Aspect
2326
@Component
2427
public class LimitAspect {

eladmin-common/src/main/java/me/zhengjie/base/BaseDTO.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,4 +18,13 @@ public class BaseDTO implements Serializable {
1818
private Timestamp createTime;
1919

2020
private Timestamp updateTime;
21+
22+
@Override
23+
public String toString() {
24+
return "BaseDTO{" +
25+
"isDelete=" + isDelete +
26+
", createTime=" + createTime +
27+
", updateTime=" + updateTime +
28+
'}';
29+
}
2130
}

eladmin-common/src/main/java/me/zhengjie/base/BaseEntity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
@MappedSuperclass
2020
public class BaseEntity implements Serializable {
2121

22-
// 删除标识
22+
/** 删除标识 **/
2323
@Column(name = "is_delete", columnDefinition = "bit default 0")
2424
private Boolean isDelete = false;
2525

eladmin-common/src/main/java/me/zhengjie/base/BaseMapper.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,21 +10,29 @@ public interface BaseMapper<D, E> {
1010

1111
/**
1212
* DTO转Entity
13+
* @param dto /
14+
* @return /
1315
*/
1416
E toEntity(D dto);
1517

1618
/**
1719
* Entity转DTO
20+
* @param entity /
21+
* @return /
1822
*/
1923
D toDto(E entity);
2024

2125
/**
2226
* DTO集合转Entity集合
27+
* @param dtoList /
28+
* @return /
2329
*/
2430
List <E> toEntity(List<D> dtoList);
2531

2632
/**
2733
* Entity集合转DTO集合
34+
* @param entityList /
35+
* @return /
2836
*/
2937
List <D> toDto(List<E> entityList);
3038
}

eladmin-common/src/main/java/me/zhengjie/config/ElPermissionConfig.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
import java.util.List;
88
import java.util.stream.Collectors;
99

10+
/**
11+
* @author Zheng Jie
12+
*/
1013
@Service(value = "el")
1114
public class ElPermissionConfig {
1215

eladmin-common/src/main/java/me/zhengjie/config/RedisConfig.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ public RedisCacheConfiguration redisCacheConfiguration(){
5353
return configuration;
5454
}
5555

56+
@SuppressWarnings("all")
5657
@Bean(name = "redisTemplate")
5758
@ConditionalOnMissingBean(name = "redisTemplate")
5859
public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisConnectionFactory) {
@@ -65,13 +66,7 @@ public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisC
6566
// 全局开启AutoType,这里方便开发,使用全局的方式
6667
ParserConfig.getGlobalInstance().setAutoTypeSupport(true);
6768
// 建议使用这种方式,小范围指定白名单
68-
// ParserConfig.getGlobalInstance().addAccept("me.zhengjie.domain");
69-
// ParserConfig.getGlobalInstance().addAccept("me.zhengjie.modules.system.service.dto");
70-
// ParserConfig.getGlobalInstance().addAccept("me.zhengjie.service.dto");
71-
// ParserConfig.getGlobalInstance().addAccept("me.zhengjie.modules.system.domain");
72-
// ParserConfig.getGlobalInstance().addAccept("me.zhengjie.modules.quartz.domain");
73-
// ParserConfig.getGlobalInstance().addAccept("me.zhengjie.modules.monitor.domain");
74-
// ParserConfig.getGlobalInstance().addAccept("me.zhengjie.modules.security.security");
69+
// ParserConfig.getGlobalInstance().addAccept("me.zhengjie.domain");
7570
// key的序列化采用StringRedisSerializer
7671
template.setKeySerializer(new StringRedisSerializer());
7772
template.setHashKeySerializer(new StringRedisSerializer());
@@ -86,7 +81,7 @@ public RedisTemplate<Object, Object> redisTemplate(RedisConnectionFactory redisC
8681
@Override
8782
public KeyGenerator keyGenerator() {
8883
return (target, method, params) -> {
89-
Map<String,Object> container = new HashMap<>();
84+
Map<String,Object> container = new HashMap<>(3);
9085
Class<?> targetClassClass = target.getClass();
9186
// 类地址
9287
container.put("class",targetClassClass.toGenericString());

eladmin-common/src/main/java/me/zhengjie/exception/handler/GlobalExceptionHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,8 @@ public ResponseEntity<ApiError> handleMethodArgumentNotValidException(MethodArgu
8181
log.error(ThrowableUtil.getStackTrace(e));
8282
String[] str = Objects.requireNonNull(e.getBindingResult().getAllErrors().get(0).getCodes())[1].split("\\.");
8383
String message = e.getBindingResult().getAllErrors().get(0).getDefaultMessage();
84-
if("不能为空".equals(message)){
84+
String msg = "不能为空";
85+
if(msg.equals(message)){
8586
message = str[1] + ":" + message;
8687
}
8788
return buildResponseEntity(ApiError.error(message));

eladmin-common/src/main/java/me/zhengjie/utils/EncryptUtils.java

Lines changed: 31 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,25 +15,46 @@
1515
*/
1616
public class EncryptUtils {
1717

18-
private static String strKey = "Passw0rd", strParam = "Passw0rd";
18+
private static String strParam = "Passw0rd";
19+
20+
private static Cipher cipher;
21+
22+
private static IvParameterSpec iv = new IvParameterSpec(strParam.getBytes(StandardCharsets.UTF_8));
23+
24+
private static DESKeySpec getDesKeySpec(String source) throws Exception {
25+
if (source == null || source.length() == 0){
26+
return null;
27+
}
28+
cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
29+
String strKey = "Passw0rd";
30+
return new DESKeySpec(strKey.getBytes(StandardCharsets.UTF_8));
31+
}
1932

2033
/**
2134
* 对称加密
2235
*/
2336
public static String desEncrypt(String source) throws Exception {
24-
if (source == null || source.length() == 0){
25-
return null;
26-
}
27-
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
28-
DESKeySpec desKeySpec = new DESKeySpec(strKey.getBytes(StandardCharsets.UTF_8));
37+
DESKeySpec desKeySpec = getDesKeySpec(source);
2938
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
3039
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
31-
IvParameterSpec iv = new IvParameterSpec(strParam.getBytes(StandardCharsets.UTF_8));
3240
cipher.init(Cipher.ENCRYPT_MODE, secretKey, iv);
3341
return byte2hex(
3442
cipher.doFinal(source.getBytes(StandardCharsets.UTF_8))).toUpperCase();
3543
}
3644

45+
/**
46+
* 对称解密
47+
*/
48+
public static String desDecrypt(String source) throws Exception {
49+
byte[] src = hex2byte(source.getBytes());
50+
DESKeySpec desKeySpec = getDesKeySpec(source);
51+
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
52+
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
53+
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
54+
byte[] retByte = cipher.doFinal(src);
55+
return new String(retByte);
56+
}
57+
3758
private static String byte2hex(byte[] inStr) {
3859
String stmp;
3960
StringBuilder out = new StringBuilder(inStr.length * 2);
@@ -50,35 +71,18 @@ private static String byte2hex(byte[] inStr) {
5071
}
5172

5273
private static byte[] hex2byte(byte[] b) {
53-
if ((b.length % 2) != 0){
74+
int size = 2;
75+
if ((b.length % size) != 0){
5476
throw new IllegalArgumentException("长度不是偶数");
5577
}
5678
byte[] b2 = new byte[b.length / 2];
57-
for (int n = 0; n < b.length; n += 2) {
79+
for (int n = 0; n < b.length; n += size) {
5880
String item = new String(b, n, 2);
5981
b2[n / 2] = (byte) Integer.parseInt(item, 16);
6082
}
6183
return b2;
6284
}
6385

64-
/**
65-
* 对称解密
66-
*/
67-
public static String desDecrypt(String source) throws Exception {
68-
if (source == null || source.length() == 0){
69-
return null;
70-
}
71-
byte[] src = hex2byte(source.getBytes());
72-
Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding");
73-
DESKeySpec desKeySpec = new DESKeySpec(strKey.getBytes(StandardCharsets.UTF_8));
74-
SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
75-
SecretKey secretKey = keyFactory.generateSecret(desKeySpec);
76-
IvParameterSpec iv = new IvParameterSpec(strParam.getBytes(StandardCharsets.UTF_8));
77-
cipher.init(Cipher.DECRYPT_MODE, secretKey, iv);
78-
byte[] retByte = cipher.doFinal(src);
79-
return new String(retByte);
80-
}
81-
8286
/**
8387
* 密码加密
8488
*/

eladmin-common/src/main/java/me/zhengjie/utils/FileUtil.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,8 +119,9 @@ static File inputStreamToFile(InputStream ins, String name) throws Exception{
119119
}
120120
OutputStream os = new FileOutputStream(file);
121121
int bytesRead;
122-
byte[] buffer = new byte[8192];
123-
while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
122+
int len = 8192;
123+
byte[] buffer = new byte[len];
124+
while ((bytesRead = ins.read(buffer, 0, len)) != -1) {
124125
os.write(buffer, 0, bytesRead);
125126
}
126127
os.close();
@@ -210,7 +211,9 @@ public static String getFileTypeByMimeType(String type) {
210211
}
211212

212213
public static void checkSize(long maxSize, long size) {
213-
if(size > (maxSize * 1024 * 1024)){
214+
// 1M
215+
int len = 1024 * 1024;
216+
if(size > (maxSize * len)){
214217
throw new BadRequestException("文件超出规定大小");
215218
}
216219
}

0 commit comments

Comments
 (0)