• 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")
  • 相关阅读:
    锁详解
    消息组件
    分布式锁
    jvm调优
    类加载
    垃圾回收
    Mysql
    redis
    悲观锁和乐光锁
    算法常见
  • 原文地址:https://www.cnblogs.com/sweetchildomine/p/7729319.html
Copyright © 2020-2023  润新知