Springboot แก้ปัญหา Ajax cross-domain สำหรับการอ้างอิงของคุณ เนื้อหาเฉพาะมีดังนี้
1. วิธีแรก
1. เขียนการกำหนดค่าที่รองรับคำขอข้ามโดเมน
นำเข้า org.springframework.context.annotation.configuration; นำเข้า org.springframework.web.servlet.config.annotation.corsregistry; นำเข้า org.springframework.web.servlet.config.annotation. levin * @time 2017-07-13 */ @configurationPublic คลาส Corsconfig ขยาย webmvcconfigureradapter {สตริงสุดท้ายของสตริงคงที่ [] = สตริงใหม่ [] {"รับ", "โพสต์", "ใส่", "ลบ"}; @Override โมฆะสาธารณะ AddCorsMappings (Corsregistry Registry) {registry.addmapping ("/**"). redaleRigins ("*"). allycredentials (จริง) .allowedMethods (ต้นกำเนิด) .MAXAGE -2. อินเตอร์เฟสคำขอ http
@RestControllerPublic คลาส Hellocontroller {@autowired HelloService HelloService; @getMapping (value = "/test", ผลิต = mediaType.application_json_utf8_value) การสืบค้นสตริงสาธารณะ () {return "hello"; -2. วิธีที่สอง (แนะนำ)
PS: ประเภทแรกมีปัญหา เมื่อเซิร์ฟเวอร์โยน 500 ยังมีปัญหาข้ามโดเมน
@springbootapplication@componentscan@enablecoverycoverycelientpublic การจัดการคลาสการจัดการ {โมฆะคงที่สาธารณะหลัก (สตริง [] args) {springapplication.run (managementApplication.class, args); } corsconfiguration ส่วนตัว buildConfig () {corsconfiguration corsconfiguration = new corsconfiguration (); Corsconfiguration.addallowedorigin ("*"); Corsconfiguration.addallowedheader ("*"); Corsconfiguration.addallowedMethod ("*"); corsconfiguration.addexposedheader (httpheaderconstant.x_total_count); ส่งคืน corsconfiguration; } / ** * ตัวกรองข้ามโดเมน * * @return * / @bean Public Corsfilter Corsfilter () {urlbasedCorsConfigurationsource แหล่งที่มา = ใหม่ urlbasedCorsCorsConfigurationsource (); Source.RegisterCorsConfiguration ("/**", buildConfig ()); // 4 ส่งคืน corsfilter ใหม่ (แหล่งที่มา); -2. index.html
<! doctype html> <html> <head> <meta charset = "utf-8"> <title> คำขอข้ามโดเมน </title> <script src = "https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js" $ ("ปุ่ม") คลิก (ฟังก์ชั่น () {$ .ajax ({url: "http: // localhost: 8080/test", ความสำเร็จ: ฟังก์ชั่น (ผลลัพธ์) {$ ("#p1"). html (ผลลัพธ์);}});});}); เนื้อหา </button> </body> </html>3. วิธีที่สามคือการเขียนตัวกรองตัวกรอง
แพ็คเกจ com.cci.market.common.filter; นำเข้า java.io.ioexception; นำเข้า Javax.servlet.filter; นำเข้า Javax.servlet.filterchain; นำเข้า Javax.servlet.filterconfig; นำเข้า Javax.servlet.servletexception; javax.servlet.servletResponse; นำเข้า Javax.servlet.http.httpservletResponse; นำเข้า org.springframework.stereotype.Component;/** * จัดการปัญหาข้ามโดเมน * @author Mr.zheng * @date 2016/08/08/08 เป็นโมฆะ init (filterConfig filterConfig) พ่น servletexception {} @Override โมฆะสาธารณะ dofilter (servletrequest req, servletResponse res, chain filterchain) โยน ioexception, servletexception Response.Setheader ("การควบคุมการควบคุม-Ollow-Origin", "*"); Response.Setheader ("การควบคุมการควบคุม-วิธีการ", "โพสต์, รับ, ตัวเลือก, ลบ, ใส่"); Response.Setheader ("Access-Control-Max-age", "3600"); Response.Setheader ("การควบคุมการควบคุม-หัว", "X-requested-with"); chain.dofilter (req, res); } @Override โมฆะสาธารณะทำลาย () {// todo วิธีการที่สร้างอัตโนมัติ stub}}}}4. การกำหนดค่า Nginx cross-domain
Nginx cross-domain นั้นค่อนข้างง่ายเพียงเพิ่มการกำหนดค่าต่อไปนี้
ตำแหน่ง/{proxy_pass http: // localhost: 8080; if ($ request_method = 'ตัวเลือก') {add_header 'การเข้าถึงการควบคุม-Ollow-Origin' '*'; add_header 'Access-Control-Ollow-Methods' 'Get, Post, Options'; add_header 'การควบคุมการควบคุมหัวหน้า' 'dnt, x-customheader, การรักษา, ผู้ใช้, เอเจนต์, x-requested-with, ถ้าดัดแปลง----, cache-control, ประเภทเนื้อหา, ช่วงเนื้อหา, ช่วง, โทเค็น'; add_header 'Access-Control-Max-age' 1728000; add_header 'type-type' 'ข้อความ/ธรรมดา; charset = utf-8 '; add_header 'ความยาวเนื้อหา' 0; กลับ 204; } if ($ request_method = 'post') {add_header 'access-control-allow-Origin' '*'; add_header 'Access-Control-Ollow-Methods' 'Get, Post, Options'; add_header 'การควบคุมการควบคุมหัวหน้า' 'dnt, x-customheader, การรักษา, ผู้ใช้, เอเจนต์, x-requested-with, ถ้าดัดแปลง----, cache-control, ประเภทเนื้อหา, ช่วงเนื้อหา, ช่วง, โทเค็น'; add_header 'การควบคุมการควบคุม-หน้าที่-หัว' 'DNT, X-Customheader, ผู้ใช้งาน, ผู้ใช้, x-requested-with, if-modified-snine, cache-control, ประเภทเนื้อหา, ช่วงเนื้อหา, ช่วง, โทเค็น'; } if ($ request_method = 'get') {add_header 'access-control-allow-Origin' '*'; add_header 'Access-Control-Ollow-Methods' 'Get, Post, Options'; add_header 'การควบคุมการควบคุมหัวหน้า' 'dnt, x-customheader, การรักษา, ผู้ใช้, เอเจนต์, x-requested-with, ถ้าดัดแปลง----, cache-control, ประเภทเนื้อหา, ช่วงเนื้อหา, ช่วง, โทเค็น'; add_header 'การควบคุมการควบคุม-หน้าที่-หัว' 'DNT, X-Customheader, ผู้ใช้งาน, ผู้ใช้, x-requested-with, if-modified-snine, cache-control, ประเภทเนื้อหา, ช่วงเนื้อหา, ช่วง, โทเค็น'; -สถานที่: add_header 'การควบคุมการควบคุม-กลไก' อย่าลืมเพิ่มส่วนหัวที่คุณนำมาเมื่อคุณร้องขอ ตัวอย่างเช่น "โทเค็น" ในตัวอย่างนี้จะถูกส่งจริงจากปลายด้านหน้าไปด้านหลัง ไม่สำคัญว่าคุณจะจำไม่ได้ว่าดีบักเกอร์ของเบราว์เซอร์จะมีคำแนะนำโดยละเอียด
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น