springboot项目学习笔记2 用户模块
# 用户模块
# 通用返回对象
- 新建包:
common
- 新建类:
ApiRestResponse
package com.izhaong.mall.common;
import com.izhaong.mall.exception.MallException;
public class ApiRestResponse<T> {
private static final int OK_CODE = 1000;
private static final String OK_MSG = "success";
private Integer status;
private String msg;
private T data;
public ApiRestResponse(Integer status, String msg, T data) {
this.status = status;
this.msg = msg;
this.data = data;
}
public ApiRestResponse(Integer status, String msg) {
this.status = status;
this.msg = msg;
}
public ApiRestResponse() {
this(OK_CODE, OK_MSG);
}
public static <T> ApiRestResponse<T> success() {
return new ApiRestResponse<>();
}
public static <T> ApiRestResponse<T> success(T result) {
ApiRestResponse<T> response = new ApiRestResponse<>();
response.setData(result);
return response;
}
public static <T> ApiRestResponse<T> error(Integer code, String msg) {
return new ApiRestResponse<>(code, msg);
}
public static <T> ApiRestResponse<T> error(MallException ex) {
return new ApiRestResponse<>(ex.getCode(), ex.getMsg());
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public T getData() {
return data;
}
public void setData(T data) {
this.data = data;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
新建异常包:
exception
新建异常类:
MallException
package com.izhaong.mall.exception; public class MallException { private final Integer code; private final String msg; public MallException(Integer code, String msg) { this.code = code; this.msg = msg; } public MallException(MallExceptionEnum exceptionEnum) { this(exceptionEnum.getCode(), exceptionEnum.getMsg()); } public Integer getCode() { return code; } public String getMsg() { return msg; } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24新建异常枚举:
MallExceptionEnum
package com.izhaong.mall.exception; public enum MallExceptionEnum { NEED_USER_NAME(10001, "用户名不能为空"); Integer code; String msg; MallExceptionEnum(Integer code, String msg) { this.code = code; this.msg = msg; } public Integer getCode() { return code; } public void setCode(Integer code) { this.code = code; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# 密码加密
使用MD5加密
新建软件包:
util
新建工具类:
MD5Utils
static
修饰 方便其他人调用package com.izhaong.mall.util; import com.izhaong.mall.common.Constant; import org.apache.tomcat.util.codec.binary.Base64; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; /** * @author izhaong */ public class MD5Utils { public static String getMD5Str(String strVal) throws NoSuchAlgorithmException { MessageDigest md5 = MessageDigest.getInstance("MD5"); return Base64.encodeBase64String(md5.digest((strVal + Constant.SAIT).getBytes())); } }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17防止暴力破解,对其加盐
定义常量:新建类:
common/Constant
package com.izhaong.mall.common; public class Constant { public static final String SAIT = "dfa@fs1231"; }
1
2
3
4
5
6
使用
service/UserServiceimpl.java
try { user.setPassword(MD5Utils.getMD5Str(password)); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); }
1
2
3
4
5
上次更新: 2022/06/05, 20:31:36