如何在 Spring Data(和 Spring Data Rest)中通过 Java Config 配置审计?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29880911/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:04:39  来源:igfitidea点击:

How to configure Auditing via Java Config in Spring Data (and Spring Data Rest)?

javaspring-dataspring-data-jpaspring-data-rest

提问by Jay

I am trying to use Spring Data's auditing capabilities (in combination with Spring Boot and Spring Data Rest), but the audit fields are not being set on save. All saves result in a constraint exception from trying to save a null "Created By."

我正在尝试使用 Spring Data 的审计功能(与 Spring Boot 和 Spring Data Rest 结合使用),但未在保存时设置审计字段。所有保存都会导致尝试保存空“创建者”的约束异常。

According to the spring data docs, I should just be able to place the appropriate auditing annotations (@CreatedDate/etc) on my Entity, and make an AuditorAware<> available to the application context. I know my auditor aware bean is being created from setting a breakpoint in the debugger.

根据spring 数据文档,我应该能够在我的实体上放置适当的审计注释(@CreatedDate/etc),并使 AuditorAware<> 可用于应用程序上下文。我知道我的审计员​​感知 bean 是通过在调试器中设置断点来创建的。

My questions are:

我的问题是:

1) Is it necessary for me to create an AuditingEntityListener, or should I expect one to be provided from having @EnableJpaAuditing? (it's not clear in the docs about java config)

1) 我是否有必要创建一个 AuditingEntityListener,或者我应该期望通过 @EnableJpaAuditing 提供一个?(关于java config的文档中不清楚)

2) Is there other configuration in the below code that I'm missing to set up automatic auditing?

2)在下面的代码中是否还有其他配置我没有设置自动审计?

3) I'm calling the creation code from a POST to Spring Data Rest, are there any special caveats with using this auditing functionality in combination with Spring Data Rest?

3) 我正在将创建代码从 POST 调用到 Spring Data Rest,将此审计功能与 Spring Data Rest 结合使用有什么特别的注意事项吗?

@Entity
public class Tag implements Serializable {

    // ... other fields omitted...

    @CreatedDate
    @Temporal(TemporalType.TIMESTAMP)
    private Date created = new Date();

    @CreatedBy
    @Basic(optional = false)
    @Column(name = "CREATED_BY", nullable = false, length = 24)
    private String createdBy = "";

    @LastModifiedDate
    @Basic(optional = false)
    @Column(nullable = false)
    @Temporal(TemporalType.TIMESTAMP)
    private Date updated = new Date();

    @LastModifiedBy
    @Basic(optional = false)
    @Column(name = "UPDATED_BY", nullable = false, length = 24)
    private String updatedBy = "";

    // ... getters and setters were generated ...

And the configuration:

和配置:

@EnableJpaAuditing
@Configuration
public class AuditingConfig {

    @Bean
    public AuditorAware<String> createAuditorProvider() {
        return new SecurityAuditor();
    }

    @Bean
    public AuditingEntityListener createAuditingListener() {
        return new AuditingEntityListener();
    }

    public static class SecurityAuditor implements AuditorAware<String> {
        @Override
        public String getCurrentAuditor() {
            Authentication auth = SecurityContextHolder.getContext().getAuthentication();
            String username = auth.getName();
            return username;
        }
    }

}

Any help is much appreciated, thanks!

非常感谢任何帮助,谢谢!

回答by Nitin Arora

1) Is it necessary for me to create an AuditingEntityListener, or should I expect one to be provided from having @EnableJpaAuditing? (it's not clear in the docs about java config)

1) 我是否有必要创建一个 AuditingEntityListener,或者我应该期望通过 @EnableJpaAuditing 提供一个?(关于java config的文档中不清楚)

Answer:No you do not need to define AuditingEntityListenerbean. Instead you need to specify @EntityListeners(AuditingEntityListener.class)on your domain class.

答:不,您不需要定义AuditingEntityListenerbean。相反,您需要@EntityListeners(AuditingEntityListener.class)在域类上指定。

e.g.

例如

@Entity
@EntityListeners(AuditingEntityListener.class)
public class Tag implements Serializable {

}

2) Is there other configuration in the below code that I'm missing to set up automatic auditing?

2)在下面的代码中是否还有其他配置我没有设置自动审计?

Answer:Other configuration settings look fine.

答:其他配置设置看起来不错。

3) I'm calling the creation code from a POST to Spring Data Rest, are there any special caveats with using this auditing functionality in combination with Spring Data Rest?

3) 我正在将创建代码从 POST 调用到 Spring Data Rest,将此审计功能与 Spring Data Rest 结合使用有什么特别的注意事项吗?

Answer:I think not. Try with above suggested change. It should just work.

回答:我认为不会。尝试使用上述建议的更改。它应该可以正常工作。