要在Spring data mongodb 中使用@CreatedBy@LastModifiedBy@CreatedBy@LastModifiedBy  这四个注解

必须实现 SpringSecurityAuditorAware

官方代码

class SpringSecurityAuditorAware implements AuditorAware<User> {

  public User getCurrentAuditor() {

    Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

    if (authentication == null || !authentication.isAuthenticated()) {
return null;
} return ((MyUserDetails) authentication.getPrincipal()).getUser();
}
}

添加配置文件 XML

<mongo:auditing mapping-context-ref="customMappingContext" auditor-aware-ref="yourAuditorAwareImpl"/>

SpringBoot 配置方式

@Configuration
@EnableMongoAuditing
class Config { @Bean
public AuditorAware<AuditableUser> myAuditorProvider() {
return new AuditorAwareImpl();
}
}

使用注解

    @CreatedDate
private LocalDateTime createDate; @CreatedBy
private User createdBy; @LastModifiedBy
private User lastModifiedBy; @LastModifiedDate
private LocalDateTime lastModifiedDate;

所以,需要在你的用户实体,添加一个方法

  public User getUser() {
return new User(this.getUsername(),
this.getPassword(),
this.isEnabled(),
this.isAccountNonExpired(),
this.isCredentialsNonExpired(),
this.isAccountNonLocked(),
this.getAuthorities());
}

当Springdata insert或者save的时候会生成数据,而且你会发现,很坑爹

    "createDate" : ISODate("2017-10-25T07:06:09.730Z"),
"createdBy" : {
"password" : "$2a$10$LceZ8.WHHrsDRBi6NNitJe4oih/xnhJKUsbfkzLnmYuhTKY683qxm",
"username" : "athos7817",
"authorities" : [
{
"role" : "AUTH_ORDER_UPDATE",
"_class" : "org.springframework.security.core.authority.SimpleGrantedAuthority"
},
{
"role" : "AUTH_ORDER_ADD",
"_class" : "org.springframework.security.core.authority.SimpleGrantedAuthority"
},
//以下省略一万个权限
],
"accountNonExpired" : true,
"accountNonLocked" : true,
"credentialsNonExpired" : true,
"enabled" : true
}, "lastModifiedBy" : {
"password" : "$2a$10$LceZ8.WHHrsDRBi6NNitJe4oih/xnhJKUsbfkzLnmYuhTKY683qxm",
"username" : "athos7817",
"authorities" : [
{
"role" : "AUTH_ORDER_UPDATE",
"_class" : "org.springframework.security.core.authority.SimpleGrantedAuthority"
},
{
"role" : "AUTH_ORDER_ADD",
"_class" : "org.springframework.security.core.authority.SimpleGrantedAuthority"
},
//以下省略一万个权限
],
"accountNonExpired" : true,
"accountNonLocked" : true,
"credentialsNonExpired" : true,
"enabled" : true
},

谁需要那么多废数据,而且SpringSecurity User的构造方法,不允许传入null

  public User(String username, String password, Collection<? extends GrantedAuthority> authorities) {
this(username, password, true, true, true, true, authorities);
} public User(String username, String password, boolean enabled, boolean accountNonExpired, boolean credentialsNonExpired, boolean accountNonLocked, Collection<? extends GrantedAuthority> authorities) {
if (username != null && !"".equals(username) && password != null) {
this.username = username;
this.password = password;
this.enabled = enabled;
this.accountNonExpired = accountNonExpired;
this.credentialsNonExpired = credentialsNonExpired;
this.accountNonLocked = accountNonLocked;
this.authorities = Collections.unmodifiableSet(sortAuthorities(authorities));
} else {
throw new IllegalArgumentException("Cannot pass null or empty values to constructor");
}
}

做出修改 User 修改为 String

    @CreatedDate
private LocalDateTime createDate; @CreatedBy
private String createdBy; @LastModifiedBy
private String lastModifiedBy; @LastModifiedDate
private LocalDateTime lastModifiedDate;
    @Bean
public AuditorAware<String> auditorProvider() {
return new SpringSecurityAuditorAware();
}
/**
* Created by laizhenwei on 2017/10/25
*/
public class SpringSecurityAuditorAware implements AuditorAware<String> { public String getCurrentAuditor() {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication == null || !authentication.isAuthenticated()) {
return null;
}
return ((MyUser) authentication.getPrincipal()).getUsername();
}
}

结果

    "createDate" : ISODate("2017-10-25T07:35:46.636Z"),
"createdBy" : "laizhenwei",
"lastModifiedBy" : "laizhenwei",
"lastModifiedDate" : ISODate("2017-10-25T07:35:46.636Z")

Spring data mongodb @CreatedBy@LastModifiedBy@CreatedBy@LastModifiedBy SpringSecurityAuditorAware,只记录用户名的更多相关文章

  1. spring data mongodb 配置遇到的几个问题

    一. mongodb 2.2版本以上的配置 spring.data.mongodb.uri = mongodb://newlook:newlook@192.168.0.109:27017/admin ...

  2. spring data mongodb中,如果对象中的属性不想加入到数据库字段中

    spring data mongodb中,如果对象中的属性不想加入到数据库字段中,可加@Transient注解,声明为透明属性 spring data mongodb 官网帮助文档 http://ww ...

  3. Spring Data MongoDB example with Spring MVC 3.2

    Spring Data MongoDB example with Spring MVC 3.2 Here is another example web application built with S ...

  4. 使用Spring访问Mongodb的方法大全——Spring Data MongoDB查询指南

    1.概述 Spring Data MongoDB 是Spring框架访问mongodb的神器,借助它可以非常方便的读写mongo库.本文介绍使用Spring Data MongoDB来访问mongod ...

  5. Spring data mongodb 聚合,投射,内嵌数组文档分页.

    尽量别直接用 DBObject  ,Spring data mongodb 的api 本来就没什么多大用处,如果还直接用 DBObject 那么还需要自己去解析结果,说动做个对象映射,累不累 Spri ...

  6. JAVA 处理 Spring data mongodb 时区问题

    Spring data mongodb 查询出结果的时候会自动 + 8小时,所以我们看起来结果是对的 但是我们查询的时候,并不会自动 + 8小时,需要自己处理 解决方法 1   @JsonFormat ...

  7. Spring data mongodb ObjectId ,根据id日期条件查询,省略@CreatedDate注解

    先看看ObjectId 的json 结构,非常丰富,这里有唯一机器码,日期,时间戳等等,所以强烈建议ID 使用 ObjectId 类型,并且自带索引 Spring data mongodb 注解 @C ...

  8. Spring Data MongoDB 三:基本文档查询(Query、BasicQuery)(一)

    一.简单介绍 Spring Data  MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一 ...

  9. Introduction to Spring Data MongoDB

    Introduction to Spring Data MongoDB I just announced the new Spring 5 modules in REST With Spring: & ...

随机推荐

  1. CGroup Namspace

    CGroup 介绍 CGroup 是 Control Groups 的缩写,是 Linux 内核提供的一种可以限制.记录.隔离 进程组 (process groups) 所使用的物力资源 (如 cpu ...

  2. 【JDK1.8】JDK1.8集合源码阅读——Set汇总

    一.前言 这一篇里,我将对HashSet.LinkedHashSet.TreeSet进行汇总分析,并不打算一一进行详细介绍,因为JDK对Set的实现进行了取巧.我们都知道Set不允许出现相同的对象,而 ...

  3. JDK安装及Tomcat安装

    JDK安装及Tomcat安装 JDK 解压JDK到常用盘符 D为例 Tomcat安装 将tomcat.zip解压到常用的根目录下,我这里以D盘为例.这样就算安装好了! 接下来开始配置环境变量,打开环境 ...

  4. notepad++代码输出电话号

    在网上看到一个程序员找合租的消息,希望找一个程序员合租,所以电话号码以代码的形式输出,闲来没事打出来玩玩.首先新建一个test.java文件,并敲入下面代码: 1 public class test{ ...

  5. 从jvm源码看synchronzied

    synchronized的使用 synchronized关键字是Java中解决并发问题的一种常用方法,也是最简单的一种方法,其作用有三个:(1)互斥性:确保线程互斥的访问同步代码(2)可见性:保证共享 ...

  6. JDK的下载,安装与环境的配置

    JDK的全称是JavaSE Development Kit,即java开发工具包,是sun公司提供的一套用于开发java应用程序的开发包,它提供了编译.运行java程序所需的各种工具和资源,包括jav ...

  7. Entity Framework VS Mybatis 不同点剖析

    大家都知道Entity Framework是.NET系统当中的一个重量级的ORM框架 ,它采用了延迟加载的技术,使得服务端不用每次都去尝试连接数据库,从而增加了使用效率和 减少了不必要的开销.而myb ...

  8. 基于 React + Webpack 的音乐相册项目(下)

    上一篇我们完成了音乐相册里面的播放图片的功能,这一篇主要完成的是音乐相册里面的音乐播放器功能.最终让我们基于 React 的音乐相册图文并茂.有声有色. 我们主要从以下几个部分来展开: 数据准备 进度 ...

  9. Effective Java 第三版——31.使用限定通配符来增加API的灵活性

    Tips <Effective Java, Third Edition>一书英文版已经出版,这本书的第二版想必很多人都读过,号称Java四大名著之一,不过第二版2009年出版,到现在已经将 ...

  10. POJ 1830 开关问题 [高斯消元XOR]

    和上两题一样 Input 输入第一行有一个数K,表示以下有K组测试数据. 每组测试数据的格式如下: 第一行 一个数N(0 < N < 29) 第二行 N个0或者1的数,表示开始时N个开关状 ...