<output id="qn6qe"></output>

    1. <output id="qn6qe"><tt id="qn6qe"></tt></output>
    2. <strike id="qn6qe"></strike>

      亚洲 日本 欧洲 欧美 视频,日韩中文字幕有码av,一本一道av中文字幕无码,国产线播放免费人成视频播放,人妻少妇偷人无码视频,日夜啪啪一区二区三区,国产尤物精品自在拍视频首页,久热这里只有精品12

      springboot~為ES實體封裝審計Auditing功能

      審記功能在Jpa框架里出現(xiàn)的,主要針對實體的幾個字段進行自動化的賦值,讓業(yè)務人員可以把關(guān)注點放在業(yè)務上,對于公用的,有規(guī)則的字段,由系統(tǒng)幫我們?nèi)ヌ幚怼?/p>

      原理

      通過spring aop功能實現(xiàn)對es倉庫接口方法的攔截,然后在方法處理之前,為實體的這些公用字段賦值即可,我們使用了jpa里的一些注解,如@CreateBy,@CreateDate,@LatestModifyDate等等。

      EsBaseEntity實體

      
      @Data
      public class EsBaseEntity {
      
          public static final String dateTimeFormat = "yyyy/MM/dd||yyyy-MM-dd" +
                  "||yyyy-MM-dd HH:mm:ss||yyyy/MM/dd HH:mm:ss" +
                  "||yyyy-MM-dd HH:mm:ss.SSS||yyyy/MM/dd HH:mm:ss.SSS" +
                  "||yyyy-MM-dd'T'HH:mm:ss.SSS";
          /**
           * 創(chuàng)建時間.
           */
          @Field(type = FieldType.Date, format = DateFormat.custom, pattern = dateTimeFormat)
          @CreatedDate
          protected String createTime;
          /**
           * 創(chuàng)建人.
           */
          @Field(type = FieldType.Keyword)
          @CreatedBy
          protected String creator;
          /**
           * 更新時間.
           */
          @Field(type = FieldType.Date, format = DateFormat.custom, pattern = dateTimeFormat)
          @LastModifiedDate
          protected String updateTime;
          /**
           * 更新人.
           */
          @Field(type = FieldType.Keyword)
          @LastModifiedBy
          protected String updateUser;
          /**
           * 刪除標記.
           */
          @Field(type = FieldType.Keyword)
          protected boolean delFlag;
          /**
           * 主鍵.
           */
          @Id
          private String id = String.valueOf(SnowFlakeUtil.getFlowIdInstance().nextId());
      
      }
      
      

      審記攔截器

      @Component
      @Aspect
      public class AuditAspect {
          DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
      
          /**
           * 添加ES實體-切入點
           */
          @Pointcut("execution(* org.springframework.data.repository.CrudRepository.save(..))")
          public void save() {
          }
      
          /**
           * 更新ES實體-切入點
           */
          @Pointcut("execution(* org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate.update(..))")
          public void update() {
          }
      
          /**
           * 插入實體攔截器.
           *
           * @param joinPoint
           * @throws IllegalAccessException
           */
          @Before("save()")
          public void beforeSave(JoinPoint joinPoint) throws IllegalAccessException {
              System.out.println("插入攔截");
      
              if (joinPoint.getArgs().length > 0) {
                  Object esBaseEntity = joinPoint.getArgs()[0];
                  Field[] fields = ClassHelper.getAllFields(esBaseEntity.getClass());
                  List<Field> fieldList = Arrays.stream(fields)
                          .filter(o -> o.getAnnotation(CreatedDate.class) != null
                                  || o.getAnnotation(LastModifiedDate.class) != null)
                          .collect(Collectors.toList());
                  if (!CollectionUtils.isEmpty(fieldList)) {
                      for (Field field : fieldList) {
                          field.setAccessible(true);//取消私有字段限制
                          if (field.get(esBaseEntity) == null) {
                              field.set(esBaseEntity, df.format(new Date()));
                          }
                      }
                  }
                  List<Field> auditFieldList = Arrays.stream(fields)
                          .filter(o -> o.getAnnotation(CreatedBy.class) != null
                                  || o.getAnnotation(LastModifiedBy.class) != null)
                          .collect(Collectors.toList());
                  if (!CollectionUtils.isEmpty(auditFieldList)) {
                      for (Field field : auditFieldList) {
                          field.setAccessible(true);//取消私有字段限制
                          if (field.get(esBaseEntity) == null) {
                              EsAuditorAware esAuditorAware = SpringContextConfig.getBean(EsAuditorAware.class);
                              if (esAuditorAware != null) {
                                  field.set(esBaseEntity, esAuditorAware.getCurrentAuditor().orElse(null));
                              }
                          }
                      }
                  }
              }
      
          }
      
          /**
           * 更新實體攔截器.
           *
           * @param joinPoint
           */
          @Before("update()")
          public void beforeUpdate(JoinPoint joinPoint) {
              System.out.println("更新攔截");
              if (joinPoint.getArgs().length == 1 && joinPoint.getArgs()[0] instanceof UpdateQuery) {
                  UpdateQuery updateQuery = (UpdateQuery) joinPoint.getArgs()[0];
                  Map source = updateQuery.getUpdateRequest().doc().sourceAsMap();
                  Field[] fields = ClassHelper.getAllFields(updateQuery.getClazz());
                  List<Field> fieldList = Arrays.stream(fields)
                          .filter(o -> o.getAnnotation(LastModifiedDate.class) != null)
                          .collect(Collectors.toList());
                  for (Field field : fieldList) {
                      if (!source.containsKey(field.getName())) {
                          source.put(field.getName(), df.format(new Date()));
                      }
                  }
                  List<Field> auditFieldList = Arrays.stream(fields)
                          .filter(o -> o.getAnnotation(LastModifiedBy.class) != null)
                          .collect(Collectors.toList());
                  for (Field field : auditFieldList) {
                      if (!source.containsKey(field.getName())) {
                          EsAuditorAware esAuditorAware = SpringContextConfig.getBean(EsAuditorAware.class);
                          if (esAuditorAware != null) {
                              source.put(field.getName(), esAuditorAware.getCurrentAuditor().orElse(null));
                          }
                      }
                  }
                  updateQuery.getUpdateRequest().doc().source(source);
              }
          }
      }
      
      

      對審記人使用Aware方式實現(xiàn)

      /**
       * Es獲取審核當前對象.
       *
       * @param <T>
       */
      public interface EsAuditorAware<T> {
          Optional<T> getCurrentAuditor();
      }
      /**
       * 得到當前操作人信息.
       */
      @Component
      public class UserAuditorAware implements EsAuditorAware<String> {
          @Override
          public Optional<String> getCurrentAuditor() {
              return Optional.of("1");
          }
      }
      
      

      第三方組件開啟審計功能

      /**
       * 開啟ES審計功能.
       * 主要對建立人、建立時間、更新人、更新時間賦值.
       */
      @Target({ElementType.TYPE})
      @Retention(RetentionPolicy.RUNTIME)
      @Documented
      @Import({AuditAspect.class, SpringContextConfig.class})
      public @interface EnableEsAuditing {
      }
      
      
      posted @ 2020-08-05 21:36  張占嶺  閱讀(904)  評論(0)    收藏  舉報
      主站蜘蛛池模板: 精品无码一区在线观看| 97久久精品人人做人人爽| 婷婷综合缴情亚洲| 国产a在视频线精品视频下载| 成人精品一区二区三区在线观看| 一区二区三区四区高清自拍| 日本熟妇色xxxxx| 国产av国片精品一区二区| 亚洲精品国产电影| 人妻伦理在线一二三区| 久久人妻av无码中文专区| 久久老熟女一区二区蜜臀| 亚洲香蕉网久久综合影视| 亚洲国产五月综合网| 九九热在线精品视频99| 国产又色又爽又黄的视频在线| 91久久国产成人免费观看| 92久久精品一区二区| 亚洲a∨国产av综合av| 亚洲中文字幕无码av永久| 中国产无码一区二区三区| 人妻精品中文字幕av| 亚洲人成亚洲人成在线观看| 免费看的日韩精品黄色片| 国产精品不卡一二三区| 亚洲 日本 欧洲 欧美 视频| 亚洲人黑人一区二区三区| 大尺度国产一区二区视频| 韩国精品一区二区三区在线观看| 汶川县| 日韩国产成人精品视频| 中文字幕日韩国产精品| 精品久久久久中文字幕APP| 麻豆天美国产一区在线播放| 日韩人妻无码一区二区三区综合部| 亚洲自拍偷拍中文字幕色| 精品国产中文字幕av| 强奷漂亮少妇高潮伦理| 精品人妻午夜一区二区三区四区| 免费视频欧美无人区码| 激情动态图亚洲区域激情|