1. คำนำ
รหัสการตรวจสอบสามารถกล่าวได้ว่าเป็นเรื่องธรรมดามากในชีวิตของเรา เว็บไซต์หรือแอพใด ๆ จะมีฟังก์ชั่นนี้ แต่ทำไมคุณถึงต้องการสิ่งนี้? ทำอย่างไร? ด้านล่างบรรณาธิการจะทำให้ทุกคนใช้ Java เพื่อทำหน้าที่ของรหัสการตรวจสอบให้สมบูรณ์
2. ฟังก์ชั่นของรหัสการตรวจสอบ
โดยทั่วไปมันสามารถป้องกันไม่ให้ผู้คนใช้หุ่นยนต์เพื่อลงทะเบียนโดยอัตโนมัติในแบทช์และเข้าสู่ระบบอย่างต่อเนื่องไปยังผู้ใช้ที่ลงทะเบียนเฉพาะโดยใช้โปรแกรมเฉพาะเพื่อวิธีการแคร็กเดรัจฉาน เนื่องจากรหัสการตรวจสอบเป็นภาพที่ผสมตัวเลขและสัญลักษณ์จึงดูยากสำหรับสายตามนุษย์ที่จะรับรู้และมันก็ยิ่งยากสำหรับเครื่องที่จะจดจำได้
3. การใช้งาน Java
3.1 สร้างคลาส servlet สำหรับรหัสการตรวจสอบ:
แพ็คเกจ com.bjpowernode.drp.util.servlet; นำเข้า java.awt.color; นำเข้า java.awt.font; นำเข้า java.awt.graphics; นำเข้า java.awt.image.bufferedimage; นำเข้า Java.io.ioException; javax.servlet.servletexception; นำเข้า javax.servlet.http.httpservlet นำเข้า Javax.servlet.http.httpservletrequest นำเข้า Javax.servlet.http.httpservletresponse; ARES * */Authimage คลาสสาธารณะขยาย HTTPSERVLET {สตริงสุดท้ายคงสุดท้าย content_type = "text/html; charset = gb2312"; // ตั้งค่าขนาดตัวอักษรขนาดตัวอักษรส่วนตัว mfont = ตัวอักษรใหม่ ("Times New Roman", Font.plain, 17); public void init () พ่น servletexception {super.init (); } color getRandColor (int fc, int bc) {สุ่มสุ่ม = new Random (); ถ้า (FC> 255) FC = 255; ถ้า (BC> 255) BC = 255; int r = fc+random.nextint (bc-fc); int g = fc+random.nextint (bc-fc); int b = fc+random.nextint (bc-fc); int b = fc+random.nextint (bc-fc); คืนสีใหม่ (R, G, B); } บริการโมฆะสาธารณะ (คำขอ httpservletrequest, การตอบสนอง httpservletResponse) พ่น servletexception, ioexception {response.setheader ("pragma", "no-cache"); Response.Setheader ("แคชควบคุม", "ไม่มีแคช"); Response.setDateHeader ("หมดอายุ", 0); // ระบุว่าการตอบสนองที่สร้างขึ้นคือการตอบสนองของรูปภาพ SetContentType ("image/jpeg"); ความกว้าง int = 100, ความสูง = 18; BufferedImage Image = New BufferedImage (ความกว้าง, ความสูง, bufferedImage.type_int_rgb); กราฟิก g = image.getGraphics (); สุ่มสุ่ม = ใหม่สุ่ม (); G.SetColor (GetRandColor (200,250)); G.FillRect (1, 1, WIDTH-1, ความสูง -1); G.SetColor (สีใหม่ (102,102,102)); G.DrawRect (0, 0, WIDTH-1, ความสูง -1); G.SetFont (MFONT); G.SetColor (GetRandColor (160,200)); // วาดเส้นสุ่มสำหรับ (int i = 0; i <155; i ++) {int x = random.nextint (ความกว้าง - 1); int y = random.nextint (ความสูง - 1); int xl = random.nextint (6) + 1; int yl = random.nextint (12) + 1; G.Drawline (x, y, x + xl, y + yl); } // วาดเส้นสุ่มจากทิศทางอื่นสำหรับ (int i = 0; i <70; i ++) {int x = random.nextint (ความกว้าง - 1); int y = random.nextint (ความสูง - 1); int xl = random.nextint (12) + 1; int yl = random.nextint (6) + 1; G.Drawline (x, y, x - xl, y - yl); } // สร้างตัวเลขสุ่มและแปลงหมายเลขสุ่มเป็นตัวอักษรสตริง srand = ""; สำหรับ (int i = 0; i <6; i ++) {int itmp = random.nextint (26)+65; ถ่าน CTMP = (ถ่าน) ITMP; srand += string.valueof (ctmp); G.SetColor (สีใหม่ (20+random.nextint (110), 20+random.nextint (110), 20+random.nextint (110))); G.DrawString (String.ValueOf (CTMP), 15*i+10,16); } httpsession session = request.getSession (จริง); Session.setAttribute ("Rand", Srand); G.Dispose (); Imageio.write (รูปภาพ, "jpeg", response.getOutputStream ()); } โมฆะสาธารณะทำลาย () {}}3.2 การกำหนดค่า servlet ใน web.xml ภายใต้ไฟล์ web-inf
// ชื่อ servlet และตำแหน่ง <servlet> <servlet-name> authimage </servlet-name> <servlet-lass> com.bjpowernode.drp.util.servlet.authimage </servlet-class> </servlet> // servlet ทำงานบนการแมป <url-pattern>/servlet/login/authimage </url-pattern> </servlet-mapping>
3.3 แผนกต้อนรับเรียก servlet
รหัส JSP:
<img id = "img" style = "เคอร์เซอร์: ตัวชี้;" src = "$ {pageContext.request.contextpath}/servlet/login/authimage" onclick = "Changeimg ()">>JS:
// คลิกรหัสการยืนยันเพื่อเปลี่ยนฟังก์ชั่นภาพ CHANGEIMG () {var img = document.getElementById ("IMG"); img.src = "$ {pageContext.request.contextpath}/servlet/login/authimage? d ="+math.random (); ส่งคืน false;}4. สรุป
ด้วยการเรียนรู้รหัสการตรวจสอบนี้ฉันยังได้รับความเข้าใจบางอย่างเกี่ยวกับการมีปฏิสัมพันธ์ระหว่างเซิร์ฟเล็ตทั้งหมดและรหัสส่วนหน้าและการส่งข้อมูลได้รับการปรับปรุงอย่างมากเช่นกัน ดังนั้นฉันยังต้องเรียนรู้เพิ่มเติมเกี่ยวกับทักษะเหล่านี้และสรุปพวกเขาในอนาคต ด้วยวิธีนี้ความรู้จะเป็นของแข็ง
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น