ตรรกะการตรวจสอบรหัส
ฉันเคยทำรหัสการตรวจสอบในโครงการมาก่อน มีรหัสมากมายบนอินเทอร์เน็ตที่สร้างรหัสการตรวจสอบและแพ็คเกจ JAR ของบุคคลที่สามบางอย่างสามารถสร้างรหัสการตรวจสอบที่สวยงาม ตรรกะการตรวจสอบรหัสนั้นง่ายมาก มันคือการวางแท็กรูปภาพในหน้าเข้าสู่ระบบและ SRC ชี้ไปที่คอนโทรลเลอร์ คอนโทรลเลอร์นี้ส่งคืนอิมเมจที่สร้างขึ้นและส่งคืนสตรีมเอาต์พุตไปยังหน้า ในขณะที่สร้างภาพข้อความบนภาพจะถูกวางไว้ในเซสชัน เมื่อเข้าสู่ระบบให้นำรหัสการตรวจสอบที่ป้อนและนำออกจากเซสชัน เปรียบเทียบทั้งสอง ครูกล่าวว่าการใช้รหัสการตรวจสอบความปลอดภัยแบบรวมของฤดูใบไม้ผลินั้นเหมือนกับที่ฉันพูด แต่มันเป็นมาตรฐานและทั่วไปมากขึ้น
Spring Security เป็นชุดของโซ่ตัวกรองดังนั้นที่นี่รหัสการตรวจสอบจะถูกประกาศว่าเป็นตัวกรองเพิ่มก่อนที่ตัวกรองการเข้าสู่ระบบของตัวกรองห่วงโซ่จากนั้นปรับแต่งคลาสข้อยกเว้นเพื่อตอบกลับข้อความแสดงข้อผิดพลาดของรหัสการยืนยัน
โครงสร้างรหัส:
รหัสการตรวจสอบถูกวางไว้ในโครงการหลักและการกำหนดค่าจะทำในโครงการเบราว์เซอร์
รหัสหลัก:
1. imagecode:
ขั้นแรกคลาส ImageCode จะห่อหุ้มภาพรหัสการตรวจสอบข้อความข้อความและเวลาหมดอายุ
แพ็คเกจ com.imooc.security.core.validate.code; นำเข้า java.awt.image.bufferedimage; นำเข้า java.time.localdatetime; นำเข้า java.time.localtime;/** รหัสการตรวจสอบ* classname: imagecode* @description BufferedImage Image; รหัสสตริงส่วนตัว; LocalDateTime ส่วนตัวหมดอายุ; // จุดเวลาหมดอายุ/** * * * <p> คำอธิบาย: </p> * @param image * @param code * @param Expiretn หมดอายุกี่วินาที */public imageCode (BufferedImage image, String Code this.image = image; this.code = code; // เวลาหมดอายุ = เวลาปัจจุบัน + จำนวนวินาทีที่หมดอายุสิ่งนี้ expireTime = localDateTime.now (). plusseconds (หมดอายุ); } public imageCode (BufferedImage Image, String Code, LocalDateTime Expiretime) {super (); this.image = image; this.code = code; this.expiretime = หมดอายุ; } / *** ไม่ว่ารหัสการตรวจสอบจะหมดอายุ* @description: ไม่ว่ารหัสการตรวจสอบจะหมดอายุ* @param @return True หมดอายุหรือไม่เท็จไม่หมดอายุ* @return บูลีนจริงหมดอายุจริงไม่หมดอายุ* @throws* @author lihaoyang } Public BufferedImage getImage () {return image; } โมฆะสาธารณะ setImage (อิมเมจ bufferedImage) {this.image = image; } สตริงสาธารณะ getCode () {รหัสส่งคืน; } โมฆะสาธารณะ setCode (รหัสสตริง) {this.code = code; } สาธารณะ localDateTime getExpireTime () {ส่งคืนวันหมดอายุ; } โมฆะสาธารณะ setExpireTime (Expiretime LocalDateTime) {this.expireTime = ExpireTime; -VerifyCode: คลาสเครื่องมือสำหรับการสร้างรหัสการตรวจสอบ ที่นี่ http://www.cnblogs.com/lihaoyang/p/7131512.html แน่นอนคุณสามารถใช้แพ็คเกจ Jar ของบุคคลที่สามไม่สำคัญ
ValidateCodeException : ข้อยกเว้นรหัสการตรวจสอบที่ห่อหุ้ม
/** * @title: validateCodeException.java * @package com.imooc.security.core.validate.code * @description: todo * @author lihaoyang * @date 2 มีนาคม 2018 */แพ็คเกจ com.imooc.security.core.validate.code; org.springframework.security.core.authenticationexception;/*** classname: validateCodeException* @description: ข้อผิดพลาดของรหัสการตรวจสอบข้อยกเว้นการตรวจสอบความปลอดภัยการรักษาความปลอดภัยในฤดูใบไม้ผลิ* @author lihaoyang* @date 2 มีนาคม 2018* ส่วนตัวคงที่สุดท้าย Long SerialVersionUid = 1L; Public ValidateCodeException (String msg) {super (msg); -ValidateCodeFilter : ตัวกรองรหัสการตรวจสอบ
ตรรกะ: การสืบทอดครั้งเดียวกับ PERPERREQUESTFILTER ทำให้มั่นใจได้ว่าตัวกรองจะถูกเรียกใช้เพียงครั้งเดียวในแต่ละครั้ง (ไม่แน่ใจว่าทำไม) ฉีดเข้าไปในโปรเซสเซอร์ที่ผ่านการตรวจสอบความถูกต้องและเรียกว่าเมื่อการตรวจสอบล้มเหลว
แพ็คเกจ com.imooc.security.core.validate.code; นำเข้า java.io.ioexception; นำเข้า Javax.servlet.filterchain; นำเข้า Javax.servlet.servletexception; นำเข้า javax.servlet.http.httpservletRequest; org.apache.commons.lang.stringutils; นำเข้า org.springframework.security.web.authentication.authenticationfailurehandler; นำเข้า org.springframework.social.connect.web.httsionsessionstions org.springframework.social.connect.web.sessionstrategy; นำเข้า org.springframework.web.bind.servletrequestbindingexception; นำเข้า org.springframework.web.bind.servletrequestutils; org.springframework.web.context.request.servletwebrequest; นำเข้า org.springframework.web.filter.onceperrequestfilter;/** * ประมวลผลตัวกรองการตรวจสอบการเข้าสู่ระบบ เวลา* @author lihaoyang* @date 2 มีนาคม 2018*/คลาสสาธารณะ ValidateCodeFilter ขยายเวลาหนึ่งครั้ง perrequestFilter {// การอนุญาตล้มเหลวโปรเซสเซอร์ส่วนตัว AuthenticationFailureHandler AuthenticationFailureHandler; // รับเครื่องมือเซสชันคลาสเซสชัน PrivateTrateGy SessionSTRATEGY = ใหม่ httpsessessessionStrategy (); @Override ป้องกันโมฆะ dofilterInternal (คำขอ httpservletrequest, httpservletResponse, filterchain filterchain) โยน servletexception, ioexception {// ถ้าเป็นคำขอเข้าสู่ระบบ && stringUtils.equalsignorecase (request.getMethod (), "post")) {ลอง {ตรวจสอบ (ใหม่ servletwebrequest (คำขอ)); } catch (validateCodeException e) {// เรียกตัวจัดการข้อผิดพลาดและในที่สุดเรียก AuthenticationFailureHandler.onauthenticationFailure (คำขอ, การตอบสนอง, e); return; // สิ้นสุดวิธีการไม่เรียกห่วงโซ่ตัวกรองอีกต่อไป}} // ไม่ใช่คำขอเข้าสู่ระบบโทรไปที่ตัวกรองตัวกรองตัวกรองอื่น ๆ dofilter (คำขอ, การตอบกลับ); } /** * รหัสการตรวจสอบการตรวจสอบ * @description: การตรวจสอบรหัสการตรวจสอบ * @param @param Request * @param @throws servletrequestbindingexception * @return เป็นโมฆะ * @throws ValidateCodeException * @author lihaoyang * @date 2 มีนาคม 2018 * ออกวัตถุ imageCode ในเซสชัน imageCode imageCodeInsession = (imageCode) SessionStrategy.getAttribute (คำขอ, ValidateCodecontroller.session_key); // นำรหัสการตรวจสอบในสตริงคำขอ imageCodeInrequest = servletRequestutils.getStringParameter (request.getRequest (), "imageCode"); // ตรวจสอบว่า (stringutils.isblank (imageCodeInrequest)) {โยน validateCodeException ใหม่ ("รหัสการตรวจสอบไม่สามารถว่างเปล่า"); } if (imageCodeInsession == null) {โยน validateCodeException ใหม่ ("รหัสการตรวจสอบไม่มีอยู่โปรดรีเฟรชรหัสการตรวจสอบ"); } if (imageCodeInsession.isexpired ()) {// ลบรหัสการตรวจสอบที่หมดอายุออกจากเซสชัน SessionStrategy.RemoveAttribute (คำขอ, validateCodecontroller.session_key); โยน ValidateCodeException ใหม่ ("รหัสการตรวจสอบหมดอายุแล้วโปรดรีเฟรชรหัสการตรวจสอบ"); } if (! stringUtils.equalsignorecase (imageCodeInsession.getCode (), imageCodeInRequest)) {โยน validateCodeException ใหม่ ("ข้อผิดพลาดรหัสยืนยัน"); } // การตรวจสอบถูกส่งออกให้ลบรหัสการตรวจสอบในเซสชัน SessionStrategy.RemoveAttribute (คำขอ, ValidateCodecontroller.session_key); } AuthenticationFailureHandler GetAuthenticationFailureHandler () {return AuthenticationFailureHandler; } โมฆะสาธารณะ setauthenticationFailureHandler (AuthenticationFailureHandler AuthenticationFailureHandler) {this.authenticationFailureHandler = AuthenticationFailureHandler; -ValidateCodecontroller : สร้างการควบคุมรหัสการตรวจสอบ
แพ็คเกจ com.imooc.security.core.validate.code; นำเข้า java.io.ioexception; นำเข้า Javax.imageio.imageio; นำเข้า javax.servlet.http.httpservletrequest; นำเข้า Javax.servlet.http.httpservletresponse; org.springframework.social.connect.web.httpsionsessionsTrategy; นำเข้า org.springframework.social.connect.web.sessionstrategy; นำเข้า org.springframework.web.bind.annotation.getmapping; org.springframework.web.bind.annotation.restcontroller; นำเข้า org.springframework.web.context.request.servletwebrequest;/** * การตรวจสอบ codecontrol * classname: validateController * @description 2018*/@restControllerPublic คลาส validateCodeController {สตริงสุดท้ายคงที่ session_key = "session_key_image_code"; // รับเซสชันเซสชันเซสชัน SessionsTrategy = ใหม่ httpsessionsessionsTrategy (); @getMapping ("/VerifyCode/image") โมฆะสาธารณะ createCode (คำขอ httpservletRequest, การตอบสนอง httpservletResponse) พ่น IOException {imageCode imageCode = createimageCode (คำขอ, การตอบสนอง); SessionsTrategy.setAttribute (ServletWebrequest ใหม่ (คำขอ), session_key, imageCode); imageio.write (imageCode.getImage (), "jpeg", response.getOutputStream ()); } private imageCode createimageCode (คำขอ httpservletRequest, การตอบสนอง httpservletResponse) {VerifyCode VerifyCode = new VerifyCode (); ส่งคืน ImageCode ใหม่ (verifyCode.getImage (), verifyCode.getText (), 60); -การกำหนดค่าตัวกรองใน BrowsersecurityConfig :
แพ็คเกจ com.imooc.security.browser; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.context.annotation.bean; นำเข้า org.springframework.context.annotation.configuration; org.springframework.security.config.annotation.web.configuration.websecurityconfigureradapter นำเข้า org.springframework.security.crypt.bcrypt.bcryptpassworder; org.springframework.security.web.authentication.authenticationfailurehandler; นำเข้า org.springframework.security.web.authentication.authenticationfailurehandler; นำเข้า org.springframework.security.web.authentication com.imooc.security.core.properties.securityproperties; นำเข้า com.imooc.security.core.validate.code.validateCodeFilter; @configuration // นี่คือการกำหนดค่าระดับความปลอดภัย SecurityProperties; // โปรเซสเซอร์ที่กำหนดเองหลังจากเข้าสู่ระบบที่ประสบความสำเร็จ @autowired การรับรองความถูกต้องส่วนตัว // การรับรองความถูกต้องที่กำหนดเองโปรเซสเซอร์ล้มเหลว @autowired AuthenticationFailureHandler ImoocauthenticationFailureHandler; // โปรดทราบว่ามันคือ org.springframework.security.crypto.password.passwordencoder @bean รหัสผ่านสาธารณะรหัสผ่านสาธารณะ () {// bcryptpasswordencoder ใช้รหัสผ่านคืน BCRYPTPASSWORDENCODER ใหม่ (); } // เวอร์ชัน 2: หน้าเข้าสู่ระบบที่กำหนดค่าได้ @Override Void Proftected Void Configure (httpsecurity http) โยนข้อยกเว้น {// validateCodeFilter ValidateCodeFilter ValidateCodeFilter = ใหม่ ValidateCodeFilter (); // ใช้การจัดการข้อผิดพลาดของคุณเองในตัวกรองรหัสการยืนยันไปยัง ValidateCodeFilter.setAuthenticationFailureHandler (ImoocauthenticationFailureHandler); // ใช้อินเทอร์เฟซที่ต้องการการรับรองความถูกต้องเพื่อกระโดดลงชื่อเข้าใช้แบบฟอร์ม, ความปลอดภัย = การรับรองความถูกต้อง + การอนุญาต //http.httpbasic () // นี่คือการรับรองความถูกต้องของกล่องป๊อปอัพเริ่มต้น // http.addfilterbefore (validateCodefilter // Form Authentication.loginPage ("/การตรวจสอบสิทธิ์/ต้องการ") // กระบวนการ Authentication BrowsersecurityConiturontroller // เข้าสู่ระบบในตัวกรองผู้ใช้ USERNAMEPASSWORDAUTHENTICATIONFILTER URL การเข้าสู่ระบบเริ่มต้นคือ "/เข้าสู่ระบบ" ซึ่งสามารถเปลี่ยนแปลงได้ที่นี่ .SuccessHandler (Imoocauthenticationsuccesshandler) // การรับรองความถูกต้องที่กำหนดเอง postprocessor.failurehandler (ImoocauthenticationFailureHandler) // การประมวลผลหลังจากการเข้าสู่ระบบและ (). SecurityProperties.getBrowser (). getLoginPage (): หน้าเข้าสู่ระบบที่ผู้ใช้กำหนดค่ากำหนด ("/การตรวจสอบ/ต้องการ", SecurityProperties.getBrowser (). getLoginPage (), // บันทึกหน้าเข้าสู่ระบบ แต่ไม่ใช่ตัวกรอง request.authenticated () // การตรวจสอบตัวตนเป็นสิ่งจำเป็นและ () .csrf (). disable () // การป้องกัน crypto ถูกปิด; -หน้าเข้าสู่ระบบ: หน้าเข้าสู่ระบบค่อนข้างประมาณ ในความเป็นจริงรหัสการตรวจสอบสามารถตรวจสอบได้เมื่ออินพุตรหัสการตรวจสอบสูญเสียโฟกัส นอกจากนี้คุณยังสามารถใช้ภาพคลิกเพื่อรีเฟรชฟังก์ชั่นการตรวจสอบรหัสดังนั้นฉันจะไม่ทำที่นี่
<body> หน้าการเข้าสู่ระบบสาธิต <br> <form action = "/Authentication/Form" method = "post"> <table> <tr> <td> ชื่อผู้ใช้: </td> <td> <อินพุต type = "text" name = "username"/> </td> <td> </td> </tr> <td> <td> <td> </td> </tr> <tr> <td> รหัสการตรวจสอบ: </td> <td> <อินพุต type = "text" name = "imageCode"/> </td> <td> <img src = "/verifycode/image"/> </td> type = "ส่ง"> เข้าสู่ระบบ </button> </td> </tr> </table> </form> </body>
เยี่ยมชม http: // localhost: 8080/demo-login.html:
ตอบกลับข้อมูลข้อยกเว้นที่กำหนดเอง
ฟังก์ชั่นทั่วไปไม่มีปัญหา แต่มันไม่เป็นสากลพอเช่นความกว้างและความสูงของรูปภาพรหัสการตรวจสอบเวลาหมดอายุ URL ที่กรองและตรรกะการตรวจสอบรหัสล้วนถูกเขียนถึงความตาย สิ่งเหล่านี้สามารถทำให้มีชีวิตอยู่และตอนนี้ประโยชน์ของการสร้างรหัสการตรวจสอบลงในตัวกรองจะสะท้อนให้เห็น เราสามารถกำหนดค่า URL ที่ต้องกรอง บางครั้งอาจไม่เพียง แต่ต้องใช้รหัสการตรวจสอบในหน้าเข้าสู่ระบบซึ่งเป็นเรื่องทั่วไปมากขึ้น
1. พารามิเตอร์พื้นฐานของรหัสการตรวจสอบสำหรับการปรับเปลี่ยนทั่วไปสามารถจับคู่ได้
มันถูกกำหนดให้เป็นแบบกำหนดค่าและแอปพลิเคชันหมายถึงโมดูลและกำหนดค่าเอง หากไม่ได้กำหนดค่าให้ใช้การกำหนดค่าเริ่มต้น นอกจากนี้การกำหนดค่าสามารถประกาศใน URL คำขอหรือในแอปพลิเคชัน ครูเป็นครูจริงและรหัสมีความหลากหลายมาก!
เอฟเฟกต์ที่ฉันต้องการให้บรรลุคือการกำหนดค่าในแอปพลิเคชันดังกล่าว properties:
#VERIFIENT CODE ความกว้างของภาพความสูง, จำนวนอักขระ imOOC.Security.Code.Image.width = 100imOoc.security.code.image.height = 30imooc.security.code.image.length = 6
จากนั้นคุณสามารถควบคุมผลกระทบของรหัสการตรวจสอบ เนื่องจากรหัสการตรวจสอบนั้นแบ่งออกเป็นรหัสการตรวจสอบภาพและรหัสการตรวจสอบ SMS คุณสามารถใช้ระดับ. code.image ซึ่งใช้ไฟล์การกำหนดค่าที่กำหนดเองของ Springboot และจำเป็นต้องประกาศคลาส Java ที่เกี่ยวข้อง:
แอตทริบิวต์รหัสจะต้องประกาศใน SecurityProperties:
แพ็คเกจ com.imooc.security.core.properties; นำเข้า org.springframework.boot.context.properties.configurationProperties; นำเข้า org.springframework.context.annotation.configuration; imooc.security ใน application.properties * * imooc.security.browser.loginpage = /demo-login.html * การกำหนดค่าเบราว์เซอร์จะถูกอ่านลงในเบราว์เซอร์โปรเจอร์ 2018*/@configurationProperties (prefix = "iMOOC.Security") ระดับความปลอดภัยระดับสาธารณะ {ส่วนตัวเบราว์เซอร์เบราว์เซอร์ = ใหม่เบราว์เซอร์โปรเปอรี่ (); รหัสส่วนตัว ValidateCodeProperties = ใหม่ ValidateCodeProperties (); BrowserProperties สาธารณะ getBrowser () {return browser; } โมฆะสาธารณะ setBrowser (เบราว์เซอร์เบราว์เซอร์) {this.browser = เบราว์เซอร์; } Public ValidateCodeProperties getCode () {รหัสส่งคืน; } โมฆะสาธารณะ setCode (รหัส ValidateCodeProperties) {this.code = code; -ValidateCodeProperties:
แพ็คเกจ com.imooc.security.core.properties;/*** การกำหนดค่ารหัสการตรวจสอบ* classname: validateCodeproperties* @description: การกำหนดค่ารหัสการตรวจสอบรหัสการตรวจสอบรวมรหัสการตรวจสอบภาพ, รหัสการตรวจสอบ SMS ฯลฯ ImageCodeProperties image = imageCodeProperties ใหม่ (); Public ImageCodeProperties getImage () {return image; } โมฆะสาธารณะ setImage (ImageCodeProperties Image) {this.image = image; -ImageCodeProperties:
แพ็คเกจ com.imooc.security.core.properties;/*** คลาสการตรวจสอบภาพการกำหนดค่ารหัสคลาส* classname: imageCodeProperties* @description: การกำหนดค่ารหัสการตรวจสอบภาพคลาสการกำหนดค่า* @author lihaoyang* @date 2 มีนาคม 2018*/คลาสสาธารณะ imagecodeproperties // ความสูงของภาพความสูง int ส่วนตัว = 23; // จำนวนอักขระการตรวจสอบรหัสส่วนตัวความยาว int ส่วนตัว = 4; // เวลาหมดอายุส่วนตัว int expirein = 60; public int getWidth () {ความกว้างกลับ; } โมฆะสาธารณะ setWidth (ความกว้าง int) {this.width = ความกว้าง; } public int getheight () {return height; } โมฆะสาธารณะ setheight (ความสูง int) {this.height = ความสูง; } public int getLength () {ความยาวคืน; } โมฆะสาธารณะ setLength (ความยาว int) {this.length = ความยาว; } public int getExpirein () {return expirein; } โมฆะสาธารณะ setexpirein (int expirein) {this.expirein = expirein; -สำหรับการกำหนดค่าระดับคำขอหากคำขอมีพารามิเตอร์รหัสยืนยันให้ใช้คำขอ:
วิธีการ createimageCode ของ ValidateCodecontroller ใช้เพื่อควบคุมเพื่อตรวจสอบว่าพารามิเตอร์คำขอมีพารามิเตอร์เหล่านี้หรือไม่ ถ้าเป็นเช่นนั้นมันจะถูกส่งผ่านไปยังรหัสการสร้างรหัสการตรวจสอบ VerifyCode ซึ่งสามารถควบคุมได้แบบไดนามิกในระหว่างการสร้าง
imageCode ส่วนตัว createimageCode (คำขอ httpservletrequest, การตอบสนอง httpservletResponse) {// ก่อนอ่านความยาวความกว้างและพารามิเตอร์หมายเลขของตัวละครจากคำขอถ้ามีให้ใช้และความกว้างเริ่มต้น = servletRequestutils int height = servletRequestutils.getIntinparameter (คำขอ "ความสูง", SecurityProperties.getCode (). getImage (). getheight ()); int charlength = this.securityProperties.getCode (). getImage (). getLength (); VerifyCode VerifyCode = NEW VERIFYCODE (ความกว้าง, ความสูง, charlength); ส่งคืน ImageCode ใหม่ (verifyCode.GetImage (), verifyCode.getText (), this.securityProperties.getCode (). getImage (). getExpirein ()); -VerifyCode:
Public VerifyCode (int w, int h, int charlength) {super (); this.w = w; this.h = h; this.charlength = charlength; -การทดลอง: การกำหนดค่าระดับแอปพลิเคชันในโครงการสาธิต
เข้าสู่ระบบในแบบฟอร์มเพื่อสร้างการกำหนดค่าระดับคำขอ
<img src = "/verifyCode/image? width = 200"/>
เข้าถึง:
ความยาวคือพารามิเตอร์ 200 ของแถบเวทีที่ร้องขอความสูงคือ 30 และอักขระได้รับการกำหนดค่า 6 ตัว
2. สามารถกำหนดค่าอินเตอร์เฟสสำหรับการตรวจสอบรหัสการปรับเปลี่ยนสากลได้
เอฟเฟกต์แรกคือการกำหนดค่าอินเทอร์เฟซที่ต้องดักจับใน application.properties แบบไดนามิกแบบไดนามิก:
ImageCodeProperties ได้เพิ่มแอตทริบิวต์ใหม่: private String url ; // URL ที่สกัดกั้นคือการจับคู่การกำหนดค่าของภาพด้านบน
หลักต้องแก้ไขตัวกรองรหัสการตรวจสอบ:
1. ประกาศชุดที่ตั้งไว้ในตัวดักเพื่อจัดเก็บ URL ที่กำหนดค่าไว้ในไฟล์กำหนดค่าที่ต้องสกัดกั้น
2. ใช้อินเทอร์เฟซ InitializingBean วัตถุประสงค์: เมื่อมีการประกอบพารามิเตอร์อื่น ๆ ทั้งหมดให้เริ่มต้นค่าของ URL ที่จำเป็นต้องสกัดกั้นและเขียนวิธี Afterpropertiesset เพื่อนำไปใช้
3. Inject SecurityProperties และอ่านไฟล์การกำหนดค่า
4. อินสแตนซ์คลาสเครื่องมือ Antpathmatcher ซึ่งเป็นตัวจับคู่
5. ตั้งค่า BrowsersecurityConfig ในโครงการเบราว์เซอร์เพื่อเรียกใช้วิธี AfterPropertIesset
6. กำหนดค่า URL ที่จะกรองในแอปพลิเคชันหน้าของโครงการสาธิตที่อ้างอิงโมดูล
ValidateCodeFilter:
/** * ประมวลผลตัวกรองรหัสการตรวจสอบการเข้าสู่ระบบ * className: validateCodeFilter * @description: * สืบทอดเครื่องมือที่จัดทำโดยครั้งหนึ่งเคยเป็นไปได้: ฤดูใบไม้ผลิเพื่อให้แน่ใจว่าตัวกรองจะถูกเรียกเพียงครั้งเดียว 2018*/คลาสสาธารณะ ValidateCodeFilter ขยายเวลาหนึ่งครั้งใน Filter ใช้การเริ่มต้นเบียน {// การตรวจสอบความถูกต้องของโปรเซสเซอร์การรับรองความถูกต้องส่วนตัว // รับเครื่องมือเซสชันคลาสเซสชัน PrivateTrateGy SessionSTRATEGY = ใหม่ httpsessessessionStrategy (); // คอลเลกชัน URL ที่ต้องสกัดกั้นชุดส่วนตัว <String> urls = new hashset <> (); // อ่านการกำหนดค่าส่วนตัวความปลอดภัย PROPERTIES SecurityProperties; // เครื่องมือสปริงคลาส Antpathmatcher Private Antpathmatcher = New Antpathmatcher (); @Override โมฆะสาธารณะ AfterPropertIesset () พ่น servletexception {super.afterpropertiesset (); // อ่าน urls ของสตริงที่ถูกกำหนดค่าที่กำหนดไว้ [] configurls = stringutils.splitbywholesparatorpreservealltokens (SecurityProperties.getCode (). getImage (). getUrl (), ","); สำหรับ (สตริง configurl: configurls) {urls.add (configurl); } // เข้าสู่ระบบคำขอจะต้องสกัดกั้น urls.add ("/การรับรองความถูกต้อง/แบบฟอร์ม"); } @Override void dofilterInternal (HTTPSERVLETREQUEST Request, HTTPSERVLETRESPONSE RESPONSIDE, FILTERCHAIN FILTERCHAIN) โยน ServleTexception, iOexception { / *** การตรวจสอบรหัสการตรวจสอบที่กำหนดค่าได้หรือไม่ สำหรับ (string url: urls) {ถ้า (antpathmatcher.match (url, request.getRequesturi ())) {action = true; }} if (action) {ลอง {ตรวจสอบความถูกต้อง (ใหม่ servletWebRequest (คำขอ)); } catch (validateCodeException e) {// เรียกตัวจัดการข้อผิดพลาดและในที่สุดเรียก AuthenticationFailureHandler.onauthenticationFailure (คำขอ, การตอบสนอง, e); return; // สิ้นสุดวิธีการไม่เรียกห่วงโซ่ตัวกรองอีกต่อไป}} // ไม่ใช่คำขอเข้าสู่ระบบ, โทรหาตัวกรองตัวกรองอื่น ๆ FilterChain.dofilter (คำขอ, การตอบกลับ); } // การละเว้นรหัสที่ไม่เกี่ยวข้อง,}BrowsersecurityConfig:
กำหนดค่า URL:
#VERIFIENT CODE Intercept Interface Configuration IMOOC.Security.Code.Image.url =/ผู้ใช้,/ผู้ใช้/*
ทดสอบ: /ผู้ใช้ /ผู้ใช้ /1 ถูกสกัดกั้น
เยี่ยมชมหน้าเข้าสู่ระบบโดยไม่ต้องเขียนรหัสยืนยัน:
สอดคล้องกับความคาดหวัง ณ จุดนี้การกำหนดค่าแบบไดนามิกของอินเทอร์เฟซการสกัดกั้นจะเสร็จสมบูรณ์
3. ตรรกะการสร้างรหัสการตรวจสอบสามารถกำหนดค่าได้
โดยทั่วไปโปรแกรมที่เขียนได้ดีกว่าจะมีอินเทอร์เฟซแบบเปิดช่วยให้ผู้ใช้สามารถปรับแต่งการใช้งานได้ หากพวกเขาไม่ได้ใช้งานให้ใช้การใช้งานเริ่มต้น มาทำสิ่งนี้กันเพื่อให้สามารถใช้รหัสการตรวจสอบได้ด้วยตนเอง หากคุณต้องการสร้างตรรกะการสร้างรหัสการตรวจสอบที่กำหนดค่าได้คุณไม่สามารถเขียนคลาสของตัวสร้างรหัสการยืนยันภาพได้ คุณต้องแยกการสร้างรหัสการตรวจสอบลงในอินเตอร์เฟส ValidateCodeGenerator ซึ่งเป็นวิธีการสร้างตัวสร้างรหัสการตรวจสอบ () เนื่องจากรหัสการตรวจสอบยังมีรหัสการตรวจสอบรูปภาพรหัสการตรวจสอบ SMS ฯลฯ เราจึงทำการใช้งานเริ่มต้นในโมดูลการตรวจสอบของเราเช่นการใช้รหัสการตรวจสอบภาพ ImageCodeGenerator ใน ImageCodeGenerator เราไม่ได้เพิ่มคำอธิบายประกอบ @component ลงในคลาสนี้ จากนั้นใช้คลาสการกำหนดค่า ValidateCodeBeanConfig ที่เขียนถั่วการตรวจสอบ คลาสการกำหนดค่านี้กำหนดค่าการตรวจสอบรหัสการตรวจสอบที่จำเป็นต่างๆเช่นการใช้รหัสการตรวจสอบภาพการใช้งาน ImagecodeGenerator, รหัสการตรวจสอบ SMS ฯลฯ ประเภทการส่งคืนของพวกเขาคือ ValidateCodeGenerator ทั้งหมด ใช้ @ConditionAlonMissingBean (name = "imageCodeGenerator") คำอธิบายประกอบ สามารถตัดสินได้ว่าหากคอนเทนเนอร์สปริงปัจจุบันมีถั่วที่ชื่อ imageCodeGenerator มันจะถูกใช้และหากไม่มีการกำหนดค่าดังนั้นหากคนอื่นอ้างถึงโมดูลของคุณหากคนอื่นใช้รหัสการตรวจสอบเพื่อสร้างส่วนต่อประสานที่ถูกต้อง
รหัสหลัก:
อินเตอร์เฟส Code Generator ValidateCodeGenerator:
แพ็คเกจ com.imooc.security.core.validate.code; นำเข้า org.springframework.web.context.request.servletwebrequest;/** * อินเทอร์เฟซการสร้างรหัสการตรวจสอบ * classname: validateCodeGenerator * @deccription / ** * อินเทอร์เฟซการสร้างรหัสการตรวจสอบรูปภาพ * @description: todo * @param @param Request * @param @return * @return imageCode * @throws * @author lihaoyang * @date 2 มีนาคม 2018 * / imageCode
ตัวสร้างรหัสการตรวจสอบภาพใช้ imageCodeGenerator:
แพ็คเกจ com.imooc.security.core.validate.code; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.stereotype.Component; นำเข้า org.springframework. org.springframework.web.context.request.servletwebrequest; นำเข้า com.imooc.security.core.properties.SecurityProperties;/** * การสร้างรหัสการสร้างภาพ ValidateCodeGenerator {@autowired Private SecurityProperties SecurityProperties; @Override Public ImageCode Generator (คำขอ ServletWebRequest) {// อ่านก่อนจากคำขอก่อนว่ามีความยาวความกว้างและพารามิเตอร์อักขระใด ๆ หากมีให้ใช้และความกว้าง int เริ่มต้น = servletRequestutils.getIntIntParameter (request.getRequest (), "ความกว้าง", SecurityProperties.getCode (). getImage (). getWidth ()); ความสูง int = servletRequestutils.getIntParameter (request.getRequest (), "ความสูง", SecurityProperties.getCode (). getImage (). getheight ()); int charlength = this.securityProperties.getCode (). getImage (). getLength (); VerifyCode VerifyCode = NEW VERIFYCODE (ความกว้าง, ความสูง, charlength); ส่งคืน ImageCode ใหม่ (verifyCode.GetImage (), verifyCode.getText (), this.securityProperties.getCode (). getImage (). getExpirein ()); } Public SecurityProperties GetSecurityProperties () {return SecurityProperties; } โมฆะสาธารณะ setSecurityProperties (SecurityProperties SecurityProperties) {this.securityProperties = ความปลอดภัย -ValidateCodeBeanConfig:
แพ็คเกจ com.imooc.security.core.validate.code; นำเข้า org.springframework.beans.factory.annotation.autowired; นำเข้า org.springframework.boot.autoconfigure.condition.condition.condition.condition.condition.condition.condition.condition.conditition org.springframework.context.annotation.bean; นำเข้า org.springframework.context.annotation.configuration; นำเข้า com.imooc.security.core.properties.SecurityProperties;/** @Description: * กำหนดค่าถั่วของคลาสการใช้งานจริงของ ValidateCodeGenerator * เช่นการใช้รหัสการตรวจสอบภาพและการใช้รหัสการตรวจสอบ SMS * @author lihaoyang * @date 5 มีนาคม 2018 /** * @description: * @ConditionAlonMissingBean คำอธิบายประกอบหมายความว่าเมื่อคอนเทนเนอร์สปริงไม่มี imageCodeGenerator ฟังก์ชั่นคือการทำให้โปรแกรมขยายได้มากขึ้น คลาสการกำหนดค่าถูกกำหนดค่าในโมดูลหลัก ซึ่งหมายความว่าหากโครงการที่อ้างถึงโมดูล * มีการใช้งานของตัวเองซึ่งใช้อินเทอร์เฟซ ValidateCodeGenerator และกำหนดการนำไปใช้งานของตัวเอง ชื่อนี้เรียกว่า imageCodeGenerator * ใช้เพื่อใช้การใช้งานระดับแอปพลิเคชันและหากไม่มีการใช้งานดังกล่าวให้ใช้การใช้งานเริ่มต้นนี้ * @param @return * @return validateCodeGenerator * @throws * @author lihaoyang * @date 5 มีนาคม 2018 */ @bean @conditionalonmissingbean (name = "imageCodeNerator") CodeGenerator.SetSecurityProperties (SecurityProperties); ส่งคืน codegenerator; -
ด้วยวิธีนี้หากโมดูลหมายถึงโมดูลรหัสการตรวจสอบนี้จะปรับแต่งการใช้งานเช่น:
แพ็คเกจ com.imooc.code; นำเข้า org.springframework.stereotype.component; นำเข้า org.springframework.web.context.request.servletwebrequest; นำเข้า com.imooc.security.core.validate.code.imagecode; com.imooc.security.core.validate.code.validateCodeGenerator; @component ("imageCodeGenerator") DemoimageCodeNerator ระดับสาธารณะ การดำเนินการ, "); คืนค่า null; -ด้วยวิธีนี้เมื่อ ValidateCodeBeanConfig กำหนดค่า Bean การตรวจสอบรหัสจะใช้การใช้งานที่กำหนดเองของผู้ใช้
รหัสที่สมบูรณ์ถูกวางไว้ใน gitHub: https://github.com/lhy1234/spring-security
สรุป
ด้านบนเป็นรหัสตัวอย่างของฟังก์ชั่นรหัสการตรวจสอบภาพความปลอดภัยของสปริงที่แนะนำโดยตัวแก้ไข ฉันหวังว่ามันจะเป็นประโยชน์กับคุณ หากคุณมีคำถามใด ๆ โปรดฝากข้อความถึงฉันและบรรณาธิการจะตอบกลับคุณทันเวลา ขอบคุณมากสำหรับการสนับสนุนเว็บไซต์ Wulin.com!