前言
在我們用戶登錄的時候,為了安全性考慮,會增加驗證碼的功能,這里采用的是google的kaptcha;spirngboot是輕便,獨立,使得基于spring的應用開發(fā)變得特別簡單。網(wǎng)上有很多介紹springboot的介紹,這里不多說。
言歸正抓,講下登陸時驗證碼結(jié)合springboot的用法
引入kaptcha所需要的jar包,我這里用的是maven
1
2
3
4
5
6
7
8
9
10
11
12
|
<dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version> 2.3 . 2 </version> <exclusions> <exclusion> <artifactId>javax.servlet-api</artifactId> <groupId>javax.servlet</groupId> </exclusion> </exclusions> </dependency> |
去除包中自帶的servlet包。在我個人的理解中springboot就是javaconfig和注解搭建起來的輕型的微架構(gòu)。
下面是kapcha的javaconfig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
@Configuration public class CaptchaConfig { @Bean (name= "captchaProducer" ) public DefaultKaptcha getKaptchaBean(){ DefaultKaptcha defaultKaptcha= new DefaultKaptcha(); Properties properties= 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" , "宋體,楷體,微軟雅黑" ); Config config= new Config(properties); defaultKaptcha.setConfig(config); return defaultKaptcha; } } |
這里的的katcha的javaconfig相當于springmvc中的bean配置,下面給是一個針對上面javaconfig的springmvc的bean示例,供參考
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
< bean id = "captchaProducer" class = "com.google.code.kaptcha.impl.DefaultKaptcha" > < property name = "config" > < bean class = "com.google.code.kaptcha.util.Config" > < constructor-arg > < props > < prop key = "kaptcha.border" >yes</ prop > < prop key = "kaptcha.border.color" >105,179,90</ prop > < prop key = "kaptcha.textproducer.font.color" >blue</ prop > < prop key = "kaptcha.image.width" >125</ prop > < prop key = "kaptcha.image.height" >45</ prop > < prop key = "kaptcha.textproducer.font.size" >45</ prop > < prop key = "kaptcha.session.key" >code</ prop > < prop key = "kaptcha.textproducer.char.length" >4</ prop > < prop key = "kaptcha.textproducer.font.names" >宋體,楷體,微軟雅黑</ prop > </ props > </ constructor-arg > </ bean > </ property > </ bean > |
其中構(gòu)造方法中的屬性參數(shù)可以根據(jù)自己的需求來設(shè)置。
配置文件已經(jīng)配好,那么如何獲取自己的二維碼呢,我的理解是畫布的概念,然后將生成的四位的驗證碼生成對應的畫布,然后讓結(jié)果write出去。
代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
|
@RequestMapping (value = "/captcha-image" ) public ModelAndView getKaptchaImage(HttpServletRequest request, HttpServletResponse response) throws Exception { response.setDateHeader( "Expires" , 0 ); response.setHeader( "Cache-Control" , "no-store, no-cache, must-revalidate" ); response.addHeader( "Cache-Control" , "post-check=0, pre-check=0" ); response.setHeader( "Pragma" , "no-cache" ); response.setContentType( "image/jpeg" ); String capText = captchaProducer.createText(); System.out.println( "capText: " + capText); try { String uuid=UUIDUtils.getUUID32().trim().toString(); redisTemplate.opsForValue().set(uuid, capText, 60 * 5 ,TimeUnit.SECONDS); Cookie cookie = new Cookie( "captchaCode" ,uuid); response.addCookie(cookie); } catch (Exception e) { e.printStackTrace(); } BufferedImage bi = captchaProducer.createImage(capText); ServletOutputStream out = response.getOutputStream(); ImageIO.write(bi, "jpg" , out); try { out.flush(); } finally { out.close(); } return null ; } |
如上面的代碼,在用戶登錄的時候使用驗證碼以及cooike中的captchacode來實現(xiàn)唯一性驗證,開始的時候我考慮到放到session中,當時想了下,感覺這不科學啊,比如講captchacode放到session中,這時候驗證碼是一個,后來另一個用戶再登陸,前一個用戶還在登陸中,這時候會出現(xiàn)一系列的問題。這里使用cookie和redis,來應對用戶的并發(fā)登陸驗證。
頁面使用也比較簡單如下:
1
2
3
|
< div style = "float: left;" > < i >< img style = "height:22px;" id = "codeImg" alt = "點擊更換" title = "點擊更換" src = "code/captcha-image" /></ i > </ div > |
更換的話加一個click事件,然后清空以前在redis中對應的緩存數(shù)據(jù);或者在獲取驗證碼的時候,設(shè)置生存周期。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對服務器之家的支持。
原文鏈接:http://blog.csdn.net/liunian02050328/article/details/53462053