使用spring-data-mongodb进行审计

时间:2017-01-08 06:52:47

标签: java spring mongodb spring-boot spring-data-mongodb

我正在尝试使用Spring数据mongodb启用自动审核字段,如here所述。下面是我的配置类

@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());
    }
}

但是当我添加@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)

2 个答案:

答案 0 :(得分:10)

1:确保您拥有spring-data-mongodb

2:如果您使用的是@CreatedDate@LastModifiedDate,那么您就不需要任何其他配置。

class ClassName {

    .......

  @CreatedDate
  private DateTime createdDate;

  @LastModifiedDate
  private DateTime @lastModifiedDate;

}

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";
    }

}

从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();
  }
}

答案 1 :(得分:3)

根据更改日志,您是否可以检查Spring Data MongoDB依赖1.9.4.RELEASE或更高版本mongoAuditingHandler是否需要MappingMongoConverter版本1.9.4.RELEASE或更高版本 - spring-data-mongodb-changelog,例如:

<dependency>
    <groupId>org.springframework.data</groupId>
    <artifactId>spring-data-mongodb</artifactId>
    <version>1.9.4.RELEASE</version>
</dependency>