Dieser Artikel teilt den spezifischen Code von Springboot Integrated Kaptcha -Verifizierungscode für Ihre Referenz. Der spezifische Inhalt ist wie folgt
1.Kaptcha bezogene Einführung
Kaptcha ist ein Open -Source -Projekt für den Verifizierungscode basierend auf SimpleCaptcha.
2. Integrationslösung
① Konfigurationsabhängigkeiten in pom.xml
<!-Verifizierungscode-> <depelzie> <gruppe> com.github.penggle </Groupid> <artifactId> Kaptcha </artifactid> <version> 2.3.2 </Version> </abhängig>
②Configure -Verifizierungscode Kaptcha -verwandte Einstellungen
@ConfigurationPublic Class Kaptchaconfig {@Bean (name = "captchaproducer") public defaultkaptcha getkaptchabean () {defaultkaptcha defaultkaptcha = new DefaultKaptcha (); Eigenschaften Eigenschaften = neue Eigenschaften (); Properties.SetProperty ("Kaptcha.Border", "Ja"); 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 = new config (Eigenschaften); defaultKaptcha.setConfig (config); Rückgabe von defaultKaptcha; }}oder
Erstellen Sie mykaptcher.xml -Datei unter Ressourcen
<? XSI: Schemalocation = "http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <bean id = captchaproducer "> < type = "java.util.properties"> <props> <prop key = "waptcha.border"> yes </prop> <prop key = "kaptcha.border.color"> 105,179,90 </prop> <prop key = "kaptcha.textproducer key="kaptcha.image.width">100</prop> <prop key="kaptcha.image.height">50</prop> <prop key="kaptcha.image.height">50</prop> <prop key="kaptcha.image.height">50</prop> <prop key="kaptcha.image.height">50</prop> <prop KEY = "KAPTCHA.TEXTPRODUCER.FONT.COLOR"> Blue </prop> <Prop Key = "Kaptcha.image.width"> 100 </prop> <Prop Key = "Kaptcha.image.Height"> 50 </prop> <propt key = "kaptcha.textProducer key="kaptcha.session.key">code</prop> <prop key="kaptcha.textproducer.char.length">4</prop> <prop key="kaptcha.textproducer.font.names">Song font, Kai font, Microsoft Yahei</prop> <prop 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="kaptcha.noise.impl">com.google.code.kaptcha.impl.NoNoise</prop> <!--<prop key="kaptcha.noise.impl">com.google.code.kaptcha.impl.DefaultNoise</prop>--> <prop KEY = "Kaptcha.background.clear.from"> 185,56,213 </prop> <Prop Key = "Kaptcha.background.clear.to"> weiß </prop> <prop key = "Kaptcha.textproducer
Laden Sie dann die Konfiguration in die Anwendung der Startklasse
@SableTransactionManagement // Annotation-Transaktionsmanagement starten, die der XML-Konfigurationsmethode entspricht. Die Klassenanwendung erweitert SpringbootServletInitializer {public static void main (String [] args) löst eine Ausnahme aus {SpringApplication.run (application.class, args); }}Beide Konfigurationsmethoden sind im Springboot erhältlich.
③kaptchacontroller
@Commonslog @ControllerPublic Class Kaptchacontroller erweitert BaseController {@autowired private Produzent CaptchaproDucer; @GetMapping ("/getkaptchaimage") public void getKaptchaimage () löst Ausnahme aus {response.setdatheader ("läuft", 0); // Standard-HTTP/1.1-No-Cache-Header einstellen. response.setheader ("cache-control", "No-Store, No-Cache, Must-Revalidate"); // IE erweitert HTTP/1.1 No-Cache-Header (addHeader verwenden). response.addHeader ("cache-control", "post-Check = 0, pre-Check = 0"); // Standard-HTTP/1.0-No-Cache-Header festlegen. response.setheader ("pragma", "no-cache"); // eine JPEG -Antwort zurückgeben. // Erstellen Sie den Text für den Bild String captext = captchaproDucer.CreateText (); // den Text in der Sitzung speichern //request.getSession().setattribute(Constants.kaptcha_Session_Key, Captext); // den Bestätigungscode in Sitzung speichern. log.info (Captext); // Erstellen Sie das Bild mit dem Text bufferedimage bi = captchaproDucer.createimage (Captext); ServLetOutputStream out = Antwort.getOutputStream (); // Die Daten imageIO.Write (bi, "jpg", out) schreiben; probieren {out.flush (); } endlich {out.close (); }}}3. Testen Sie den Effekt
Das obige ist der gesamte Inhalt dieses Artikels. Ich hoffe, es wird für das Lernen aller hilfreich sein und ich hoffe, jeder wird Wulin.com mehr unterstützen.