使用spring-data-mongodb进行审计

roh*_*hit 6 java spring mongodb spring-data-mongodb spring-boot

我试图使弹簧数据MongoDB的自动审计领域的解释在这里.下面是我的配置类

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.abc")
@EnableMongoRepositories(basePackages = "com.abc.xyz.repository")
@EnableMongoAuditing
public class ApplicationConfiguration {

    @Bean
    public MongoDbFactory mongoDbFactory() throws Exception {
        ServerAddress serverAddress = new ServerAddress("127.0.0.1", 27017);
        MongoCredential mongoCredential = MongoCredential.createCredential("user", "test", "abc123".toCharArray());
        MongoClient mongoClient = new MongoClient(serverAddress, Arrays.asList(mongoCredential));
        return new SimpleMongoDbFactory(mongoClient, "test");
    }

    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
        return new MongoTemplate(mongoDbFactory());
    }
}
Run Code Online (Sandbox Code Playgroud)

但是当我添加@EnableMongoAuditing时,我在启动服务器时遇到以下错误.

Caused by: java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'mongoAuditingHandler': Cannot create inner bean '(inner bean)#6dca0c34' of type [org.springframework.data.mongodb.config.MongoAuditingRegistrar$MongoMappingContextLookup] while setting constructor argument; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name '(inner bean)#6dca0c34': Unsatisfied dependency expressed through constructor parameter 0: No qualifying bean of type [org.springframework.data.mongodb.core.convert.MappingMongoConverter] found for dependency [org.springframework.data.mongodb.core.convert.MappingMongoConverter]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.data.mongodb.core.convert.MappingMongoConverter] found for dependency [org.springframework.data.mongodb.core.convert.MappingMongoConverter]: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {}
    at io.undertow.servlet.core.DeploymentManagerImpl.deploy(DeploymentManagerImpl.java:236)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService.startContext(UndertowDeploymentService.java:100)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentService$1.run(UndertowDeploymentService.java:82)
Run Code Online (Sandbox Code Playgroud)

Cha*_*ndu 11

1:确保你有 spring-data-mongodb

2:如果您正在使用@CreatedDate或者@LastModifiedDate您不需要任何其他配置.

class ClassName {

    .......

  @CreatedDate
  private DateTime createdDate;

  @LastModifiedDate
  private DateTime @lastModifiedDate;

}
Run Code Online (Sandbox Code Playgroud)

3:如果您正在使用@CreatedBy,@LastModifiedBy那么您必须实现AuditorAware<T>SPI接口

class ClassName {

    .......

    @CreatedBy
    private String createdBy;

    @LastModifiedBy
    private String lastModifiedBy;

}

public class AppAuditor implements AuditorAware<String> {

    @Override
    public String getCurrentAuditor() {

        // get your user name here
        return "xxxx";
    }

}
Run Code Online (Sandbox Code Playgroud)

从Spring doc实现基于Spring Security的AuditorAware

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();
  }
}
Run Code Online (Sandbox Code Playgroud)


Arp*_*wal 4

您可以检查您是否具有所需的Spring Data MongoDB依赖项1.9.4.RELEASE或更高版本MappingMongoConverter根据变更日志 - spring-data-mongodb-changelog在版本或更高版本中可用,例如:mongoAuditingHandler1.9.4.RELEASE

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>1.9.4.RELEASE</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)