مقدمة:
فيما يتعلق بإدخال Kaptcha وتكامل Spring Kaptcha ، شرحت بالتفصيل في مقال آخر. يرجى الرجوع إلى: رمز التحقق من الربيع Kaptcha.
ستقدم هذه المقالة طريقتين لدمج Springboot في Kaptcha.
أدوات وتقنيات التطوير:
1. فكرة 2017
2. Springboot 2.0.2
3. كابتشا
تبدأ رسميًا:
الطريقة 1: تكوين من خلال kaptcha.xml
1. قم بإنشاء ربيع جديد initializr باستخدام الفكرة
2. إضافة تبعيات Kaptcha:
<!-رمز التحقق من kaptcha-> <reperency> <roupiD> com.github.penggle </rougeid> <StifactId> kaptcha </shintifactid> <sophy> 2.3.2 </version> </dependency>
3. قم بإنشاء kaptcha.xml جديد تحت الموارد ، مع المحتوى التالي:
kaptcha.xml
<؟ XSI: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <! <constructor-arg type = "java.util.properties"> <!-تعيين خصائص kaptcha-> <props> <prop key = "kaptcha.border"> نعم </prop> <prop key = "kaptcha.border.color"> 105،179،90 </prop> <prop> key = "kaptcha.textproducer.font.color"> Blue </prop> <prop key = "kaptcha.image.width"> 100 </prop> <prop key = "kaptcha.image.height"> 50 </sper> <propcha = "kaptcha.image.height <prop key = "kaptcha.image.height"> 50 </prop> <prop key = "kaptcha.image.height"> 50 </prop> <prop key = "kaptcha. key =" kaptcha.textProducer.font.size "> 27 </prop> key = "kaptcha.textproducer.char.length"> 4 </prop> <prop key = "kaptcha.textproducer.font.names"> Font ، Kai Font ، Microsoft Yahei </prop> <prop> <prop> <prop key = "kaptcha.textproducer.char.string"> 0123456789ABCEFGHIJKLMNOPQRSTUVWXYZ </prop> <propcha key = "kaptcha.noise.color"> أسود </prop> <prop key = "kaptcha.noise.impl"> com.google.code.kaptcha.impl.defaultnoise </prop> <prop> key = "kaptcha.back.clear.from"> 185،56،213 </prop> key = "kaptcha.background.clear.to"> الأبيض </prop> <prop key = "kaptcha.textproducer.char.space"> 3 </props> </props> </structructor-arg> </ban> </sprentive> </bean> </bans>
ملاحظة: المحتوى في kaptcha.xml هو في الواقع نفس المحتوى في spring-kaptcha.xml عندما يدمج Spring kaptcha. وهذا يعني تسليم kaptcha إلى إدارة حاويات الزنبرك ، ووضع بعض الخصائص ، ثم الحقن مباشرة عند الحاجة.
4. تحميل kaptcha.xml:
إضافة @ImportResource(locations = {"classpath:kaptcha/kaptcha.xml"}), أضف هذا التعليقات التوضيحية ، سوف يقوم Springboot بتحميل ملف kaptcha.xml. لاحظ أنه لا ينبغي كتابة مسار kaptcha.xml بشكل غير صحيح ، فإن ClassPath هو دليل الموارد هنا.
5. اكتب وحدة تحكم لإنشاء رمز التحقق:
Codecontroller.java
ControllerPublic Class Codecontroller {Autowired Private Producer CaptChaproducer = null ؛ requestmapping ("/kaptcha") public void getKaptChaImage (طلب httpservletrequest ، استجابة httpservletresponse) يلقي الاستثناء {httpsession session = request.getSession () ؛ استجابة. استجابة. reponse.addheader ("Cache-Control" ، "post-check = 0 ، pre-check = 0") ؛ استجابة. استجابة. // إنشاء Captext string code captext = captchaproducer.createText () ؛ Session.setAttribute (Constants.kaptcha_session_key ، captext) ؛ // اكتب bufferedimage إلى العميل bi = captchaproducer.createImage (captext) ؛ servleToutPutStream out = response.getOutputStream () ؛ imageio.write (Bi ، "JPG" ، Out) ؛ حاول {out.flush () ؛ } أخيرًا {out.close () ؛ }}}ملاحظة: حقن الفاصوليا التي تم تكوينها للتو في kaptcha.xml في مسار وحدة التحكم هذا ، وبعد ذلك يمكنك استخدامه لإنشاء رمز التحقق وإخراج رمز التحقق إلى العميل ؛ تذكر مسار هذه الفئة ، يحتاج SRC لرمز التحقق من الصفحة الأمامية إلى الإشارة إلى هذا المسار.
6. قم بإنشاء أداة مقارنة رمز التحقق الجديد:
CodeUtil.java
الفئة العامة CodeUtil { / ** * قم بتحويل المعلمة الأمامية التي تم الحصول عليها إلى نوع السلسلة * request * param request * param Key * @return * / public static string getTring (httpservletrequest طلب ، مفتاح السلسلة) {try {string result = request.getParameter (مفتاح) ؛ if (result! = null) {result = result.trim () ؛ } if ("". equals (result)) {result = null ؛ } نتيجة الإرجاع ؛ } catch (استثناء e) {return null ؛ }} /** * التحقق من رمز التحقق * param request * regurn * /public static boolean checkverifyCode (httpservletrequest request) {// احصل على سلسلة التحقق المولدة verifycodeexpisted = (string) request.getSession (). // احصل على رمز التحقق الذي تم إدخاله بواسطة سلسلة المستخدم VerifyCodeActual = codeUtil.getString (طلب ، "VerifyCodeActual") ؛ if (VerifyCodeActual == null ||! verifyCodeActual.equals (VerifyCodeExpected)) {return false ؛ } إعادة صواب ؛ }}ملاحظة: يتم استخدام هذه الفئة لمقارنة رمز التحقق الذي تم إنشاؤه باستخدام رمز التحقق الذي أدخله المستخدم. سيتم إضافة رمز التحقق الذي تم إنشاؤه تلقائيًا إلى الجلسة ، وسيتم الحصول على إدخال المستخدم من خلال GetParameter. لاحظ أن القيمة الرئيسية لـ GetParameter يجب أن تكون متسقة مع قيمة اسم رمز التحقق في الصفحة.
7. استخدم رمز التحقق:
controller
Helloworld.java
RestControllerPublic Class HelloWorld {REquestMapping ("/hello") سلسلة عامة Hello (httpservletrequest request) {if (! } آخر {return "Hello ، World" ؛ }}} ② صفحة
مرحبا
<! doctype html> <html lang = "en"> <head> <meta charset = "utf-8"> <title> العنوان </title> <script type = "text/javaScript"> function refresh () {document.getElementByid ('captcha_img'). src = "/kaptcha؟ } </script> </head> <body> <form action = "/hello" method = "post"> رمز التحقق: <input type = "text" placeholder = "الرجاء إدخال رمز التحقق" name = "verifyCodeActual"> <viv> <img id = "captcha_img" onClick = "sresp () src =" /> </form> </body> </html>ملاحظة: رمز التحقق هو في الأساس صورة ، لذا استخدم علامة <img> ، ثم استخدم src = "/kaptcha" للإشارة إلى مسار وحدة التحكم التي تنشئ رمز التحقق ؛ اتصل برمز JS من خلال OnClick = "Refresh ()" لتحقيق وظيفة النقر إلى التبديل ؛ انتبه إلى قيمة الاسم في <input name = "VerifyCodeActual"> ، وينبغي القيمة الرئيسية التي تم تمريرها عند الحصول على رمز التحقق الذي أدخله المستخدم من خلال طريقة طلب getParameter () في CodeUtil مع قيمة الاسم هنا.
8. الاختبار:
أدخل رمز التحقق الصحيح
لقد مرت التحقق
أدخل رمز التحقق الخاطئ
فشل التحقق
الطريقة 2: تكوين kaptcha من خلال فئة التكوين
1. تكوين kaptcha
مقارنة مع الطريقة الأولى ، يزيد واحد وينخفضان.
يقلل:
①delete kaptcha.xml ②deleteformortresource @ImportResource(locations = {"classpath:kaptcha/kaptcha.xml"})
يزيد:
① قم بإنشاء فئة تكوين KaptChaconFig جديدة ، والمحتوى كما يلي:
Kaptchaconfig.java
ConfigurationPublic Class KaptChaconfig {bean public defaultkaptcha getDefaultKaptCha () {defaultkaptcha captchaproducer = 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" ، "110") ؛ Properties.SetProperty ("kaptcha.image.height" ، "40") ؛ Properties.SetProperty ("Kaptcha.TextProducer.font.size" ، "30") ؛ 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 (خصائص) ؛ captChaproducer.setConfig (config) ؛ إرجاع captchaproducer. }}ملاحظة: يتم استخدام هذه الفئة لتكوين kaptcha ، والتي تعادل kaptcha.xml في الطريقة 1. أضف kaptcha إلى حاوية IOC ، ثم العودة إلى مثيل باستخدام خصائص المجموعة ، وأخيراً حقنها في codecontroller. يمكنك استخدامه لإنشاء رمز التحقق في Codecontroller. إيلاء اهتمام خاص للعودة captchaproducer ؛ اسم CaptChaproducer في المنتج الخاص CaptChaproducer = NULL ؛ ، وإلا فلن يتم تحميل الفول.
2. الاختبار:
أدخل رمز التحقق الصحيح:
لقد مرت التحقق
أدخل رمز التحقق الخاطئ
فشل التحقق
من أجل شرح أن رموز التحقق من المرتين يتم إنشاؤها بناءً على طريقتين ، يتم تعيين رموز التحقق للطرق الأولى والثانية سمات مختلفة. من الصورة ، يمكننا أن نرى أن الألوان وخطوط التداخل والخلفيات وما إلى ذلك. لرموز التحقق من المرتين مختلفة.
تلخيص:
1. فرز العملية:
بغض النظر عن الطريقة ، يجب عليك أولاً إضافة kaptcha إلى حاوية الربيع ، أي يجب أن يكون هناك حبة kaptcha ؛ ثم قم بإنشاء وحدة تحكم جديدة ، وحقن حبة Kaptcha في وحدة التحكم لإنشاء رموز التحقق ؛ ثم هناك فئة أدوات تقارن رمز التحقق ، واتصل بفئة الأدوات في وحدة تحكم الاختبار لمقارنة رمز التحقق ؛ أخيرًا ، في الصفحة الأمامية ، تحتاج فقط إلى استخدام <img src = "/生成验证码的controller的路由"> للحصول على رمز التحقق. عن طريق إضافة حدث نقرة إلى علامة IMG هذه ، يمكنك إدراك "انقر فوق تبديل رمز التحقق".
2. مقارنة مع تكامل الربيع Kaptcha
تقدم Spring Integration Kaptcha أيضًا طريقتين. التكوين هو أبسط في web.xml ، وليس هناك حاجة لكتابة وحدة تحكم تقوم بإنشاء رمز التحقق. في الصفحة ، استخدم SRC للإشارة إلى قيمة <Url-pattern> لـ Kaptcha's Servlet في web.xml.
كلتا الطريقتين لدمج سبرينغ بوت تشبه الطريقة الثانية لدمج Spring Kaptcha. يتم استخدامها لتكوين الفاصوليا أولاً ، ثم استخدام وحدات التحكم لإنشاء رموز التحقق. يستخدم الواجهة الأمامية SRC للإشارة إلى وحدة التحكم هذه. الفرق هو أنه إذا كانت وحدة التحكم التي تنشئ رموز التحقق هي /xxx ، ثم src = "xxx.jpg" عند دمج Springboot kaptcha ، src = "/xxx"。
ما سبق هو كل محتوى هذه المقالة. آمل أن يكون ذلك مفيدًا لتعلم الجميع وآمل أن يدعم الجميع wulin.com أكثر.