Google Kaptcha產生圖形驗證碼

來源:互聯網
上載者:User

google captcha 是google產生驗證碼的一個工具類,其原理是將隨機產生字串儲存到session中,同時以圖片的形式返回給頁面,之後前台頁面提交到後台進行對比。

依賴庫可以去maven repository查看:


但是我沒有下載下來,然後選擇了把jar包下載之後放到項目的lib下面,在通過gradle腳步編譯。

compile fileTree("$rootDir/lib") {        include 'kaptcha-2.3.jar'    }

網上說可以從阿里的maven倉庫下載,下載速度還行,挺穩定,附地址:http://maven.aliyun.com/nexus/#welcome,文末我會給出作者文章連結。

Google Captcha 是可以配置的,如下:

kaptcha.border  是否有邊框  預設為true  我們可以自己設定yes,no  kaptcha.border.color   邊框顏色   預設為Color.BLACK  kaptcha.border.thickness  邊框粗細度  預設為1  kaptcha.producer.impl   驗證碼產生器  預設為DefaultKaptcha  kaptcha.textproducer.impl   驗證碼文本產生器  預設為DefaultTextCreator  kaptcha.textproducer.char.string   驗證碼文本字元內容約制  預設為abcde2345678gfynmnpwx  kaptcha.textproducer.char.length   驗證碼文本字元長度  預設為5  kaptcha.textproducer.font.names    驗證碼文本字型樣式  預設為new Font("Arial", 1, fontSize), new Font("Courier", 1, fontSize)  kaptcha.textproducer.font.size   驗證碼文本字元大小  預設為40  kaptcha.textproducer.font.color  驗證碼文本字元顏色  預設為Color.BLACK  kaptcha.textproducer.char.space  驗證碼文本字元間距  預設為2  kaptcha.noise.impl    驗證碼噪點產生對象  預設為DefaultNoise  kaptcha.noise.color   驗證碼噪點顏色   預設為Color.BLACK  kaptcha.obscurificator.impl   驗證碼樣式引擎  預設為WaterRipple  kaptcha.word.impl   驗證碼文本字元渲染   預設為DefaultWordRenderer  kaptcha.background.impl   驗證碼背景產生器   預設為DefaultBackground  kaptcha.background.clear.from   驗證碼背景顏色漸進   預設為Color.LIGHT_GRAY  kaptcha.background.clear.to   驗證碼背景顏色漸進   預設為Color.WHITE  kaptcha.image.width   驗證碼圖片寬度  預設為200  kaptcha.image.height  驗證碼圖片高度  預設為50
配置servlet mapping(最簡單的方式,配置擷取驗證碼的介面,也可以通過springmvc 進行配置擷取)

在web項目下的web.xml配置

 <servlet>    <servlet-name>Kaptcha</servlet-name>    <servlet-class>com.google.code.kaptcha.servlet.KaptchaServlet</servlet-class>    <init-param>      <param-name>kaptcha.border</param-name>      <param-value>no</param-value>    </init-param>    <init-param>      <param-name>kaptcha.textproducer.char.string</param-name>      <param-value>02345689</param-value>    </init-param>    <init-param>      <param-name>kaptcha.background.clear.from</param-name>      <param-value>white</param-value>    </init-param>    <init-param>      <param-name>kaptcha.background.clear.to</param-name>      <param-value>white</param-value>    </init-param>    <init-param>      <param-name>kaptcha.obscurificator.impl</param-name>      <param-value>com.google.code.kaptcha.impl.WaterRipple</param-value>    </init-param>    <init-param>      <param-name>kaptcha.noise.color</param-name>      <param-value>255,96,0</param-value>    </init-param>  </servlet>  <servlet-mapping>    <servlet-name>Kaptcha</servlet-name>    <url-pattern>/images/captcha.jpg</url-pattern>  </servlet-mapping>

啟動服務,訪問:http://localhost:8140/auth-server/images/captcha.jpg就可以看到效果。


參考:SpringMvc項目中使用GoogleKaptcha 產生驗證碼

           springmvc使用Googlecaptcha產生圖片驗證碼

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.