【问题标题】:Spring AuditorAware set createdBy manuallySpring AuditorAware set createdBy 手动
【发布时间】:2018-02-14 19:09:37
【问题描述】:

在我的存储库中,我有字段,这些字段用@CreatedBy@CreationTimestamp 进行了注释。我正在使用 spring-security 进行身份验证。一切正常,数据库中的字段填充了正确的 usernamecreationTimestamp。但有时我需要自己设置 created 列,我需要覆盖 @CreatedBy 注释。如何实现?

【问题讨论】:

    标签: spring spring-boot spring-security spring-data spring-annotations


    【解决方案1】:

    用户ID可以通过以下方式手动设置:

    @PrePersist
    protected void onCreate() {
        //retrieve userid here and set it.
        String userUid =  SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        setCreatedBy(userUid);
    }
    
    @PreUpdate
    protected void onUpdate() {
        //retrieve userid here and set it.
        String userUid =  SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        setUpdatedBy(userUid);
    }
    

    【讨论】:

    • 这个问题是,userId 每次都不一样。 @PrePersist 不接受任何参数。如何将参数传递给它?
    猜你喜欢
    • 2021-02-03
    • 1970-01-01
    • 2012-12-22
    • 2017-03-13
    • 1970-01-01
    • 2015-06-10
    • 2017-07-08
    • 2018-06-11
    • 2017-11-29
    相关资源
    最近更新 更多