บทความนี้ได้แชร์รหัสเฉพาะของรหัสตรวจสอบการเข้าสู่ระบบ Javaweb สำหรับการอ้างอิงของคุณ เนื้อหาเฉพาะมีดังนี้
ใช้:
คอนโทรลเลอร์: สร้างรหัสการตรวจสอบ
@RequestMapping ("/user/check.jpg") โมฆะสาธารณะ createCode (คำขอ httpservletrequest, การตอบสนอง httpservletResponse) โยน ioexception {// แจ้งเบราว์เซอร์ไม่ให้ตอบสนองแคช Response.Setheader ("แคชควบคุม", "ไม่มีแคช"); Response.Setheader ("Pragma", "-1"); captchautil util = captchautil.instance (); // ป้อนรหัสการตรวจสอบลงในเซสชันเพื่อตรวจสอบรหัสสตริง = util.getString (); request.getSession (). setAttribute ("รหัส", รหัส); // เอาต์พุตหน้าเว็บ imageio.write (util.getImage (), "jpg", response.getOutputStream ()); - JSP: แสดงรหัสการยืนยัน
<img id = "img" src = "<%= basepath%> user/check.jpg" onclick = "Refresh ()">
ฟังก์ชั่นรีเฟรช () {var url = $ ("#basepath"). val () + "ผู้ใช้/check.jpg? number =" + math.random (); $ ("#img"). attr ("src", url); - ตรวจสอบ:
รับรหัสในเซสชันที่สอดคล้องกับรหัสที่ส่งกลับมาเบื้องหน้า
/ ** * การตรวจสอบรหัสการตรวจสอบ * * @param เซสชัน * @param รหัส */ private void checkCode (เซสชัน httpsession, รหัสสตริง) {String codesession = (String) Session.getAttribute ("Code"); if (stringUtils.isEmpty (รหัส)) {log.error ("ไม่มีการสร้างข้อมูลรหัสการตรวจสอบ"); โยนใหม่ unlilstatexception ("ERR-01000"); } if (stringUtils.isEmpty (รหัส)) {log.error ("ไม่มีข้อมูลรหัสการยืนยันถูกกรอกใน"); โยน BusinessException ใหม่ ("ERR-06018"); } if (codesession.equalsignorecase (รหัส)) {// รหัสการตรวจสอบถูกส่งผ่าน} else {log.error ("ข้อผิดพลาดรหัสการตรวจสอบ"); โยน BusinessException ใหม่ ("ERR-06019"); - เครื่องมือ:
นำเข้า Java.awt.Color; นำเข้า Java.awt.Font; นำเข้า java.awt.graphics; นำเข้า java.awt.image.bufferedimage; นำเข้า java.util.random; / ** * เครื่องมือสร้างรหัสการตรวจสอบ * @author hxl * */ คลาสสาธารณะ captchautil {private bufferedimage image; // image private string str; // รหัสการตรวจสอบรหัสส่วนตัวคงที่ [] = "abcdefghijklmnopqrstuvwxyabcdefhijklmnopqqq สตริงสุดท้ายคงที่สาธารณะเซสชัน _code_name = "รหัส"; Private Captchautil () {init (); // คุณสมบัติการเริ่มต้น}/* รับอินสแตนซ์แบบสุ่ม Numutil*/ อินสแตนซ์ captchautil แบบคงที่สาธารณะ () {ส่งคืน captchautil ใหม่ (); } /** รับรูปภาพการตรวจสอบภาพ* / public bufferedImage getImage () {return this.image; } /** รับรหัสการยืนยันสำหรับรูปภาพ* / สตริงสาธารณะ getString () {return this.str; } โมฆะส่วนตัว init () {// สร้างภาพความกว้าง int = 85, ความสูง = 20; BufferedImage Image = New BufferedImage (ความกว้าง, ความสูง, bufferedImage.type_int_rgb); // รับกราฟิกบริบทกราฟิก g = image.getGraphics (); // สร้างการสุ่มสุ่ม = new Random (); // ตั้งค่าสีพื้นหลัง G.SetColor (GetRandColor (200, 250)); G.FillRect (0, 0, ความกว้าง, ความสูง); // ตั้งค่าตัวอักษร G.SetFont (ฟอนต์ใหม่ ("Times New Roman", Font.plain, 18)); // แบบสุ่มสร้างสายรบกวน 155 เส้นทำให้รหัสการตรวจสอบสิทธิ์ในภาพมีโอกาสน้อยที่จะตรวจพบได้โดยโปรแกรมอื่น ๆ G.SetColor (GetRandColor (160, 200)); สำหรับ (int i = 0; i <155; i ++) {int x = random.nextint (ความกว้าง); int y = random.nextint (ความสูง); int xl = random.nextint (12); int yl = random.nextint (12); G.Drawline (x, y, x + xl, y + yl); } // ใช้รหัสการรับรองความถูกต้องที่สร้างขึ้นแบบสุ่ม (หมายเลข 4 หลัก) สตริง srand = ""; สำหรับ (int i = 0; i <4; i ++) {string rand = string.valueof (รหัส [random.nextint (code.length)]); SRAND += RAND; // แสดงรหัสการรับรองความถูกต้องในรูปภาพ G.SetColor (สีใหม่ (20 + random.nextint (110), 20 + random.nextint (110))); // สีของฟังก์ชั่นการโทรเหมือนกันอาจเป็นเพราะเมล็ดอยู่ใกล้เกินไปดังนั้นคุณสามารถสร้าง G.DrawString โดยตรง (Rand, 13 * i + 6, 16); } // รหัสการตรวจสอบที่ได้รับมอบหมาย this.str = srand; // ภาพมีผล g.dispose (); // byteArrayInputStream อินพุต = null; // byteArrayOutputStream output = new ByTeArrayOutputStream (); // ลอง {// imageOutputStream imageOut = imageio.createImageOutputStream (เอาต์พุต); // imageio.write (รูปภาพ, "jpeg", imageout); // imageout.close (); // input = new ByteArrayInputStream (output.tobyteArray ()); //} catch (Exception e) {// system.out.println ("เกิดข้อผิดพลาดเมื่อสร้างภาพรหัสการยืนยัน:" + e.toString ()); //} // this.image = อินพุต this.image = image;/* กำหนดรูปภาพ*/}/** รับสีแบบสุ่มสำหรับช่วงที่กำหนด*/ สีส่วนตัว getRandColor (int fc, int bc) {random random = 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); คืนสีใหม่ (R, G, B); - สุดท้ายเรามาอธิบาย: ตัวดักจับเข้าสู่ระบบจะต้องปล่อยพา ธ เพื่อสร้างรหัสการตรวจสอบ! เข้าสู่ระบบ Interceptor ต้องปล่อยพา ธ เพื่อสร้างรหัสการตรวจสอบ! - เข้าสู่ระบบ Interceptor ต้องปล่อยพา ธ เพื่อสร้างรหัสการตรวจสอบ! - - พูดสิ่งสำคัญสามครั้ง ~~
จอแสดงผลล่าสุด:
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น