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: & ...
随机推荐
- android adapter 中添加OnClickListener事件
public class SearchAutoAdapter extends BaseAdapter { private OnClickListener mOnClickListener; publi ...
- tomcat监控(二)
标签: linux 笔者Q:972581034 交流群:605799367.有任何疑问可与笔者或加群交流 这里介绍二种监控Tomcat的方法 使用windows版本的jdk监控 使用zabbix监控 ...
- python selenium 鼠标悬停
#鼠标悬停 chain = ActionChains(driver) implement = driver.find_element_by_link_text() chain.move_to_elem ...
- LNMP之Nginx
Nginx初探 概念: Nginx是一款免费.开源.高性能的HTTP服务器和反向代理,同时也可作为邮件代理服务器.其因为高性能.稳定.丰富的功能集.配置简单和低系统资源消耗而闻名. Tengine是由 ...
- OpenStreetMap数据清洗(SQL&MonogoDB版本)
目标:通过网上下载的OpenStreetMap.xml数据格式,将该文件的格式进行统计,清洗,并导出成CSV格式的文件,最后倒入到SQLite中 本案例中所需的包 import csv import ...
- inotify-tools使用方法详解
inotify-tools 是为linux下inotify文件监控工具提供的一套c的开发接口库函数,同时还提供了一系列的命令行工具,这些工具可以用来监控文件系统的事件. inotify-tools是用 ...
- rsync源目录写法的一点小细节
原始状态: [root@localhost tmp]# tree . ├── a │ ├── a1 │ └── a2 └── b directories, files [root@localhost ...
- AWWWB.COM网站克隆器
AWWWB.COM建议收费软件实行免费化的倡议书:AWWWB.COM原打算对软件中的高级功能收取260元的注册费,但是,考虑到网友使用软件的同时也是对软件的支持和传播,所以,放弃收费计划,实施一种免费 ...
- iOS-Mac Charles抓包工具的使用【Mac 抓包工具Charles】
1.下载文件 Charles安装包以及破解文件下载地址:http://charles.iiilab.com 2.安装及使用 使用介绍 http://www.cocoachina.com/ios/201 ...
- MySQL完全备份、增量备份与恢复[转]
原文链接:http://www.360doc.com/content/11/1209/09/834950_170836197.shtml 场景:每周日执行一次完全备份,每天下午1点执行增量备份 [ 适 ...