Cet article partage le code spécifique du code de vérification KAPTCHA intégré à Springboot pour votre référence. Le contenu spécifique est le suivant
1. Introduction liée à Kaptcha
Kaptcha est un projet open source de code de vérification basé sur SimplecaptCHA.
2. Solution d'intégration
① Dépendances de configuration dans pom.xml
<! - CODE DE VÉRIFICATION -> <Dependance> <ProupId> com.github.peggle </romp grouped> <Artifactid> kaptcha </retifactid> <version> 2.3.2 </ version> </Dependency>
② Code de vérification Configure Paramètres liés à Kaptcha
@Configurationpublic classe kaptchaconfig {@bean (name = "capchaproducer") public defaultKaptcha getKaptCHABEAN () {defaultKaptCha defaultKaptCha = new defaultKaptcha (); Propriétés Properties = New Properties (); Properties.SetProperty ("kaptcha.border", "oui"); Properties.SetProperty ("kaptcha.border.color", "105,179,90"); Properties.SetProperty ("kaptcha.textproducer.font.color", "bleu"); 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 = new Config (Properties); defaultKaptCha.setConfig (config); return defaultKaptCha; }}ou
Créer un fichier mykaptcher.xml sous ressources
<? xml version = "1.0" encoding = "utf-8"?> <beans xmlns = "http://www.springframework.org/schema/beans" xmlns: xsi = "http://www.w3.org/2001/xmlschema-instance" xsi: schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = "CaptChaproduer"> <propriété name = "config"> <ean> type = "java.util.properties"> <popps> <prop key = "kaptcha.border"> oui </prop> <prop key = "kaptcha.border.color"> 105,179,90 </prop> <prop Key = "kaptcha.textProducer.font.color"> bleu </prop> <pcha key = "kaptcha.image.width"> 100 </prop> <prop key = "kaptcha.image.height"> 50 </ prop> <prop Key = "kaptcha.image.height"> 50 </ prop> <prop. key = "kaptcha.textproducer.font.color"> bleu </prop> <prop key = "kaptcha.image.width"> 100 </prop> <prop key = "kaptcha.image.height"> 50 </ prop> <p key = "kaptcha.textproducer.font.size"> 27 </pp> <prop> key = "kaptcha.session.key"> code </ prop> <prop key = "kaptcha.textproducer.char.length"> 4 </prop> <prop key = "kaptcha.textproduner.font.names"> Font, kai font, Microsoft Yahei </prop> <project key = "kaptcha.textproducer.char.string"> 23456789abcefghjkmnopqrstuvwxyz </prop> <prop key = "kaptcha.obscurificator.impl"> com.google.code.kaptcha.impl.waterripple </ proP> <prop> key = "kaptcha.noise.color"> Black </Prop> <prop> Key = " <prop key = "kaptcha.noise.impl"> com.google.code.kaptcha.impl.nonoise </ prop> <! - <prop key = "kaptcha.noise.impl"> com.google.code.kaptcha.impl.defaultnoise </pr> -> <prop key = "kaptcha.background.clear.from"> 185,56,213 </prop> <prop key = "kaptcha.background.clear.to"> white </ prop> <prop.
Puis chargez la configuration dans l'application de classe de démarrage
@ ENABLETRANSACTIONMANGAGE // Démarrer la gestion des transactions d'annotation, ce qui équivaut à la méthode de configuration XML <TX: Annotation-Driven /> @ SpringbootApplication @ ActivedCheDuling // Démarrer la tâche de synchronisation @Mapperscan (BasEpackages = "com.shawn.mapper") @ ImportResource (Locations = {"classpath: mykaptcha.xml"}) La classe publique de la classe étend SpringbootServletInitializer {public static void main (String [] args) lance une exception {SpringApplication.Run (application.class, args); }}Les deux méthodes de configuration sont disponibles à Springboot;
③KaptChacontroller
@ Commonslog @ ControllerPublic Classe KaptChacontroller étend BaseController {@autowired producteur privé CAPTCHAPRODUCER; @GetMapping ("/ GetKaptChaimage") public void getKaptChaimage () lève une exception {réponse.setDateHeader ("expire", 0); // Définissez des en-têtes standard HTTP / 1.1 sans cache. Response.sethEader ("Cache-Control", "sans magasin, sans cache, doit-revalider"); // Définir IE EXPELT HTTP / 1.1 En-têtes sans cache (utilisez AddHeader). Response.Addheader ("Cache-Control", "post-Check = 0, pré-Check = 0"); // Définissez l'en-tête standard HTTP / 1.0 sans cache. Response.sethEader ("Pragma", "No-Cache"); // Renvoie une réponse jpeg.setContentType ("image / jpeg"); // Créez le texte pour la chaîne d'image capText = CAPTCHAPRODUCER.CREATETEXT (); // Stockez le texte dans la session //request.getSession().SetAttribute(Constants.kaptcha_session_key, CapText); // Enregistrez le code de vérification sur session session.setAttribute (constants.kaptcha_session_key, captext); Log.info (CapText); // Créez l'image avec le texte BufferedImage BI = CAPTCHAPRODUCER.CREATEIMAGE (CapText); ServletOutputStream out = Response.getOutputStream (); // Écrivez les données ImageIo.Write (BI, "JPG", out); essayez {out.flush (); } enfin {out.close (); }}}3. Testez l'effet
Ce qui précède est tout le contenu de cet article. J'espère que cela sera utile à l'apprentissage de tous et j'espère que tout le monde soutiendra davantage Wulin.com.