要在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. JAVA中文件与Byte数组相互转换的方法

    JAVA中文件与Byte数组相互转换的方法,如下: public class FileUtil { //将文件转换成Byte数组 public static byte[] getBytesByFile ...

  2. Request.getparameternames 获取form表单里面所有的请求参数 。 返回一个Enumeration类型的枚举.

    通过Enumeration的hasMoreElements()方法遍历.再由nextElement()方法获得枚举的值.此时的值是form表单中所有控件的name属性的值. 最后通过request.g ...

  3. 号外号外!解决github+hexo+yilia评论插件的问题!!!

    先走一波效果图!    本人网站--http://www.wenzheng.club/ ps:效果还是不错的,支持QQ微信登录,支持表情,甚至gif动图评论! 插件采用韩国服务器的来必力评论插件--h ...

  4. 豹哥嵌入式讲堂:ARM知识概要杂辑(1)- 内核架构编年史

    众所周知,ARM公司是一家微处理器行业的知名企业,ARM公司本身并不靠自有的设计来制造或出售CPU,而是将处理器架构授权给有兴趣的厂家.这些厂家基本涵盖了全球领先的知名半导体企业.软件和OEM厂商:T ...

  5. 浅谈ES6

    ECMAScript6.0(简称ES6)是javaScript语言的下一代标准,已经在2015年6月正式发布了.它的目标,使得javaScript语言可以用来编写复杂的大型应用程序,成为企业级开发语言 ...

  6. 关于word图片显示不全

    问题:在编辑word时,在其中一行插入一张图片,但是显示不全. 原因:给文字行距设置成 [ 固定值 ]的原因. 解决方案:先删除图片,在插入图片的一行右键--> 段落,弹出对话框,找到设置行距的 ...

  7. selenium的使用技巧及集成到scrapy

    为了爬取拉钩,今天学习了selenum的使用技巧.   from scrapy.http import HtmlResponse   class JSPageMiddleware(object):   ...

  8. iOS-Runtime之关于页面跳转的捷径【Runtime获取当前ViewController】

    写在前面 在我们操作页面跳转时,如果当前的类不是UIViewcontroller(下面用VC表示),你会不会写一个代理,或者block给VC传递信息,然后在VC里面进行 ///假如targetVc是将 ...

  9. 原生JS实现图片轮播

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  10. Python基础篇(六)

    retun空值,后面的语句将不再被执行 >>> def test(): ...    print("just a test!") ...    return .. ...