この記事では、参照用のSpringboot Integrated Kaptcha検証コードの特定のコードを共有しています。特定のコンテンツは次のとおりです
1.Kaptcha関連の紹介
Kaptchaは、SimpleCaptchaに基づいた検証コードのオープンソースプロジェクトです。
2。統合ソリューション
pom.xmlの構成依存関係
<! - 検証コード - > <依存関係> <groupId> com.github.penggle </groupid> <artifactid> kaptcha </artifactid> <バージョン> 2.3.2 </version> </dependency>
Configure検証コードKaptcha関連設定
@configurationPublic class kaptchaconfig {@bean(name = "captchaproducer")public defaultkaptcha getkaptchabean(){defaultkaptcha defaultkaptcha = new DefaultKaptcha();プロパティプロパティ= new Properties(); properties.setProperty( "kaptcha.border"、 "yes"); 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(Properties); DefaultKaptcha.setConfig(config); DefaultKaptchaを返します。 }}または
リソースの下にmykaptcher.xmlファイルを作成します
<?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 = "captchaproducer"> <propertion = <bean type = "java.util.properties"> <props> <prop key = "kaptcha.border"> yes </prop> <prop key = "kaptcha.color"> 105,179,90 </prop> <prop key = "kaptcha.textproducer.font.color"> blue 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.height"> 50 </prop> <prop key = "kaptcha.heigh key = "kaptcha.textproducer.font.color"> blue </prop> <prop key = "kaptcha.image.width"> 100 </prop> <prop key = "kaptcha.image.height"> 50 </prop> <prop key = "kaptcha.textproducer.font.size"> 27 </prop> </prop 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.wateripple </prop> <prop key = "kaptcha.noise.color"> black </prop> <prop key = "kaptcha.noise.impl"> com.google.code.kaptcha.impl.nonoise </prop> <! - <prop key = "kaptcha.noise.imp key = "kaptcha.background.clear.from"> 185,56,213 </prop> <prop key = "kaptcha.background.clear.to"> white </prop> <prop key = "kaptcha.textproducer.char.space"> 3 </prop> </props
次に、スタートアップクラスアプリケーションに構成をロードします
@enabletransactionManagement // Annotation Transaction Managementを開始します。これはXML構成方法<TX:Annotation-Driven/>@SpringBootApplication@enableScheduling // Annotation Timing Task @mapperscan(BasePackages( "com.shawn.mapper")@importresource(locations = {"classpath:mykaptcha.xml"})public class applications springbootservletinitializer {public static void main(string [] args)throws {springapplication.run(application.rass、args); }}どちらの構成方法もスプリングブートで使用できます。
③kaptchacontroller
@commonslog @ControllPublic class KaptchacontrollerはBasecontrollerを拡張します{@autowiredプライベートプロデューサーCaptchaproducer; @getMapping( "/getKaptChaimage")public void getKaptChaimage()throws Exception {respons.setDateHeader( "expires"、0); //標準のhttp/1.1ノーキャッシュヘッダーを設定します。 Response.setheader( "Cache-Control"、 "No-Store、No-Cache、必須再評価"); // IE拡張http/1.1 no-cacheヘッダー(addheaderを使用)を設定します。 Response.Addheader( "Cache-Control"、 "Post-Check = 0、Pre-Check = 0"); //標準のHTTP/1.0 No-Cacheヘッダーを設定します。 Response.setheader( "Pragma"、 "no-cache"); // jpeg response.setContentType( "Image/jpeg");を返します。 //画像文字列captext = captchaproducer.createText()のテキストを作成します。 //テキストをセッションに保存する//Request.getSession().setAttribute(CONSTANTS.KAPTCHA_SESSION_KEY、CAPTEXT); //検証コードをセッションセッションに保存します。SetAttribute(constants.kaptcha_session_key、captext); log.info(captext); //テキストで画像を作成するbufferedimage bi = captchaproducer.createimage(captext); servletoutputStream out = respons.getOutputStream(); // data out imageio.write(bi、 "jpg"、out)を書きます。 try {out.flush(); }最後に{out.close(); }}}3.効果をテストします
上記はこの記事のすべての内容です。みんなの学習に役立つことを願っています。誰もがwulin.comをもっとサポートすることを願っています。