spring - 如何使用 Jhipster 生成的代码为注销添加审核?

标签 spring jhipster audit-logging

我调试了如何将审计添加到系统中以成功登录并发现 CustomAuditEventRepository.auditEventRepository().add(AuditEvent 事件) 正在使用 调用奥普 .是否有任何文档如何为任何自定义操作添加审核?

最佳答案

我能够使用以下代码实现上述目标。

  • 创建了可以发布审计事件的类。

  • import org.springframework.boot.actuate.audit.AuditEvent;
    import org.springframework.boot.actuate.audit.listener.AuditApplicationEvent;
    import org.springframework.context.ApplicationEventPublisher;
    import org.springframework.context.ApplicationEventPublisherAware;
    import org.springframework.context.annotation.Configuration;
    
    @Configuration
    public class AuditEventPublisher implements ApplicationEventPublisherAware  {
        private ApplicationEventPublisher publisher;
    
        @Override
        public void setApplicationEventPublisher(
                ApplicationEventPublisher publisher) {
            this.publisher = publisher;
        }
    
        public void publish(AuditEvent event) {
            if (this.publisher != null)
                this.publisher.publishEvent(new AuditApplicationEvent(event));
        }
    }
    
  • 在需要的地方注入(inject) AuditEventPublisher 并调用带有审计事件的发布以插入到数据库

  • @RestController
    @RequestMapping("/api")
    public class UserXAuthTokenController {
    
        @Inject
        private AuditEventPublisher auditPublisher;
    
    .....
    .....
    
        @RequestMapping(value = "/logout",
                method = RequestMethod.POST)
        @Timed
        public void logout(@RequestParam String authToken) {
            String principal = tokenProvider.getUserNameFromToken(authToken);
            AuditEvent event = new AuditEvent(principal, "LOGOUT_START", new HashMap<String, Object>());
            auditPublisher.publish(event);
            SecurityContextHolder.clearContext();
        }
    
    }
    

    关于spring - 如何使用 Jhipster 生成的代码为注销添加审核?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28853218/

    相关文章:

    spring-boot - Spring Data Elastic 从 3.x 迁移到 4.x 的索引问题

    elasticsearch - 在ElasticSearch中建立索引以进行审核

    javascript - 如何在 Web 应用程序中处理用户审核日志(或其他频繁的客户端操作)?

    java - 使用 Spring 从测试/资源加载资源/文件

    java - 这相当于在 XML 配置文件中使用 @Autowire 注释 Autowiring 接口(interface)?

    java - 在 maven 中测试时 Spring-Data h2 : java. io.FileNotFoundException

    java - Spring 3.2 和 jackson 2 : add custom object mapper

    java - 在Jhipster中使用JDL时出错

    c# - 在 ASP.NET 样板中为应用程序服务配置审计日志记录

    java - JHipster JDL 未生成任何内容,因为必须传递数据库类型