如何在 Spring Boot 应用程序中启用和配置审核

问题描述 投票:0回答:1

我有这个主课:

@SpringBootApplication
@EnableScheduling
@ConfigurationPropertiesScan
@EnableJpaAuditing(auditorAwareRef = "auditorAwareImpl")
public class PlanetsApplication {


    public static void main(String[] args) {

        SpringApplication.run(PlanetsApplication.class, args);

    }
}

@Component("auditorAwareImpl")
public class AuditorAwareImpl implements AuditorAware<String> {

    @NotNull
    @Override
    public Optional<String> getCurrentAuditor() {
        return Optional.of("system");
    }

}

@Getter
@Setter
@EntityListeners(AuditingEntityListener.class)
@MappedSuperclass
public class BaseEntity {


    @CreatedDate
    @Column(updatable = false)
    protected LocalDateTime createdAt;

    @CreatedBy
    @Column(updatable = false)
    private String createdBy;

    @LastModifiedDate
    @Column(updatable = false)
    protected LocalDateTime updatedAt;

    @LastModifiedBy
    @Column(updatable = false)
    protected String updatedBy;


}

@Entity
@Table(name = "t_spotify_playlist", uniqueConstraints =
@UniqueConstraint(columnNames = {"playlistId", "sun", "moon"}))
@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@Builder
public class SpotifyPlayList extends BaseEntity implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    private String playlistId;
    @Column(length = 50)
    private String sun;
    @Column(length = 50)
    private String moon;


    // Many-to-Many relationship with SpotifyTrack
    @ManyToMany(fetch = FetchType.EAGER)
    @JoinTable(
            name = "t_playlist_track", // Join table name
            joinColumns = @JoinColumn(name = "playlist_id"), // Foreign key for SpotifyPlayListDesc
            inverseJoinColumns = @JoinColumn(name = "track_id") // Foreign key for SpotifyTrack
    )
    private Set<SpotifyTrack> tracks; // Tracks associated with the playlist
    

}

我保存实体:

SpotifyPlayList spotifyPlayList2 = spotifyPlayListService.findAll().stream().findAny().get();
        spotifyPlayList2.setSun(spotifyPlayList2.getSun().toUpperCase());
        spotifyPlayListService.save(spotifyPlayList2);

        log.info("saved {} ", spotifyPlayList2);

但是数据库中没有审计任何内容

java spring spring-boot audit audit-logging
1个回答
0
投票

可能缺少 AuditorAware Bean

@Configuration
@EnableJpaAuditing(auditorAwareRef = "auditorAwareImpl")
public class AuditConfiguration {    
    @Bean
    public AuditorAware<String> auditorProvider() {
        return new AuditorAwareImpl();
    }
}
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.