บทความนี้แนะนำ Springboot รวมกับ Springsecurity เพื่อใช้ฟังก์ชั่นรหัสการตรวจสอบกราฟิกและแชร์กับคุณดังนี้:
สร้างรหัสตรวจสอบกราฟิก
กระบวนการสร้างรหัสการตรวจสอบกราฟิกนั้นค่อนข้างง่ายและไม่มีส่วนเกี่ยวข้องกับความปลอดภัยของสปริงส์ ดังนั้นฉันเพิ่งโพสต์รหัส
สร้างรูปภาพตามตัวเลขสุ่ม
/** * สร้างรหัสการตรวจสอบกราฟิก * @param Request * @return */Private ImageCode Generate (คำขอ servletwebrequest) {int width = 64; ความสูง int = 32; BufferedImage Image = New BufferedImage (ความกว้าง, ความสูง, bufferedImage.type_int_rgb); กราฟิก g = image.getGraphics (); สุ่มสุ่ม = ใหม่สุ่ม (); G.SetColor (GetRandColor (200, 250)); G.FillRect (0, 0, ความกว้าง, ความสูง); G.SetFont (ฟอนต์ใหม่ ("Times New Roman", Font.italic, 20)); 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); } สตริง srand = ""; สำหรับ (int i = 0; i <4; i ++) {string rand = string.valueof (random.nextint (10)); SRAND += RAND; G.SetColor (สีใหม่ (20 + random.nextint (110), 20 + random.nextint (110), 20 + random.nextint (110))); G.DrawString (แรนด์, 13 * i + 6, 16); } G.Dispose (); ส่งคืน ImageCode ใหม่ (Image, Srand, 60);}/** * สร้างแถบพื้นหลังแบบสุ่ม * * @param fc * @param bc * @return */สีส่วนตัว getRandColor (int fc, int bc) {สุ่มสุ่ม = new random (); if (fc> 255) {fc = 255; } if (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);}บันทึกหมายเลขสุ่มในเซสชัน && เขียนภาพที่สร้างขึ้นไปยังการตอบกลับอินเตอร์เฟส
@RestControllerPublic คลาส ValidateCodeController {สตริงสุดท้ายคงที่ session_key = "session_key_image_code"; SessionsTrategy SessionSTRATEGY = HTTPSISESSESSIONSTRATEGY () ใหม่ (); @getMapping ("/code/image") โมฆะสาธารณะ createCode (คำขอ httpservletRequest, การตอบสนอง httpservletResponse) พ่น IOException {imageCode imageCode = สร้าง (ServletWebRequest (ขอ)); SessionsTrategy.setAttribute (ServletWebrequest ใหม่ (คำขอ), session_key, imageCode); imageio.write (imageCode.getImage (), "jpeg", response.getOutputStream ()); -เพิ่มรหัสตรวจสอบกราฟิกลงในกระบวนการตรวจสอบสิทธิ์
ในคำอธิบายโดยละเอียดเกี่ยวกับกระบวนการตรวจสอบความปลอดภัยของสปริงส์เรากล่าวว่าการตรวจสอบความปลอดภัยของสปริงส์ผ่านห่วงโซ่ตัวกรอง เราต้องการตรวจสอบรหัสการตรวจสอบกราฟิกดังนั้นเราจึงสามารถตรวจสอบได้ก่อนกระบวนการตรวจสอบความถูกต้องนั่นคือก่อนที่ UsernamePasswordAuthenticationFilter
ตัวกรองรหัสตรวจสอบกราฟิกที่กำหนดเอง
@ComponentPublic Class ValidateCodeFilter ขยาย OsperRequestFilter {Private SessionsTrategy SessionsTrategy = ใหม่ httpsessionsessionStrategy (); AuthenticationFailureHandler AuthenticationFailureHandler; @Override ป้องกันโมฆะ dofilterInternal (httpservletRequest httpservletrequest, httpservletResponse httpservletResponse, filterchain filterchain) โยน servletexception, ioexception StringUtils.equalsignorecase (httpservletrequest.getMethod (), "post")) {ลอง {// 1. ตรวจสอบการตรวจสอบรหัสการตรวจสอบ (ใหม่ servletwebrequest (httpservletrequest)); } catch (validateCodeException e) {// 2. หากการตรวจสอบล้มเหลวให้โทรหาโปรเซสเซอร์การตรวจสอบความล้มเหลวของสปริงส์ กลับ ; }} // 3. หากผ่านการตรวจสอบให้ปล่อย FilterChain.dofilter (httpservletRequest, httpservletResponse); -กระบวนการตรวจสอบรหัสการตรวจสอบที่นี่ค่อนข้างง่าย ส่วนใหญ่จะกำหนดว่าพารามิเตอร์ที่ส่งผ่านนั้นสอดคล้องกับพารามิเตอร์ที่เก็บไว้ในเซสชันหรือไม่และรหัสการตรวจสอบในเซสชันหมดอายุหรือไม่
หลังจากมีตัวกรองรหัสการยืนยันของเราเองเราต้องกำหนดค่าก่อนการใช้งาน usernamepasswordauthenticationFilter:
@OverrideProtected โมฆะกำหนดค่า (httpsecurity http) พ่นข้อยกเว้น {validateCodeFilter ValidateCodeFilter = ใหม่ ValidateCodeFilter (); ValidateCodeFilter.SetAuthenticationFailureHandler (MyAuthenticationFailureHandler); // กำหนดค่าตัวกรองที่กำหนดเองของเราเป็น USERNAMEPASSWORDAUTHENTICATIONFILTER ก่อน http.addfilterbefore (validateCodeFilter, UserNamePasswordAuthenticationFilter.class). formlogin () // define หน้าเข้าสู่ระบบดาวน์โหลดรหัส
ความปลอดภัยในฤดูใบไม้ผลิ
ข้างต้นเป็นเนื้อหาทั้งหมดของบทความนี้ ฉันหวังว่ามันจะเป็นประโยชน์ต่อการเรียนรู้ของทุกคนและฉันหวังว่าทุกคนจะสนับสนุน wulin.com มากขึ้น