บทความนี้แนะนำการใช้ interceptors ใน Spring-Boot โดยทั่วไปแล้วพวกเขาจัดการการประมวลผลข้ามโดเมนในตัวดักทำให้การเข้าถึงโครงการข้ามโดเมน สำหรับข้อมูลโดยละเอียดเกี่ยวกับการใช้ตัวดักจับโปรดดูที่เว็บไซต์อย่างเป็นทางการ
ขั้นตอนในการใช้งาน Interceptor ที่กำหนดเอง:
1. สร้างคลาสและใช้อินเทอร์เฟซ HandlerInterceptor
2. สร้างคลาส Java เพื่อสืบทอด WebMVCCONFigurerAdapter และแทนที่วิธี AddInterceptors
2. ส่งมอบการสกัดกั้นที่กำหนดเองเพื่อการจัดการสปริงจากนั้นเพิ่มวัตถุด้วยตนเองลงในห่วงโซ่ interceptor (เพิ่มในวิธี addInterceptors)
สร้างคลาส Interceptor
แพ็คเกจ com.example.springboot.config.intercepter; นำเข้า org.springframework.stereotype.component; นำเข้า org.springframework.web.servlet.handlerinterceptor; นำเข้า org.springframework.web.servlet.modelandview; javax.servlet.http.httpservletrequest; นำเข้า Javax.servlet.http.httpservletResponse;/** * @desc custom interceptor, ใช้ @component handlerInterceptor {@Override บูลีนสาธารณะ prehandle (คำขอ httpservletrequest, httpservletResponse การตอบสนอง, ตัวจัดการวัตถุ) โยนข้อยกเว้น {system.out.println (">>> prehandle >>>>>>>>>>>> โทรก่อนการดำเนินการตามคำขอ setcrossdomain (การตอบสนอง); Return True; // เฉพาะเมื่อผลตอบแทนที่แท้จริงจะดำเนินการต่อไปเพื่อลดลงส่งคืน FALSE เพื่อยกเลิกคำขอปัจจุบัน} @Override Public Void Postthandle (คำขอ httpservletRequest, การตอบสนอง httpservletResponse, ตัวจัดการวัตถุ, modelandview modelandview) System.out.println (">>> Postthandle >>>>>>>>>>>>>>>>>> โทรหลังจากดำเนินการตามคำขอ แต่ก่อนที่จะแสดงการแสดงผล (หลังจากวิธีการควบคุมถูกเรียก)"); } @Override โมฆะสาธารณะ ftercompletion (คำขอ httpservletrequest, การตอบกลับ httpservletResponse, ตัวจัดการวัตถุ, ข้อยกเว้น ex) โยนข้อยกเว้น {// เรียกหลังจากการร้องขอทั้งหมดสิ้นสุดลงนั่นคือดำเนินการหลังจาก dispatcherservlet แสดงมุมมองที่เกี่ยวข้อง System.out.println (">>> Postthandle >>>>>>>>>>>>>>> ถูกเรียกหลังจากการร้องขอทั้งหมดสิ้นสุดลงนั่นคือหลังจาก dispatcherservlet แสดงมุมมองที่สอดคล้องกัน (ส่วนใหญ่สำหรับการทำความสะอาดทรัพยากร)"); } / *** @param @param การตั้งค่าการตั้งค่าไฟล์* @return โมฆะส่งคืนประเภท* @throws* @title: setcrossdomain* @description: todo (ตั้งค่าปัญหาข้ามโดเมน)* / โมฆะส่วนตัว setcrossdomain Response.addheader ("การควบคุมการควบคุม-วิธีการ", "โพสต์, รับ"); Response.addheader ("การควบคุมการควบคุม-อนุญาต-credentials", "true"); -เพิ่ม interceptor ที่กำหนดเองลงในห่วงโซ่ interceptor
แพ็คเกจ com.example.springboot.config.config; นำเข้า com.example.springboot.config.intercepter.testintercepter; นำเข้า org.springframework.beans.factory.annotation.autowired; org.springframework.web.servlet.config.annotation.interceptorRegistry; นำเข้า org.springframework.web.servlet.config.annotation.webmvcconfigureradapter Testinterceptor Testinterceptor; @Override โมฆะสาธารณะ AddInterceptors (InterceptorRegistry Registry) {registry.addinterceptor (testinterceptor); -สร้างคลาสคอนโทรลเลอร์
แพ็คเกจ com.example.springboot.config.conroller; นำเข้า org.springframework.web.bind.annotation.requestmapping; นำเข้า org.springframework.web.bind.annotation.restcontroller; String Hello () {return "Hello"; -สร้างคลาสบริการเริ่มต้น
แพ็คเกจ com.example.springboot.config; นำเข้า org.springframework.boot.springapplication; นำเข้า org.springframework.boot.autoconfigure.springbootapplication; @springbootaplicplication SpringApplication.run (SpringbootConfigapplication.class, args); -
เริ่มต้นการทดสอบบริการเยี่ยมชม http: // localhost: 8088/hello ในเบราว์เซอร์พิมพ์บันทึกดังนี้คุณจะเห็นว่า interceptor ผ่านไปแล้ว
สรุป
ด้านบนเป็นคำอธิบายโดยละเอียดเกี่ยวกับการใช้สปริงบู๊ต config interceptors ที่แนะนำโดยตัวแก้ไข ฉันหวังว่ามันจะเป็นประโยชน์กับทุกคน หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉัน บรรณาธิการจะตอบกลับทุกคนในเวลา!