仲灏小栈 仲灏小栈
首页
大前端
后端&运维
其他技术
生活
关于我
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)

仲灏

诚意, 正心, 格物, 致知
首页
大前端
后端&运维
其他技术
生活
关于我
收藏
  • 分类
  • 标签
  • 归档
GitHub (opens new window)
  • Java

    • java初学者maven项目建立
    • Maven学习笔记
    • Untitled
    • idea 插件推荐
    • springboot项目学习笔记
    • springboot项目学习笔记2 用户模块
      • 用户模块
      • 通用返回对象
      • 密码加密
    • springboot项目学习笔记3 分类模块
    • springboot项目学习笔记4 商品模块
    • springboot项目学习笔记6 订单模块
    • 开源的物联网平台thingsboard部署
    • Docker 学习
    • java版本管理 jenv使用
    • mac中安装卸载java
    • springboot学习上手
  • Docker

  • Jenkins

  • Nacos

  • SQL

  • Nginx

  • Windows

  • Linux

  • 虚拟机

  • Git

  • 网络

  • 其他

  • 后端&运维
  • Java
仲灏
2022-04-09
目录

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
  • 新建异常包: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
springboot项目学习笔记
springboot项目学习笔记3 分类模块

← springboot项目学习笔记 springboot项目学习笔记3 分类模块→

最近更新
01
vim日常使用记录
04-02
02
滑动窗口最大值
04-02
03
有效的字母异位词
04-02
更多文章>
Theme by Vdoing | Copyright © 2021-2025 izhaong | github | 蜀ICP备2021031194号
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式