服務器開發之圖形驗證碼
驗證碼
驗證碼是一種區分用戶是計算機還是人的公共全自動程序。短時間是無法退出人類舞臺的,目前只是盡量提升用戶體驗。
作用
-
賬號安全
-
反作弊
-
反爬蟲
-
防論壇灌水
-
防惡意注冊
分類
-
圖形驗證碼
-
Gif動畫驗證碼
-
手機短信驗證碼
-
手機語音驗證碼
-
視頻驗證碼
-
web2.0驗證碼
kaptcha驗證碼組件
簡介
kaptcha 是一個非常實用的驗證碼生成工具。有了它,你可以生成各種樣式的驗證碼,因為它是可配置的。
常見配置
-
驗證碼的字體
-
驗證碼字體的大小
-
驗證碼字體的字體顏色
-
驗證碼內容的范圍(數字,字母,中文漢字!)
-
驗證碼圖片的大小,邊框,邊框粗細,邊框顏色
-
驗證碼的干擾線(可以自己繼承com.google.code.kaptcha.NoiseProducer寫一個自定義的干擾線)
-
驗證碼的樣式(魚眼樣式、3D、普通模糊……當然也可以繼承com.google.code.kaptcha.GimpyEngine自定義樣式)
maven
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
kaptcha.properties
kaptcha.textproducer.font.color=red
kaptcha.image.width=130
kaptcha.image.height=44
kaptcha.textproducer.font.size=35
kaptcha.textproducer.char.length=4
kaptcha.textproducer.font.names=\\u5B8B\\u4F53,\\u6977\\u4F53,\\u5FAE\\u8F6F\\u96C5\\u9ED1
kaptcha.noise.color=gray
kaptcha.obscurificator.impl=com.google.code.kaptcha.impl.WaterRipple
代碼具體實現
本代碼主要用filter過濾器來生成驗證碼。
web.xml
<filter>
<filter-name>KaptchaFilter</filter-name>
<filter-class>com.xxoo.admin.ui.filter.KaptchaFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>KaptchaFilter</filter-name>
<url-pattern>/kaptcha.jpg</url-pattern>
</filter-mapping>
注意:驗證碼過濾器需要放到Shiro之后,因為Shiro將包裝HttpSession.如果不,可能造成兩次的sesisonid不一樣。
圖片驗證碼類
public class CaptchaService {
private static ImageCaptchaService instance = null;
static {
instance = new KaptchaImageCaptchaService();
}
public synchronized static ImageCaptchaService getInstance() {
return instance;
}
public synchronized static boolean validate(HttpServletRequest httpServletRequest, String input) throws Exception {
String text = instance.getText(httpServletRequest);
boolean result = text.equalsIgnoreCase(input);
instance.removeKaptcha(httpServletRequest);
return result;
}
}
基于Kaptcha的驗證碼圖片實現
public class KaptchaImageCaptchaService implements ImageCaptchaService {
private Logger logger = LoggerFactory.getLogger(getClass());
public KaptchaImageCaptchaService() {
}
public static Config getConfig() throws IOException {
Properties p = new Properties();
p.load(new DefaultResourceLoader().getResource("kaptcha.properties").getInputStream());
Config config = new Config(p);
return config;
}
@Override
public void create(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
httpServletResponse.setDateHeader("Expires", 0L);
httpServletResponse.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
httpServletResponse.addHeader("Cache-Control", "post-check=0, pre-check=0");
httpServletResponse.setHeader("Pragma", "no-cache");
httpServletResponse.setContentType("image/jpeg");
Config config = getConfig();
Producer producer = config.getProducerImpl();
String capText = producer.createText();
if(logger.isDebugEnabled()){
logger.info("create captcha:" + capText + ":" + config.getSessionKey() );
}
httpServletRequest.getSession().setAttribute(config.getSessionKey(), capText);
httpServletRequest.getSession().setAttribute(config.getSessionDate(), new Date());
BufferedImage bi = producer.createImage(capText);
ServletOutputStream out = httpServletResponse.getOutputStream();
ImageIO.write(bi, "jpg", out);
out.flush();
out.close();
}
@Override
public String getText(HttpServletRequest httpServletRequest) throws Exception {
return (String)httpServletRequest.getSession().getAttribute(getConfig().getSessionKey());
}
@Override
public void removeKaptcha(HttpServletRequest httpServletRequest) throws Exception {
httpServletRequest.getSession().removeAttribute(getConfig().getSessionKey());
httpServletRequest.getSession().removeAttribute(getConfig().getSessionDate());
}
}
驗證碼工具類
public class CaptchaService {
private static ImageCaptchaService instance = null;
static {
instance = new KaptchaImageCaptchaService();
}
public synchronized static ImageCaptchaService getInstance() {
return instance;
}
public synchronized static boolean validate(HttpServletRequest httpServletRequest, String input) throws Exception {
String text = instance.getText(httpServletRequest);
boolean result = text.equalsIgnoreCase(input);
instance.removeKaptcha(httpServletRequest);
return result;
}
}
生成驗證碼過濾器
public class KaptchaFilter extends OncePerRequestFilter {
private Logger logger = LoggerFactory.getLogger(getClass());
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
try {
CaptchaService.getInstance().create(httpServletRequest,httpServletResponse);
} catch (Exception e) {
logger.info("create captcha error.",e);
}
}
}
驗證碼校驗
private boolean doCaptchaValidate(HttpServletRequest request, String code) {
//比對
try {
if (code == null || !CaptchaService.validate(request, code)) {
return false;
} else {
return true;
}
} catch (Exception e) {
logger.warn("captcha check error!");
return false;
}
}
小結
本文主要講述了kaptcha圖形化驗證碼的使用和介紹,小伙伴可以根據自己的需求進行引入;后面會繼續介紹web2.0極驗證。
來自:http://www.jianshu.com/p/90963f73ad5b
本文由用戶 BrandyDunro 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!