共计 3297 个字符,预计需要花费 9 分钟才能阅读完成。
我最新最全的文章都在 南瓜慢说 www.pkslow.com,欢送大家来喝茶!
1 数据库审计
数据库审计是指当数据库有记录变更时,能够记录数据库的变更工夫和变更人等,这样当前出问题回溯问责也比拟不便。对于审计表记录的变更能够两种形式,一种是建设一张审计表专门用于记录,另一种是在数据库减少字段。本文所探讨的是第二种计划。
那如何在新增、批改、删除的时候同时减少记录呢?如果每张表都独自记录,代码就会显得很冗余。更好的形式应该是做切面或者事件监听,当数据有变更时对立进行记录。
2 Spring Data JPA 审计
Spring Data JPA
为咱们提供了不便的 Audit
性能,通过四个注解来标记字段:
(1) @CreatedBy:创建人
(2) @CreatedDate:创立工夫
(3) @LastModifiedBy:最初批改人
(4) @LastModifiedDate:最初批改工夫
接下来咱们来看看怎么应用。
2.1 我的项目筹备
通过 Docker
启动 PostgreSQL
数据库:
docker run -itd \ | |
--name pkslow-postgres \ | |
-e POSTGRES_DB=pkslow \ | |
-e POSTGRES_USER=pkslow \ | |
-e POSTGRES_PASSWORD=pkslow \ | |
-e PGDATA=/var/lib/postgresql/data/pgdata \ | |
-p 5432:5432 \ | |
postgres:10 |
引入相干依赖:
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-web</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-data-jpa</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.postgresql</groupId> | |
<artifactId>postgresql</artifactId> | |
</dependency> | |
<dependency> | |
<groupId>org.springframework.boot</groupId> | |
<artifactId>spring-boot-starter-security</artifactId> | |
</dependency> |
Spring Security
不是必须的,这里应用它来获取用户名。配置的用户为:
spring.security.user.name=pkslow | |
spring.security.user.password=123456 |
2.2 创立实体父类
其实父类不是必须的,你能够在每个想 Audit
的实体类进行配置,但比拟麻烦,不如创立一个父类,再让想审计的子类都继承它:
@MappedSuperclass | |
@EntityListeners(AuditingEntityListener.class) | |
public class Auditable<U> { | |
@CreatedBy | |
@Column(name = "created_by") | |
private U createdBy; | |
@CreatedDate | |
@Column(name = "created_date") | |
private Date createdDate; | |
@LastModifiedBy | |
@Column(name = "last_modified_by") | |
private U lastModifiedBy; | |
@LastModifiedDate | |
@Column(name = "last_modified_date") | |
private Date lastModifiedDate; | |
// getter | |
//setter | |
} |
@MappedSuperclass
能够让其它子实体类继承相干的字段和属性;
@EntityListeners
设置监听类,会对 新增
和批改
进行回调解决。
有了父类之后,子类就简略了:
@Entity | |
@Table(name = "pkslow_users") | |
public class User extends Auditable<String> { | |
@Id | |
@GeneratedValue(strategy = GenerationType.AUTO) | |
private Long userId; | |
private String name; | |
private String email; | |
private String country; | |
private String website; | |
//getter setter | |
} |
2.3 如何获取名字
数据总是被批改的,咱们要提供一个获取批改人名字的接口,配置如下:
@Configuration | |
@EnableJpaAuditing(auditorAwareRef = "auditorProvider") | |
public class JpaAuditingConfiguration { | |
@Bean | |
public AuditorAware<String> auditorProvider() {return () -> { | |
String username = "system"; | |
SecurityContext context = SecurityContextHolder.getContext(); | |
if (context != null) {Authentication authentication = context.getAuthentication(); | |
if (authentication != null) {username = authentication.getName(); | |
} | |
} | |
String result = username; | |
return Optional.ofNullable(result); | |
}; | |
} | |
} |
这里配置的是通过 Spring Security
的Context
来获取登陆用户的名字,当然能够有其它计划,如获取申请头的某个字段等。
留神注解 @EnableJpaAuditing
开启了审计性能。
2.4 测试
咱们通过一个 Controller
来新增数据,看看会有什么成果:
@RestController | |
@RequestMapping("/user") | |
public class UserController { | |
@Autowired | |
private UserRepository userRepository; | |
@PostMapping | |
public User save(@RequestBody User user) {return userRepository.save(user); | |
} | |
} |
通过 curl
命令来测试如下:
$ curl 'http://localhost:8088/user' -X POST \ | |
> -H 'Content-Type: application/json' \ | |
> -H 'Authorization:Basic cGtzbG93OjEyMzQ1Ng==' \ | |
> -d '{ | |
> "name":"larry", | |
> "email":"admin@pkslow.com", | |
> "country":"China", | |
> "website":"www.pkslow.com" | |
> }'{"createdBy":"pkslow","createdDate":"2021-01-15T15:08:47.035+0000","lastModifiedBy":"pkslow","lastModifiedDate":"2021-01-15T15:08:47.035+0000","userId":7,"name":"larry","email":"admin@pkslow.com","country":"China","website":"www.pkslow.com"} |
查看数据库,曾经生成了审计记录:
3 总结
代码请查看:https://github.com/LarryDpk/p…
欢送关注微信公众号 <南瓜慢说>,将继续为你更新 …
多读书,多分享;多写作,多整顿。