ข้อมูลสปริงให้การสนับสนุนสำหรับการตรวจสอบ: นั่นคือเมื่อเอนทิตีถูกสร้างหรือแก้ไข ข้อมูลฤดูใบไม้ผลิให้การเพิ่ม @CreatedBy, @LastModifiedBy, @CreatedDate, @LastModifiedDate คำอธิบายประกอบกับคุณสมบัติของคลาสเอนทิตีและกำหนดค่ารายการการกำหนดค่าที่สอดคล้องกันเพื่อรับฟังก์ชั่นการตรวจสอบ ระบบจะบันทึกค่าของคุณสมบัติทั้งสี่โดยอัตโนมัติของ CreatedBy ที่สร้างขึ้น LastModifiedBy LastModifiedDate ต่อไปนี้เป็นรายการการกำหนดค่าเฉพาะ
ตัวอย่าง
สร้างคลาสเอนทิตี
แพ็คเกจ com.hfcsbc.infrastructureservice.domain; นำเข้า com.hfcsbc.repository.support.domain.abstractauditingentity นำเข้า lombok.data; นำเข้า org.springframework.data.annotation.date. org.springframework.data.annotation.lastmodifiedby; นำเข้า org.springframework.data.annotation.lastmodifieddate; นำเข้า org.springframework.data.jpa.domain.support.support. สร้างโดย Pengchao เมื่อ 2018/3/7 */@entity@data@entityListeners ({AuditingEntityListener.class}) บุคคลชั้นเรียนสาธารณะ {@id @Id @GeneratedValue Long ID ส่วนตัว; ชื่อสตริงส่วนตัว; อายุจำนวนเต็มส่วนตัว @createdby @column (name = "created_by", nullable = false, length = 50, update = false) สตริงส่วนตัวที่สร้างขึ้นโดย; @CreatedDate @Column (name = "createD_Date", nullable = false, update = false) วันที่ส่วนตัว createDate = new Date (); @lastModifiedBy @Column (name = "last_modified_by", ความยาว = 50) สตริงส่วนตัว LastModifiedBy; @LastModifiedDate @Column (name = "last_modified_date") วันที่ส่วนตัว lastModifiedDate = วันที่ใหม่ ();}สร้างที่เก็บข้อมูลที่สอดคล้องกัน
แพ็คเกจ com.hfcsbc.repository; นำเข้า com.hfcsbc.domain.person; นำเข้า org.springframework.data.jpa.repository.jparepository;/** * สร้างโดย Pengchao เมื่อปี 2018/3/7 *
กำหนดค่าถั่วเพื่อรับข้อมูลผู้ใช้
แพ็คเกจ com.hfcsbc.infrastructureservice.config; นำเข้า org.springframework.data.domain.auditoraware; นำเข้า org.springframework.security.core.authentication; นำเข้า org.springframework.security org.springframework.stereotype.Component; นำเข้า java.util.optional;/** * สร้างโดย Pengchao เมื่อ 2018/3/7 */ @Component ("Auditoraware") ผู้ตรวจสอบบัญชีสาธารณะ SecurityContextholder.getContext (). getAuthentication (); return poiceal.of (Authentication.getPrincipal (). ToString ()); -เปิดฟังก์ชั่นการตรวจสอบในคลาสสปริงบูตรายการ
แพ็คเกจ com.hfcsbc.infrastructureservice; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.springbootapplication; นำเข้า org.springframework.data.jpa. org.springframework.scheduling.annotation.enableasync;@springbootapplication@enablejpaauditing (AuditorAwareref = "Auditoraware")@enableasyncpublic personapplication -
นั่นคือการกำหนดค่าจะเสร็จสมบูรณ์ เมื่อบันทึกวัตถุโดยใช้ที่เก็บข้อมูลที่สร้างขึ้นโดย LastModifiedBy LastModifiedDate มีฟังก์ชั่นการตรวจสอบเพื่อแทรกโดยอัตโนมัติ
หมายเหตุ: วิธีรับข้อมูลผู้ใช้ในวิธีการแบบอะซิงโครนัส
เนื่องจากคุณใช้ที่เก็บเพื่อบันทึกวัตถุในวิธีการแบบอะซิงโครนัสคุณจะไม่สามารถรับข้อมูลผู้ใช้ผู้ใช้ได้ คุณต้องเพิ่มรายการการกำหนดค่าต่อไปนี้เพื่อรับข้อมูลผู้ใช้ในการรับรองความถูกต้อง
แพ็คเกจ com.hfcsbc.config; นำเข้า org.springframework.beans.factory.config.methodinvokingfactorybean; นำเข้า org.springframework.context.annotation.bean; นำเข้า org.springframework.context.annotation org.springframework.security.core.context.securitycontextholder;/** * สร้างโดย Pengchao เมื่อปี 2018/3/7 */ @configurationpublic คลาส AuditorawareConfig {@bean methodinvokingfactorybean method MethodInvokingFactoryBean (); MethodInvokingFactoryBean.settargetClass (SecurityContextholder.class); MethodInvokingFactoryBean.settargetMethod ("setstrategyname"); MethodInvokingFactoryBean.setArguments (สตริงใหม่ [] {SecurityContextholder.Mode_InherItableThreatHreadLocal}); วิธีการกลับมาใช้วิธีการ FactoryBean; -ฟังก์ชั่นหลักของ SecurityContextholder คือการเชื่อมโยงกระบวนการที่ดำเนินการในปัจจุบันกับ SecurityContext
SecurityContextholder.mode_inheritableThreadLocal: ใช้ในกรณีที่เธรดมีความสัมพันธ์แบบพาเรนต์-ลูกเธรดลูกรวม SecurityContextholder ของเธรดพาเรนต์;
SecurityContextholder.mode_inheritableThreadLocal: แบ่งปันความปลอดภัย CONTERCONTEXTHOLDER ทั่วโลก
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น