借助于Kaptcha生成验证码实战
需要注意以下文件kaptcha.properties,KaptchaConfig.java,CommonKaptcha.java,KaptchaController.java。
1.pom文件引入jar包:
<!-- kaptcha 验证码 -->
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
2.kaptcha.properties内容:
#Kaptcha
kaptcha.border=no
#kaptcha.border.thickness=1
kaptcha.border.color=105,179,90
kaptcha.textproducer.char.string=0123456789
kaptcha.textproducer.char.length=4
#kaptcha.textproducer.char.space=2
kaptcha.textproducer.font.color=black
kaptcha.textproducer.font.size=70
kaptcha.textproducer.font.names=\u5B8B\u4F53,\u6977\u4F53,\u5FAE\u8F6F\u96C5\u9ED1
kaptcha.image.width=250
kaptcha.image.height=90
kaptcha.session.key=WEB_YZM
kaptcha.background.clear.from=white
kaptcha.background.clear.to=gray
#kaptcha.noise.impl=com.google.code.kaptcha.impl.NoNoise
#kaptcha.obscurificator.impl=com.google.code.kaptcha.impl.ShadowGimpy
3.KaptchaConfig.java 内容:
import java.util.Properties;
import org.springframework.stereotype.Component;
import com.google.code.kaptcha.util.Config;
import com.common.tool.SysUtil;
/**
* Description: 验证码配置
* All Rights Reserved.
* @version 1.0 2015-1-15 下午4:28:54 by
*/
@Component("kaptchaConfig")
public class KaptchaConfig extends Config{
/**
* 验证码配置资源名
*/
private static final String RESNAME_KAPTCHA = "/kaptcha.properties";
private static final Properties PROPERTIES_KAPTCHA = SysUtil.getProperties(RESNAME_KAPTCHA);
/**
* 加载资源
*/
public KaptchaConfig() {
super(PROPERTIES_KAPTCHA);
}
}
4.CommonKaptcha.java文件内容:
import org.springframework.stereotype.Component;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
/**
* Description:
* All Rights Reserved.
* @version 1.0 2015-1-15 下午5:08:17 by
*/
@Component("commonKaptcha")
public class CommonKaptcha extends DefaultKaptcha{
@Resource(name = "kaptchaConfig")
public void setConfig(Config config) {
super.setConfig(config);
}
}
5.KaptchaController.java文件内容:
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
/**
* Description: 生成验证码controller
* All Rights Reserved.
* @version 1.0 2015-1-20 上午10:52:45 by
*/
@Controller
public class KaptchaController{
/**
* @Fields LOGGER : TODO(日志记录器)
*/
private static final Logger LOGGER = LoggerFactory.getLogger(KaptchaController.class);
@Autowired
private DefaultKaptcha sZKaptcha ;
/**
* Description: 生成验证码
* @Version1.0 2015-2-4 下午2:03:19 by
* @param session
* @return
*/
@RequestMapping(value = "/getYzmImage", method = RequestMethod.GET)
public ResponseEntity<byte[]> getKaptchaImage(HttpSession session) {
ResponseEntity<byte[]> responseEntity = null;
ByteArrayOutputStream out = null;
try {
String capText = sZKaptcha.createText();
LOGGER.debug("******************生成验证码: {}******************",capText);
//存贮验证码
session.setAttribute(Constants.KAPTCHA_SESSION_CONFIG_KEY, capText);
// 创建图片
BufferedImage bi = sZKaptcha.createImage(capText);
out = new ByteArrayOutputStream();
ImageIO.write(bi, "JPEG", out);
HttpHeaders headers= new HttpHeaders();
headers.setCacheControl("no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
headers.setPragma("no-cache");
headers.setExpires(0);
headers.setContentType(MediaType.IMAGE_JPEG);
responseEntity = new ResponseEntity<byte[]>(out.toByteArray(), headers, HttpStatus.OK);
} catch (IOException e) {
LOGGER.error("生成验证码异常:{}", e.getMessage());
} finally {
if(out != null) {
IOUtils.closeQuietly(out); //关闭流
}
}
return responseEntity;
}
}
1.pom文件引入jar包:
<!-- kaptcha 验证码 -->
<dependency>
<groupId>com.github.axet</groupId>
<artifactId>kaptcha</artifactId>
<version>0.0.9</version>
</dependency>
2.kaptcha.properties内容:
#Kaptcha
kaptcha.border=no
#kaptcha.border.thickness=1
kaptcha.border.color=105,179,90
kaptcha.textproducer.char.string=0123456789
kaptcha.textproducer.char.length=4
#kaptcha.textproducer.char.space=2
kaptcha.textproducer.font.color=black
kaptcha.textproducer.font.size=70
kaptcha.textproducer.font.names=\u5B8B\u4F53,\u6977\u4F53,\u5FAE\u8F6F\u96C5\u9ED1
kaptcha.image.width=250
kaptcha.image.height=90
kaptcha.session.key=WEB_YZM
kaptcha.background.clear.from=white
kaptcha.background.clear.to=gray
#kaptcha.noise.impl=com.google.code.kaptcha.impl.NoNoise
#kaptcha.obscurificator.impl=com.google.code.kaptcha.impl.ShadowGimpy
3.KaptchaConfig.java 内容:
import java.util.Properties;
import org.springframework.stereotype.Component;
import com.google.code.kaptcha.util.Config;
import com.common.tool.SysUtil;
/**
* Description: 验证码配置
* All Rights Reserved.
* @version 1.0 2015-1-15 下午4:28:54 by
*/
@Component("kaptchaConfig")
public class KaptchaConfig extends Config{
/**
* 验证码配置资源名
*/
private static final String RESNAME_KAPTCHA = "/kaptcha.properties";
private static final Properties PROPERTIES_KAPTCHA = SysUtil.getProperties(RESNAME_KAPTCHA);
/**
* 加载资源
*/
public KaptchaConfig() {
super(PROPERTIES_KAPTCHA);
}
}
4.CommonKaptcha.java文件内容:
import org.springframework.stereotype.Component;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
/**
* Description:
* All Rights Reserved.
* @version 1.0 2015-1-15 下午5:08:17 by
*/
@Component("commonKaptcha")
public class CommonKaptcha extends DefaultKaptcha{
@Resource(name = "kaptchaConfig")
public void setConfig(Config config) {
super.setConfig(config);
}
}
5.KaptchaController.java文件内容:
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.http.HttpSession;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.impl.DefaultKaptcha;
/**
* Description: 生成验证码controller
* All Rights Reserved.
* @version 1.0 2015-1-20 上午10:52:45 by
*/
@Controller
public class KaptchaController{
/**
* @Fields LOGGER : TODO(日志记录器)
*/
private static final Logger LOGGER = LoggerFactory.getLogger(KaptchaController.class);
@Autowired
private DefaultKaptcha sZKaptcha ;
/**
* Description: 生成验证码
* @Version1.0 2015-2-4 下午2:03:19 by
* @param session
* @return
*/
@RequestMapping(value = "/getYzmImage", method = RequestMethod.GET)
public ResponseEntity<byte[]> getKaptchaImage(HttpSession session) {
ResponseEntity<byte[]> responseEntity = null;
ByteArrayOutputStream out = null;
try {
String capText = sZKaptcha.createText();
LOGGER.debug("******************生成验证码: {}******************",capText);
//存贮验证码
session.setAttribute(Constants.KAPTCHA_SESSION_CONFIG_KEY, capText);
// 创建图片
BufferedImage bi = sZKaptcha.createImage(capText);
out = new ByteArrayOutputStream();
ImageIO.write(bi, "JPEG", out);
HttpHeaders headers= new HttpHeaders();
headers.setCacheControl("no-store, no-cache, must-revalidate, post-check=0, pre-check=0");
headers.setPragma("no-cache");
headers.setExpires(0);
headers.setContentType(MediaType.IMAGE_JPEG);
responseEntity = new ResponseEntity<byte[]>(out.toByteArray(), headers, HttpStatus.OK);
} catch (IOException e) {
LOGGER.error("生成验证码异常:{}", e.getMessage());
} finally {
if(out != null) {
IOUtils.closeQuietly(out); //关闭流
}
}
return responseEntity;
}
}