SpringBoot从零到全栈商城(三)审计信息自动生成和lombox工具

762 阅读2分钟

章节介绍

数据库的表一般都有这四个字段,创建时间,更新时间,创建者,更新者。SpringBoot 审计功能可以自动更新这四个字段,无需手动赋值。本文主要介绍了审计功能的实现,以及自动写set和get的lombok工具。

项目介绍

记录SpringBoot从零到实现一整个商城后端的过程。

项目组成及技术栈

  • 接口 SpingBoot + JPA + mySql
  • 后台 vue + vue-element-admin
  • 移动端 uniapp + colorui + vuex + scss

entity实体类

先贴一下我项目中包含lombok和审计功能的实体类

package com.smxy.mall.entity;

import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.Data;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;

import javax.persistence.*;
import java.util.Date;

@Entity
@EntityListeners(AuditingEntityListener.class)
@Table(name = "fat_user")
@Data
public class User {
    @Id //主键Id
    @Column(name="id")
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private int id;

    @CreatedDate
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    @Column(name = "createTime",updatable = false,nullable=false)
    private Date createTime; //创建时间
    @LastModifiedDate
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
    private Date updateTime; //更新时间
    @CreatedBy
    @Column(name = "createUser",updatable = false,nullable=false)
    private String createUser; //创建者
    @LastModifiedBy
    private String updateUser; //更新者

    private String userName; //账号
    private String userPsw;  //密码
    private String phone; //手机号
    private String openId; //微信标识
    private String type; //用户类型

    private String name;  //昵称姓名
    private String img; //头像
    private String sex; //性别
    private int defaultId; //默认地址
}

@Entity代表这是一个数据库表对应的实体类 @Table(name = "fat_user") name对应数据库表名 @EntityListeners(AuditingEntityListener.class) 审计功能创建者和更新者需要用到 @Data注解 可以自动生成属性 set和get方法

lombok工具类

  • pom依赖
<dependency>
    <groupId>org.projectlombok</groupId>
    <artifactId>lombok</artifactId>
</dependency>
  • idea 点击File-- Settings设置界面,安装Lombok插件
  • 实体类添加注解@Data

审计功能(创建时间,更新时间,创建者,更新者)

  • 创建时间
@CreatedDate
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8")
@Column(name = "createTime",updatable = false,nullable=false)
private Date createTime; //创建时间

@CreatedDate 创建时间注解 @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",timezone = "GMT+8") 对时间进行格式化 @Column(name = "createTime",updatable = false,nullable=false) updatable,nullable设为false,解决更新操作时创建时间变null

  • 更新时间 使用注解@LastModifiedDate

  • 创建者

@CreatedBy
@Column(name = "createUser",updatable = false,nullable=false)
private String createUser; //创建者

@CreatedBy 创建者注解 @Column(name = "createUser",updatable = false,nullable=false) 防止更新时创建者变null

  • 更新者 使用注解 @LastModifiedBy

创建者,更新者需额外添加配置

创建类AuditorConfig

package com.smxy.mall.config;

import com.smxy.mall.model.Audience;
import com.smxy.mall.model.Current;
import com.smxy.mall.utils.JwtTokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.AuditorAware;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Optional;

@Component
public class AuditorConfig implements AuditorAware<String> {

    @Autowired
    private Audience audience;

    @Override
    public Optional<String> getCurrentAuditor() {
        // 这里应根据实际业务情况获取具体信息
        ServletRequestAttributes sra = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        String token = sra.getRequest().getHeader("token");
        String auth = "";
        if(StringUtils.isEmpty(token)){
            auth = "未有token";
        }else{
            Current current = JwtTokenUtil.getCurrentUser(token,audience.getBase64Secret());
            auth = current.getUserId()+"-"+current.getUserName();
        }
        return Optional.of(auth);
    }
}

通过获取header中的token,解析token获取当前用户的信息,通过 return Optional.of(当前用户标识,String)

启动类

启动类添加注解 @EnableJpaAuditing

效果