import java.io.UnsupportedEncodingException;
import java.util.Random;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.google.code.kaptcha.util.Config;
@Component
public class ChineseText {
@Autowired
private Config config;
public String getText() {
int length = config.getTextProducerCharLength();
char[] chars = config.getTextProducerCharString();
String finalWord = "", firstWord = "";
String[] array = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9",
"a", "b", "c", "d", "e", "f" };
Random rand = new Random();
for (int i = 0; i < length; i++) {
switch (rand.nextInt(3)) {
case 1:
firstWord = String.valueOf(chars[rand.nextInt(chars.length)]);
break;
case 2:
int r1,
r2,
r3,
r4;
String strH,
strL;// high&low
r1 = rand.nextInt(3) + 11; // 前闭后开[11,14)
if (r1 == 13) {
r2 = rand.nextInt(7);
} else {
r2 = rand.nextInt(16);
}
r3 = rand.nextInt(6) + 10;
if (r3 == 10) {
r4 = rand.nextInt(15) + 1;
} else if (r3 == 15) {
r4 = rand.nextInt(15);
} else {
r4 = rand.nextInt(16);
}
strH = array[r1] + array[r2];
strL = array[r3] + array[r4];
byte[] bytes = new byte[2];
bytes[0] = (byte) (Integer.parseInt(strH, 16));
bytes[1] = (byte) (Integer.parseInt(strL, 16));
try {
firstWord = new String(bytes, "GB2312");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
break;
default:
firstWord = String.valueOf(chars[rand.nextInt(chars.length)]);
break;
}
finalWord += firstWord;
}
return finalWord;
}
}
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import com.dominos.cloud.common.helper.RedisManager;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
public class RedisKaptcha {
@Autowired
private Producer producer;
@Autowired
private ChineseText chineseText;
@Value("${chineseQRCode:false}")
private Boolean chineseQRCode = false;
@Autowired
private RedisManager redisManager;
@RequestMapping("/get")
public void getKaptcha(HttpServletResponse response, String key) throws IOException {
/**
* 无缓存
*/
response.setDateHeader("Expires", 0);
// Set standard HTTP/1.1 no-cache headers.
response.setHeader("Cache-Control", "no-store, no-cache, must-revalidate");
// Set IE extended HTTP/1.1 no-cache headers (use addHeader).
response.addHeader("Cache-Control", "post-check=0, pre-check=0");
// Set standard HTTP/1.0 no-cache header.
response.setHeader("Pragma", "no-cache");
response.setContentType("image/jpeg");
String text = null;
if (chineseQRCode) {
text = chineseText.getText();
if (null == text) {
text = producer.createText();
}
} else {
text = producer.createText();
}
BufferedImage image = producer.createImage(text);
/** 存入session 校验是否正确 */
// session.setAttribute(Constants.KAPTCHA_SESSION_CONFIG_KEY, text);
// System.out.println(text);
redisManager.setex(Constants.KAPTCHA_SESSION_CONFIG_KEY + "_" + key, text, 60);
ServletOutputStream outputStream = response.getOutputStream();
ImageIO.write(image, "jpg", outputStream);
}
public boolean isValid(HttpServletRequest request, /* HttpSession session, */String captcha, String key) {
// String text = (String)
// session.getAttribute(Constants.KAPTCHA_SESSION_CONFIG_KEY);
String redisKey = Constants.KAPTCHA_SESSION_CONFIG_KEY + "_" + key;
String text = (String) redisManager.get(redisKey, String.class);
redisManager.del(redisKey);
if (StringUtils.isEmpty(text)) {
return false;
}
if (text.equalsIgnoreCase(captcha)) {
return true;
}
return false;
}
}
import java.util.Properties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
@Configuration
public class KaptchaAutoConfiguration {
/**
* <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>
*/
@Bean
public Producer producer() {
DefaultKaptcha p = new DefaultKaptcha();
p.setConfig(config());
return p;
}
@Bean
public Config config() {
Properties properties = new Properties();
// properties.setProperty(Constants.KAPTCHA_BORDER, "yes");
// properties.setProperty(Constants.KAPTCHA_BORDER_COLOR, "105,179,90");
// properties.setProperty(Constants.KAPTCHA_IMAGE_HEIGHT, "50");
// properties.setProperty(Constants.KAPTCHA_IMAGE_WIDTH, "150");
// properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_SIZE, "40");
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_COLOR, "black");
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_CHAR_LENGTH, "4");
properties.setProperty(Constants.KAPTCHA_TEXTPRODUCER_FONT_NAMES, "宋体,楷体,微软雅黑");
Config config = new Config(properties);
return config;
}
@Bean
public RedisKaptcha redisKaptcha() {
RedisKaptcha redisKaptcha = new RedisKaptcha();
return redisKaptcha;
}
}
@RestController
@RequestMapping(value = "/kaptcha")
public class KaptchaController {
@Autowired
private RedisKaptcha redisKaptcha;
@RequestMapping(value = "/get",method = {RequestMethod.POST, RequestMethod.GET})
public void getCap(HttpServletRequest request, HttpServletResponse response) throws IOException {
redisKaptcha.getKaptcha(response,"");
}
@RequestMapping(value = "/valid",method = {RequestMethod.POST, RequestMethod.GET})
public boolean isValid(HttpServletRequest request, String captcha) {
return redisKaptcha.isValid(request, captcha, "");
}
}