Spring Data fournit une prise en charge de l'audit: c'est-à-dire lorsque l'entité est créée ou modifiée. Spring Data fournit l'ajout de @CreatedBy, @lastModifiedBy, @createdDate, @lastmodifiedDate Annotations aux propriétés de la classe d'entité et configurez les éléments de configuration correspondants pour réaliser la fonction d'audit. Le système enregistre automatiquement les valeurs des quatre propriétés de CreatedBy CreatedDate LastModifiedBy LastModifiedDate. Voici les éléments de configuration spécifiques.
Exemple
Créer une classe d'entité
package com.hfcsbc.infrastructureservice.domain; import com.hfcsbc.repository.support.domain.abstractauditingentity; importage Lombok.data; import org.springframework.data.annotation.createdBy; import org.springframework.data.annotation.createdDate; org.springframework.data.annotation.lastModifiedBy; import org.springframework.data.annotation.lastmodifiedDate; import org.springFramework.data.jpa.domain.support.auditingEntityListener; import javax.persistence. Pengchao le 2018/3/7 * / @ entité @ data @ entityListeners ({AuditEntityListener.class}) Public classe Personne {@id @generatedvalue private long id; nom de chaîne privé; Âge entier privé; @CreatedBy @Column (name = "Created_By", Nullable = false, Longueur = 50, Update = FALSE) String privé CreatedBy; @CreatedDate @Column (name = "Created_Date", nullable = false, update = false) Private Date CreatedDate = new Date (); @LastModifiedBy @Column (name = "last_modified_by", longueur = 50) String privé LastModifiedBy; @LastmodifiedDate @Column (name = "last_modified_date") Date privée lastmodifiedDate = new Date ();}Créer le référentiel correspondant
package com.hfcsbc.Repository; import com.hfcsbc.domain.person; import org.springframework.data.jpa.repository.jParepository; / ** * Création par Pengchao le 2018/3/7 * / interface publique PersonPository étend JParepository <Personn
Configurer des haricots pour obtenir des informations utilisateur
package com.hfcsbc.infrastructureservice.config; import org.springframework.data.domain.auditoraware; import org.springframework.security.core.contentication org.springframework.sterreotype.component; import java.util.optional; / ** * Créer par Pengchao le 2018/3/7 * / @ composant ("AuditorAware") Public Class AuditorAwareImpl SecurityContexTholder.getContext (). GetAuthentication (); return facultatif.of (authentication.getPrincipal (). toString ()); }}Allumez la fonction d'audit dans la classe d'entrée de démarrage Spring
package com.hfcsbc.infrastructureService; import org.springframework.boot.springApplication; import org.springframework.boot.autoconfigure.springbootapplication; org.springframework.scheduling.annotation.enableasync; @ SpringbootApplication @ activerjpaauditing (AuditorAwareref = "AuditorAware") @ perteasyncpublic PersonApplication {public static void main (String [] args); }}C'est-à-dire que la configuration est terminée. Lors de l'enregistrement d'objets à l'aide du référentiel, CreatedBy CreatedDate LastModifiedBy LastModifiedDate a une fonction d'audit pour l'insérer automatiquement.
Remarque: comment obtenir des informations utilisateur dans la méthode asynchrone
Étant donné que vous utilisez le référentiel pour enregistrer des objets dans une méthode asynchrone, vous ne pouvez pas obtenir d'informations utilisateur utilisateur. Vous devez ajouter les éléments de configuration suivants pour obtenir des informations utilisateur dans l'authentification
package com.hfcsbc.config; import org.springframework.beans.factory.config.methodinvokingfactorybean; import org.springframework.context.annotation.bean; import org.springframework.context.annotation.configuration; import org.springframework.security.core.context.securityContextholder; / ** * Créer par Pengchao le 2018/3/7 * / @ ConfigurationPublic Class AuditorAwareConfig {@Bean Public MethodinVokingFactoryBean méthodinvokingfactoryBean = Newdine MethodInvokingFactoryBean (); MethodInvokingFactoryBean.setTargetClass (SecurityContexTholder.class); MethodInvokingFactoryBean.setTargetMethod ("setStrateGyName"); MethodInVokingFactoryBean.SetArguments (new String [] {SecurityContexTholder.Mode_InheritableThreadLocal}); Retour MethodInvokingFactoryBean; }}La fonction principale de SecurityContexTholder est d'associer le processus actuellement exécuté avec le SecurityContex.
SecurityContexTholder.Mode_InheritableThreadLocal: utilisé dans le cas où les threads ont des relations parent-enfant, le thread enfant intègre le SecurityContexTholder du thread parent;
SecurityContexTholder.Mode_InheritableThreadLocal: partagez globalement SecurityContexTholder.
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.