要在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. js中键盘按键对应的键值

    js键盘键值 keycode    8 = BackSpace BackSpace  keycode    9 = Tab Tab  keycode   12 = Clear  keycode   1 ...

  2. linkin大话数据结构--数组

    数组概述:如何正确理解数组?数组也是一种类型 数组是多个相同类型数据的组合,实现对这些数据的统一管理.数组属引用类型,数组型数据是对象(Object),数组中的每个元素相当于该对象的成员变量数组中的元 ...

  3. NW.js 简介与使用

    简介 (1)以网络最流行的技术编写原生应用程序的新方法 (2)基于HTML5, CSS3, JS and WebGL而编写 (3)完全支持nodejs所有api及第三方模块 (4)可以使用DOM直接调 ...

  4. Mysql的主从配置

    前言:这次学习分布式的思想要配置mysql的主从复制和读写分离,我在主从配置上踩到很多坑,在此演示一遍配置过程,并附上问题的说明和自己的一些见解 Mysql主从复制的原理 附上原理图: mysql的主 ...

  5. JMeter打开jmx文件报错解决方法

    错误提示: Error Problem loading XMLfrom:'D:\software\apace-jmeter-3.3\bin\线程组jmx', missing class com.tho ...

  6. JDK安装及Tomcat安装

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

  7. 渗透测试,form对象类型转换,简单demo

    最近公司的项目在进行国家某行业的安全检测,涉及到项目安全渗透等方面的问题: 参与项目的渗透等改造,是一个机遇与挑战,今后对与项目安全等方面会思考更多: 下面说说form表单对象提交,为了防止抓包,后台 ...

  8. 怎么使用IDEA

    war 和 war exploded war部署首先通过IDEA生成.war工程文件,然后将WEB工程以包的形式上传到服务器,因此会替代服务器本来同名的web app项目. war exploded模 ...

  9. Opensshd 源码升级

    OPenssh 下载地址: http://www.openssh.com/ 以下步骤 OS: 6.x  安装Opensshd 7.4p1 验证通过 6.8  安装Opensshd 7.6p1 验证通过 ...

  10. c# HTTP技术

    种植头发能完全成活吗?头发上出现了掉落,头发变细等情况下是需要去看看是不是皮下的毛囊出了问题,要解决这个头发脱发上,选择植发的方式能有效改善,不过也不要小看这个头发脱发,脱发后如果不加以做好毛发护理, ...