تشارك هذه المقالة رمز التحقق من Kaptcha المدمج في Springboot للرجوع إليه. المحتوى المحدد كما يلي
1.Kaptcha مقدمة ذات صلة
Kaptcha هو مشروع مفتوح المصدر لرمز التحقق استنادًا إلى SimpleCaptcha.
2. حل التكامل
① تبعيات التكوين في pom.xml
<!-رمز التحقق-> <reperency> <roupiD> com.github.penggle </rougiD> <StifactId> kaptcha </stifactid> <الإصدار> 2.3.2 </version> </sependency>
② تكوين رمز التحقق من الإعدادات ذات الصلة Kaptcha
ConfigurationPublic Class KaptChaconfig {bean (name = "captChaproducer") public defaultkaptcha getKaptChabean () {defaultKaptCha defaultKaptCha = new 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" ، "code") ؛ Properties.SetProperty ("kaptcha.textProducer.char.length" ، "4") ؛ Properties.SetProperty ("Kaptcha.TextProducer.font.names" ، "Song Font ، Kai Font ، Microsoft Yahei") ؛ config config = config config (خصائص) ؛ DefaultKaptCha.setConfig (config) ؛ إرجاع DefaultKaptcha ؛ }}أو
إنشاء ملف mykaptcher.xml تحت الموارد
<؟ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "captChaproducer" type = "java.util.properties"> <bors> <prop key = "kaptcha.border"> نعم </prop> <prop key = "kaptcha.border.color"> 105،179،90 </prop> <prop key = "kaptcha.textprodurocer.font.color"> key = "kaptcha.image.width"> 100 </prop> <prop key = "kaptcha.image.height"> 50 </prop> <prop key = "kaptcha.image.height"> 50 </prop> <prop> <"kaptcha.image.height"> 50 </prop> key = "kaptcha.textproducer.font.color"> Blue </prop> <prop key = "kaptcha.image.width"> 100 </prop> <prop key = "kaptcha.image.height"> 50 </prop> <propcha = "kaptcha.textproducer.font.s key = "kaptcha.session.key" code </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.waterripple </sperple> <prop key = "kaptcha.noise.impl"> com.google.code.kaptcha.impl.nonoise </prop> <!-<prop key = "kaptcha.noise.impl"> com.google.code.kaptcha.impl.defaultnoise </prop>- key = "kaptcha.background.clear.from"> 185،56،213 </prop> <prop key = "kaptcha.background.clear.to"> white </prop> <propcha = "kaptcha.textproducer.char.space"> 3 </props> </bindructor-arg>
ثم قم بتحميل التكوين في تطبيق فئة بدء التشغيل
@enableTransActionManagement // ابدأ إدارة معاملات التعليق التوضيحي ، وهو ما يعادل طريقة تكوين XML <tx: تعليقات التوضيح/>@springbootapplication@enblescheduling // بدء توقيت التعليق التوضيحي@mapperscan (basePackages = "com.shawn.mapper")@Importresource (المواقع = {"classpath: mykaptcha.xml"}) يمتد تطبيق الطبقة العامة على springbootservletinitializer }}كلتا طرق التكوين متوفرة في Springboot ؛
③kaptchacontroller
@commonslog @controllerpublic class KaptChaconTroller يمتد BaseController {Auutowired Properger CaptChaproducer ؛ GetMapping ("/getKaptChaImage") public void getKaptChaImage () يلقي الاستثناء {reponse.setDateHeader ("Expires" ، 0) ؛ // قم بتعيين رؤوس HTTP/1.1 NO-Cache. استجابة. // قم بتعيين IE HTTP/1.1 NO-CACH HAYS (استخدم ADDHEDER). reponse.addheader ("Cache-Control" ، "post-check = 0 ، pre-check = 0") ؛ // اضبط رأس HTTP/1.0 NO-Cache. استجابة. // إرجاع jpeg response.setContentType ("Image/JPEG") ؛ // قم بإنشاء نص لسلسلة الصور captext = captChaproducer.CreateText () ؛ // قم بتخزين النص في الجلسة //request.getSession().setAttribute(constants.kaptcha_session_key ، captext) ؛ // حفظ رمز التحقق إلى الجلسة session.setattribute (Constants.KaptCha_Session_key ، captext) ؛ log.info (captext) ؛ // قم بإنشاء الصورة باستخدام Text BufferEdImage BI = captChaproducer.CreateImage (captext) ؛ servleToutPutStream out = response.getOutputStream () ؛ // اكتب البيانات Out Imageio.write (BI ، "JPG" ، Out) ؛ حاول {out.flush () ؛ } أخيرًا {out.close () ؛ }}}3. اختبار التأثير
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.