Skip to content

springboot项目学习笔记2 用户模块

仲灏2022-04-09约 1 分钟

用户模块

通用返回对象

  • 新建包:common
  • 新建类:ApiRestResponse
java
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;
    }
}
  • 新建异常包:exception

  • 新建异常类:MallException

    java
    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;
        }
    }
  • 新建异常枚举:MallExceptionEnum

    java
    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;
        }
    }

密码加密

使用MD5加密

  • 新建软件包:util

  • 新建工具类:MD5Utils

    static修饰 方便其他人调用

    java
    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()));
        }
    }
  • 防止暴力破解,对其加盐

    • 定义常量:新建类:common/Constant

      java
      package com.izhaong.mall.common;
      
      public class Constant {
          public static final String SAIT = "dfa@fs1231";
      }
  • 使用

    service/UserServiceimpl.java

    java
            try {
                user.setPassword(MD5Utils.getMD5Str(password));
            } catch (NoSuchAlgorithmException e) {
                e.printStackTrace();
            }

讨论区

欢迎留下想法与补充