Spring data mongodb @CreatedBy@LastModifiedBy@CreatedBy@LastModifiedBy SpringSecurityAuditorAware,只记录用户名
要在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,只记录用户名的更多相关文章
- spring data mongodb 配置遇到的几个问题
一. mongodb 2.2版本以上的配置 spring.data.mongodb.uri = mongodb://newlook:newlook@192.168.0.109:27017/admin ...
- spring data mongodb中,如果对象中的属性不想加入到数据库字段中
spring data mongodb中,如果对象中的属性不想加入到数据库字段中,可加@Transient注解,声明为透明属性 spring data mongodb 官网帮助文档 http://ww ...
- 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 ...
- 使用Spring访问Mongodb的方法大全——Spring Data MongoDB查询指南
1.概述 Spring Data MongoDB 是Spring框架访问mongodb的神器,借助它可以非常方便的读写mongo库.本文介绍使用Spring Data MongoDB来访问mongod ...
- Spring data mongodb 聚合,投射,内嵌数组文档分页.
尽量别直接用 DBObject ,Spring data mongodb 的api 本来就没什么多大用处,如果还直接用 DBObject 那么还需要自己去解析结果,说动做个对象映射,累不累 Spri ...
- JAVA 处理 Spring data mongodb 时区问题
Spring data mongodb 查询出结果的时候会自动 + 8小时,所以我们看起来结果是对的 但是我们查询的时候,并不会自动 + 8小时,需要自己处理 解决方法 1 @JsonFormat ...
- Spring data mongodb ObjectId ,根据id日期条件查询,省略@CreatedDate注解
先看看ObjectId 的json 结构,非常丰富,这里有唯一机器码,日期,时间戳等等,所以强烈建议ID 使用 ObjectId 类型,并且自带索引 Spring data mongodb 注解 @C ...
- Spring Data MongoDB 三:基本文档查询(Query、BasicQuery)(一)
一.简单介绍 Spring Data MongoDB提供了org.springframework.data.mongodb.core.MongoTemplate对MongoDB的CRUD的操作,上一 ...
- Introduction to Spring Data MongoDB
Introduction to Spring Data MongoDB I just announced the new Spring 5 modules in REST With Spring: & ...
随机推荐
- java事件处理机制
java中的事件机制的参与者有3种角色: 1.event object:就是事件产生时具体的"事件",用于listener的相应的方法之中,作为参数,一般存在与listerne ...
- windows下使用Git Bash命令行克隆远程仓库代码
此处使用的代码托管平台是GitLab,相比GitHub来说,它可以设置免费的私有仓库,哈哈,妈妈再也不用担心我的源码泄露了!1.切换到本地的工作目录,我的目录是: cd /d/coder/websit ...
- matlab获取文件夹中的所有文件名(dir)
当前目录中包含文件及目录如下: abc111.txt abc112.txt abc113.txt a\ (文件夹) CODE: >> dir('test') %目录 . ...
- *C语言有关指针的变量声明中的几个易错点
转至:http://my.oschina.net/ypimgt/blog/108265 Technorati 标签: 指针, typedef, const, define 我们都知道,至少听说过 ...
- 《.NET 设计规范》第 3 章 命名规范
<.NET 规范>第 3 章 命名规范 3.1 大小写约定 要把 PascalCasing 用于由多个单词构成的命名空间.类型以及成员的名字. 要把 camelCasing 用于参数的名字 ...
- JavaScript转unix时间戳
由于 unix 的时间戳是10位不带毫秒的,所以前端获取到时间戳之后需要做一下处理,才能获取正确的时间. // 假设这里是从服务端获取到的时间戳 var unixTime = data.time; / ...
- jQuery&Ajax应用
jQuery对Ajax操作进行了封装,在jQuery中$.ajax()方法属于最底层的方法,第二层是load(),$.get()和$.post()方法,第三层是$.getScript(),$.getJ ...
- Java中获取本地某一个目录下的所有文件和文件夹
在从事web开发工作中,经常需要对本地某一个目录下的文件进行处理,而在这之前,我们需要做的就是获取到这个目录下的文件. String filepath = "D:\file";// ...
- wpf timePicker 时间选择控件
wpf里有日期选择控件,但没有时间选择控件.其他地方也有类似的,但效果并不太好,而且复杂.所以就自己写了个.参考codeproject上的. 分两部分. 第一部分是.cs文件.也就是control控件 ...
- iOS-NSPredicate正则验证【三种验证方法】
1.NSPredicate验证(谓词匹配) ///验证(string:验证的字符串) + (BOOL)stringValidate:(NSString *)string{ NSString *regu ...