บทความนี้แบ่งปันรหัสเฉพาะของรหัสตรวจสอบ Kaptcha Integrated Kaptcha สำหรับการอ้างอิงของคุณ เนื้อหาเฉพาะมีดังนี้
1. Kaptcha บทนำที่เกี่ยวข้อง
Kaptcha เป็นโครงการโอเพ่นซอร์สสำหรับรหัสการตรวจสอบตาม SimpleCaptcha
2. โซลูชันการรวม
①การพึ่งพาการกำหนดค่าใน pom.xml
<!-รหัสการตรวจสอบ-> <การพึ่งพา> <roupId> com.github.penggle </groupId> <ratifactid> kaptcha </artifactid> <version> 2.3.2 </version> </predency>
②การกำหนดค่ารหัสการตรวจสอบ kaptcha การตั้งค่าที่เกี่ยวข้อง
@ConfigurationPublic คลาส kaptchaconfig {@bean (name = "captchaproducer") สาธารณะ defaultkaptcha getKaptChabean () {defaultKaptcha defaultKaptcha = ใหม่ defaultKaptcha (); คุณสมบัติคุณสมบัติ = คุณสมบัติใหม่ (); Properties.SetProperty ("kaptcha.border", "ใช่"); Properties.SetProperty ("kaptcha.border.color", "105,179,90"); Properties.setProperty ("kaptcha.textproducer.font.color", "blue"); Properties.SetProperty ("kaptcha.image.width", "125"); Properties.SetProperty ("kaptcha.image.height", "45"); Properties.SetProperty ("kaptcha.session.key", "รหัส"); Properties.SetProperty ("kaptcha.textproducer.char.length", "4"); Properties.SetProperty ("kaptcha.textproducer.font.names", "Song Font, Kai Font, Microsoft Yahei"); config = config ใหม่ (คุณสมบัติ); defaultkaptcha.setConfig (config); ส่งคืน defaultkaptcha; -หรือ
สร้างไฟล์ mykaptcher.xml ภายใต้ทรัพยากร
<? xml version = "1.0" การเข้ารหัส = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://ww.w3.org/2001/xml XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd" type = "java.util.properties"> <props> <prop key = "kaptcha.border"> ใช่ </prop> <prop key = "kaptcha.border.color"> 105,179,90 </prop> <prop key = "kaptcha.textproducer.font.color" key = "kaptcha.image.width"> 100 </prop> <prop key = "kaptcha.image.height"> 50 </prop> <prop key = "kaptcha.image.height"> 50 </prop> <prop key = "kaptcha.image.height" key = "kaptcha.textproducer.font.color"> blue </prop> <prop key = "kaptcha.image.width"> 100 </prop> <prop key = "kaptcha.image.height"> 50 </prop> key = "kaptcha.session.key"> รหัส </prop> <prop key = "kaptcha.textproducer.char.length"> 4 </prop> <prop key = "kaptcha.textproducer.font.names"> ตัวอักษรเพลง key = "kaptcha.textproducer.char.string"> 23456789abcefghjkmnopqrstuvwxyz </prop> <prop key = "kaptcha.obscurificator.impl"> com.google.code.kaptcha.impl.ateripl.ater <prop key = "kaptcha.noise.impl"> com.google.code.kaptcha.impl.nonoise </prop> <!-<prop key = "kaptcha.noise.impl"> com.google.code.kaptcha.impl.defaultnoise key = "kaptcha.background.clear.from"> 185,56,213 </prop> <prop key = "kaptcha.background.clear.to"> สีขาว </prop> <prop key = "kaptcha.textproducer.char.space"> 3 </prop>
จากนั้นโหลดการกำหนดค่าในแอปพลิเคชันเริ่มต้น
@enableTransactionManagement // เริ่มการจัดการธุรกรรมคำอธิบายประกอบซึ่งเทียบเท่ากับวิธีการกำหนดค่า XML <tx: คำอธิบายประกอบ-ไดรฟ์/>@springbootapplication@enableceduling // เริ่มงานกำหนดเวลาคำอธิบายประกอบ @mapperscan (basepackages = "com.shawn.mapper")@importresource (locations = {"classpath: mykaptcha.xml"}) แอปพลิเคชันระดับสาธารณะขยาย SpringbootservletInitializer {โมฆะคงที่สาธารณะหลัก (สตริง [] Args) โยนข้อยกเว้น -วิธีการกำหนดค่าทั้งสองมีอยู่ใน Springboot;
③kaptchacontroller
@commonslog @controllerpublic คลาส kaptchacontroller ขยาย basecontroller {@autoWired Private Producer Captchaproducer; @getMapping ("/getKaptChaimage") โมฆะสาธารณะ getKaptChaimage () โยนข้อยกเว้น {response.setDateHeader ("หมดอายุ", 0); // ตั้งค่ามาตรฐาน HTTP/1.1 NO-CACHE Response.SetheAder ("Cache-Control", "ไม่มีร้านค้าไม่ต้อง-ต้องทำการตรวจสอบ"); // set IE ขยาย HTTP/1.1 ส่วนหัวไม่มีแคช (ใช้ addheader) Response.addheader ("แคชควบคุม", "post-check = 0, pre-check = 0"); // ตั้งค่ามาตรฐาน HTTP/1.0 NO-CACHE Response.Setheader ("Pragma", "No-cache"); // ส่งคืน jpeg response.setContentType ("image/jpeg"); // สร้างข้อความสำหรับสตริงภาพ captext = captchaproducer.createtext (); // จัดเก็บข้อความในเซสชัน //request.getSession().setAttribute(constants.kaptcha_session_key, captext); // บันทึกรหัสการตรวจสอบเป็นเซสชันเซสชัน SetAttribute (ค่าคงที่ Kaptcha_session_key, captext); log.info (captext); // สร้างภาพด้วย bufferedimage bi = captchaproducer.createImage (captext); servletoutputStream out = response.getOutputStream (); // เขียนข้อมูล imageio.write (bi, "jpg", ออก); ลอง {out.flush (); } ในที่สุด {out.close (); -3. ทดสอบเอฟเฟกต์
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น