ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

SpringDataJpa使用审计(Auditing)功能

2019-08-18 22:03:30  阅读:500  来源: 互联网

标签:审计 ApplicationContext SpringDataJpa return EnableJpaAuditing AuditorAware bean A


SpringBoot项目使用SpringDataJpa提供的审计功能的使用流程

SpringDataJpa提供审计注解:@CreatedBy,@LastModifiedBy,@CreatedDate,@LastModifiedDate

第一步:在SpringBoot启动类上添加@EnableJpaAuditing

 

public @interface EnableJpaAuditing {

    /**
     * Configures the {@link AuditorAware} bean to be used to lookup the current principal.
     *
     * @return
     */
        // 当SpringIOC容器中注册了多个审计的Bean,需要指定Bean的名称
    String auditorAwareRef() default "";

    /**
     * Configures whether the creation and modification dates are set. Defaults to {@literal true}.
     *
     * @return
     */
    boolean setDates() default true;

    /**
     * Configures whether the entity shall be marked as modified on creation. Defaults to {@literal true}.
     *
     * @return
     */
    boolean modifyOnCreate() default true;

    /**
     * Configures a {@link DateTimeProvider} bean name that allows customizing the {@link org.joda.time.DateTime} to be
     * used for setting creation and modification dates.
     *
     * @return
     */
    String dateTimeProviderRef() default "";
}

原文:

If you expose a bean of type AuditorAware to the ApplicationContext, the auditing infrastructure automatically picks it up and uses it to determine the current user to be set on domain types. If you have multiple implementations registered in the ApplicationContext, you can select the one to be used by explicitly setting the auditorAwareRef attribute of @EnableJpaAuditing.

翻译:

如果将AuditorAware类型的bean公开给ApplicationContext,则审计基础结构会自动选择它并使用它来确定要在域类型上设置的当前用户。 如果在ApplicationContext中注册了多个实现,则可以通过显式设置@EnableJpaAuditing的auditorAwareRef属性来选择要使用的实现。

第二步:在实体类上进行添加注解的标注字段

class Customer {

  @CreatedBy
  private User user;

  @CreatedDate
  private DateTime createdDate;

  // … further properties omitted
}
View Code

第三步:实现 AuditorAware 接口,泛型T是返回的限定类型,并注册到Spring管理的容器中

class SpringSecurityAuditorAware implements AuditorAware<User> {

  public Optional<User> getCurrentAuditor() {

    return Optional.ofNullable(SecurityContextHolder.getContext())
              .map(SecurityContext::getAuthentication)
              .filter(Authentication::isAuthenticated)
              .map(Authentication::getPrincipal)
              .map(User.class::cast);
  }
}
View Code

第四步:在实体类上添加 @EntityListeners(AuditingEntityListener.class) 注解

之后,当进行save操作的时候,会自动设置获取通过审计注解获取的相关信息

 

 

 

参考官方文献:https://docs.spring.io/spring-data/jpa/docs/2.1.9.RELEASE/reference/html/#auditing

标签:审计,ApplicationContext,SpringDataJpa,return,EnableJpaAuditing,AuditorAware,bean,A
来源: https://www.cnblogs.com/XingXiaoMeng/p/11374146.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有