Просмотр исходного кода

feat:后端项目,修改数据表结构,给用户表添加accessKey和secretKey字段,在用户注册是自动生成

yang yi 11 месяцев назад
Родитель
Сommit
356e0e93f8

+ 2 - 0
sql/init.sql

@@ -10,6 +10,8 @@ CREATE TABLE  if NOT EXISTS `user` (
                                        `user_name` VARCHAR(255) NOT NULL COMMENT '用户名称',
                                        `user_avatar` VARCHAR(1024) DEFAULT NULL COMMENT '用户头像',
                                        `user_role` CHAR(32) NOT NULL DEFAULT '用户' COMMENT '用户角色',
+                                       `access_key` CHAR(32) NOT NULL COMMENT '访问接口的accessKey',
+                                       `secret_key` CHAR(32) NOT NULL COMMENT '访问接口的secretKey',
                                        `create_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
                                        `update_time` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
                                        `delete_flag` TINYINT(1) NOT NULL DEFAULT '0' COMMENT '删除标志,0:未删除,1:已删除'

+ 26 - 138
src/main/java/space/anyi/openAPI/model/user/User.java

@@ -4,15 +4,13 @@ import com.baomidou.mybatisplus.annotation.IdType;
 import com.baomidou.mybatisplus.annotation.TableField;
 import com.baomidou.mybatisplus.annotation.TableId;
 import com.baomidou.mybatisplus.annotation.TableName;
+import lombok.Data;
 
 import java.io.Serializable;
 import java.util.Date;
 
-/**
- * 用户表
- * @TableName user
- */
 @TableName(value ="user")
+@Data
 public class User implements Serializable {
     /**
      * 用户ID
@@ -43,151 +41,35 @@ public class User implements Serializable {
     /**
      * 用户角色
      */
-    private String userRole = "用户";
+    private String userRole;
 
     /**
-     * 创建时间
-     */
-    private Date createTime;
-
-    /**
-     * 更新时间
-     */
-    private Date updateTime;
-
-    /**
-     * 删除标志,0:未删除,1:已删除
-     */
-    private Integer deleteFlag;
-
-    @TableField(exist = false)
-    private static final long serialVersionUID = 1L;
-
-    /**
-     * 用户ID
-     */
-    public Long getId() {
-        return id;
-    }
-
-    /**
-     * 用户ID
-     */
-    public void setId(Long id) {
-        this.id = id;
-    }
-
-    /**
-     * 用户账号
-     */
-    public String getUserAccount() {
-        return userAccount;
-    }
-
-    /**
-     * 用户账号
-     */
-    public void setUserAccount(String userAccount) {
-        this.userAccount = userAccount;
-    }
-
-    /**
-     * 用户密码
-     */
-    public String getUserPassword() {
-        return userPassword;
-    }
-
-    /**
-     * 用户密码
-     */
-    public void setUserPassword(String userPassword) {
-        this.userPassword = userPassword;
-    }
-
-    /**
-     * 用户名称
-     */
-    public String getUserName() {
-        return userName;
-    }
-
-    /**
-     * 用户名称
-     */
-    public void setUserName(String userName) {
-        this.userName = userName;
-    }
-
-    /**
-     * 用户头像
+     * 访问接口的accessKey
      */
-    public String getUserAvatar() {
-        return userAvatar;
-    }
-
-    /**
-     * 用户头像
-     */
-    public void setUserAvatar(String userAvatar) {
-        this.userAvatar = userAvatar;
-    }
+    private String accessKey;
 
     /**
-     * 用户角色
+     * 访问接口的secretKey
      */
-    public String getUserRole() {
-        return userRole;
-    }
-
-    /**
-     * 用户角色
-     */
-    public void setUserRole(String userRole) {
-        this.userRole = userRole;
-    }
-
-    /**
-     * 创建时间
-     */
-    public Date getCreateTime() {
-        return createTime;
-    }
+    private String secretKey;
 
     /**
      * 创建时间
      */
-    public void setCreateTime(Date createTime) {
-        this.createTime = createTime;
-    }
-
-    /**
-     * 更新时间
-     */
-    public Date getUpdateTime() {
-        return updateTime;
-    }
+    private Date createTime;
 
     /**
      * 更新时间
      */
-    public void setUpdateTime(Date updateTime) {
-        this.updateTime = updateTime;
-    }
+    private Date updateTime;
 
     /**
      * 删除标志,0:未删除,1:已删除
      */
-    public Integer getDeleteFlag() {
-        return deleteFlag;
-    }
+    private Integer deleteFlag;
 
-    /**
-     * 删除标志,0:未删除,1:已删除
-     */
-    public void setDeleteFlag(Integer deleteFlag) {
-        this.deleteFlag = deleteFlag;
-    }
+    @TableField(exist = false)
+    private static final long serialVersionUID = 1L;
 
     @Override
     public boolean equals(Object that) {
@@ -202,14 +84,16 @@ public class User implements Serializable {
         }
         User other = (User) that;
         return (this.getId() == null ? other.getId() == null : this.getId().equals(other.getId()))
-            && (this.getUserAccount() == null ? other.getUserAccount() == null : this.getUserAccount().equals(other.getUserAccount()))
-            && (this.getUserPassword() == null ? other.getUserPassword() == null : this.getUserPassword().equals(other.getUserPassword()))
-            && (this.getUserName() == null ? other.getUserName() == null : this.getUserName().equals(other.getUserName()))
-            && (this.getUserAvatar() == null ? other.getUserAvatar() == null : this.getUserAvatar().equals(other.getUserAvatar()))
-            && (this.getUserRole() == null ? other.getUserRole() == null : this.getUserRole().equals(other.getUserRole()))
-            && (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()))
-            && (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime()))
-            && (this.getDeleteFlag() == null ? other.getDeleteFlag() == null : this.getDeleteFlag().equals(other.getDeleteFlag()));
+                && (this.getUserAccount() == null ? other.getUserAccount() == null : this.getUserAccount().equals(other.getUserAccount()))
+                && (this.getUserPassword() == null ? other.getUserPassword() == null : this.getUserPassword().equals(other.getUserPassword()))
+                && (this.getUserName() == null ? other.getUserName() == null : this.getUserName().equals(other.getUserName()))
+                && (this.getUserAvatar() == null ? other.getUserAvatar() == null : this.getUserAvatar().equals(other.getUserAvatar()))
+                && (this.getUserRole() == null ? other.getUserRole() == null : this.getUserRole().equals(other.getUserRole()))
+                && (this.getAccessKey() == null ? other.getAccessKey() == null : this.getAccessKey().equals(other.getAccessKey()))
+                && (this.getSecretKey() == null ? other.getSecretKey() == null : this.getSecretKey().equals(other.getSecretKey()))
+                && (this.getCreateTime() == null ? other.getCreateTime() == null : this.getCreateTime().equals(other.getCreateTime()))
+                && (this.getUpdateTime() == null ? other.getUpdateTime() == null : this.getUpdateTime().equals(other.getUpdateTime()))
+                && (this.getDeleteFlag() == null ? other.getDeleteFlag() == null : this.getDeleteFlag().equals(other.getDeleteFlag()));
     }
 
     @Override
@@ -222,6 +106,8 @@ public class User implements Serializable {
         result = prime * result + ((getUserName() == null) ? 0 : getUserName().hashCode());
         result = prime * result + ((getUserAvatar() == null) ? 0 : getUserAvatar().hashCode());
         result = prime * result + ((getUserRole() == null) ? 0 : getUserRole().hashCode());
+        result = prime * result + ((getAccessKey() == null) ? 0 : getAccessKey().hashCode());
+        result = prime * result + ((getSecretKey() == null) ? 0 : getSecretKey().hashCode());
         result = prime * result + ((getCreateTime() == null) ? 0 : getCreateTime().hashCode());
         result = prime * result + ((getUpdateTime() == null) ? 0 : getUpdateTime().hashCode());
         result = prime * result + ((getDeleteFlag() == null) ? 0 : getDeleteFlag().hashCode());
@@ -240,6 +126,8 @@ public class User implements Serializable {
         sb.append(", userName=").append(userName);
         sb.append(", userAvatar=").append(userAvatar);
         sb.append(", userRole=").append(userRole);
+        sb.append(", accessKey=").append(accessKey);
+        sb.append(", secretKey=").append(secretKey);
         sb.append(", createTime=").append(createTime);
         sb.append(", updateTime=").append(updateTime);
         sb.append(", deleteFlag=").append(deleteFlag);

+ 13 - 0
src/main/java/space/anyi/openAPI/service/impl/UserServiceImpl.java

@@ -2,10 +2,13 @@ package space.anyi.openAPI.service.impl;
 
 
 import cn.hutool.core.util.IdUtil;
+import cn.hutool.core.util.RandomUtil;
+import cn.hutool.crypto.digest.MD5;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
 import com.github.benmanes.caffeine.cache.Cache;
+import org.apache.catalina.security.SecurityUtil;
 import org.springframework.security.authentication.AuthenticationManager;
 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
 import org.springframework.security.core.Authentication;
@@ -25,6 +28,7 @@ import space.anyi.openAPI.model.user.vo.UserVO;
 import space.anyi.openAPI.service.UserService;
 import space.anyi.openAPI.util.BeanCopyUtil;
 import space.anyi.openAPI.util.JwtUtil;
+import space.anyi.openAPI.util.SecurityUtils;
 
 import javax.annotation.Resource;
 import java.util.*;
@@ -42,6 +46,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
     private Cache<String,Object> localCache;
     @Resource
     private  PasswordEncoder passwordEncoder;
+    //生成accessKey和secretKey使用的盐
+    private String slat  = "yangyi";
+
     @Override
     public ResponseResult login(User user) {
         //认证
@@ -104,6 +111,12 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements Us
         //生成ID
         long id = IdUtil.getSnowflake(1, 1).nextId();
         user.setId(id);
+        //生成accessKey和secretKey
+        MD5 md5 = MD5.create();
+        String accessKey = md5.digestHex("access_" + slat + user.getUserAccount() + RandomUtil.randomNumbers(4));
+        String secretKeyKey = md5.digestHex("secret_" + slat + user.getUserAccount() + RandomUtil.randomNumbers(4));
+        user.setAccessKey(accessKey);
+        user.setSecretKey(secretKeyKey);
         //存入数据库
         save(user);
         return ResponseResult.okResult();

+ 5 - 2
src/main/resources/mapper/UserMapper.xml

@@ -10,7 +10,9 @@
             <result property="userPassword" column="user_password" jdbcType="VARCHAR"/>
             <result property="userName" column="user_name" jdbcType="VARCHAR"/>
             <result property="userAvatar" column="user_avatar" jdbcType="VARCHAR"/>
-            <result property="userRole" column="user_role" jdbcType="VARCHAR"/>
+            <result property="userRole" column="user_role" jdbcType="CHAR"/>
+            <result property="accessKey" column="access_key" jdbcType="CHAR"/>
+            <result property="secretKey" column="secret_key" jdbcType="CHAR"/>
             <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
             <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
             <result property="deleteFlag" column="delete_flag" jdbcType="TINYINT"/>
@@ -19,6 +21,7 @@
     <sql id="Base_Column_List">
         id,user_account,user_password,
         user_name,user_avatar,user_role,
-        create_time,update_time,delete_flag
+        access_key,secret_key,create_time,
+        update_time,delete_flag
     </sql>
 </mapper>