java控制器里面生成验证码,java + springmvc 酷炫验证码分享

java + springmvc 酷炫验证码分享

一、maven 依赖

com.github.bingoohuang

patchca

0.0.1

二、spring mvc 控制器

import java.awt.Color;

import java.io.IOException;

import java.util.Random;

import javax.servlet.http.*;

import com.github.bingoohuang.patchca.background.*;

import com.github.bingoohuang.patchca.color.*;

import com.github.bingoohuang.patchca.custom.*;

import com.github.bingoohuang.patchca.filter.predefined.*;

import com.github.bingoohuang.patchca.font.*;

import com.github.bingoohuang.patchca.utils.encoder.EncoderHelper;

import com.github.bingoohuang.patchca.word.*;

@Controller

@RequestMapping("/login")

public class DemoController {

/**

* 彩色,验证码

* @param request

* @param response

* @throws IOException

*/

@RequestMapping("/verfiy-code")

public void VerifyCode(HttpServletRequest request, HttpServletResponse response) {

//生成验证码并写入输出流

String code = com.galaxyf.xd.web.utilis.VerifyCode.outputCaptcha(request, response);

//记录验证码到 session

/*

HttpSession session = req.getSession();

session.setAttribute("verify-code", code);

* */

}

}

三、验证码生产类(VerifyCode.java)

package com.springdemo.controller;

import java.awt.Color;

import java.io.IOException;

import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

import javax.servlet.http.HttpSession;

import com.github.bingoohuang.patchca.background.MyCustomBackgroundFactory;

import com.github.bingoohuang.patchca.color.ColorFactory;

import com.github.bingoohuang.patchca.custom.ConfigurableCaptchaService;

import com.github.bingoohuang.patchca.filter.predefined.CurvesRippleFilterFactory;

import com.github.bingoohuang.patchca.filter.predefined.DiffuseRippleFilterFactory;

import com.github.bingoohuang.patchca.filter.predefined.DoubleRippleFilterFactory;

import com.github.bingoohuang.patchca.filter.predefined.MarbleRippleFilterFactory;

import com.github.bingoohuang.patchca.filter.predefined.WobbleRippleFilterFactory;

import com.github.bingoohuang.patchca.font.RandomFontFactory;

import com.github.bingoohuang.patchca.utils.encoder.EncoderHelper;

import com.github.bingoohuang.patchca.word.RandomWordFactory;

public class VerifyCode {

static Logger log = Logger.getLogger(VerifyCode.class);

private static ConfigurableCaptchaService cs = new ConfigurableCaptchaService();

private static Random random = new Random();

static int verifyCodeType=1;//验证码字符集

/**

* 参数初始化

*/

static {

// cs.setColorFactory(new SingleColorFactory(new Color(25, 60, 170)));

// 设置颜色

cs.setColorFactory(new ColorFactory() {

@Override

public Color getColor(int x) {

int[] c = new int[3];

int i = random.nextInt(c.length);

for (int fi = 0; fi < c.length; fi++) {

if (fi == i) {

c[fi] = random.nextInt(71);

} else {

c[fi] = random.nextInt(256);

}

}

return new Color(c[0], c[1], c[2]);

}

});

RandomWordFactory wf = new RandomWordFactory();

//验证码字符集

switch (verifyCodeType) {

case 1: //纯数字

wf.setCharacters("0123456789");

break;

case 2: //纯字母

wf.setCharacters("abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ");

break;

default: //数字字母混合

wf.setCharacters("23456789abcdefghigkmnpqrstuvwxyzABCDEFGHIGKLMNPQRSTUVWXYZ");

break;

}

// 验证码位数

wf.setMaxLength(4); // 最大

wf.setMinLength(4); // 最小

cs.setWordFactory(wf);

// 设置图片大小

cs.setWidth(170);

cs.setHeight(60);

// 设置字体大小

RandomFontFactory font = new RandomFontFactory();

font.setMaxSize(32);

font.setMinSize(42);

cs.setFontFactory(font);

// 干扰线

MyCustomBackgroundFactory background = new MyCustomBackgroundFactory();

cs.setBackgroundFactory(background);

}

/**

* 设置 http 相应头信息

*

* @param response

*/

protected static void setResponseHeaders(HttpServletResponse response) {

response.setContentType("image/png");

response.setHeader("Cache-Control", "no-cache, no-store");

response.setHeader("Pragma", "no-cache");

long time = System.currentTimeMillis();

response.setDateHeader("Last-Modified", time);

response.setDateHeader("Date", time);

response.setDateHeader("Expires", time);

}

/**

* 输出验证码到响应流

*

* @param request

* @param response

* @throws IOException

*/

public static String outputCaptcha(HttpServletRequest request, HttpServletResponse response) {

//验证码效果

switch (random.nextInt(5)) {

case 0:

cs.setFilterFactory(new CurvesRippleFilterFactory(cs.getColorFactory()));

break;

case 1:

cs.setFilterFactory(new MarbleRippleFilterFactory());

break;

case 2:

cs.setFilterFactory(new DoubleRippleFilterFactory());

break;

case 3:

cs.setFilterFactory(new WobbleRippleFilterFactory());

break;

case 4:

cs.setFilterFactory(new DiffuseRippleFilterFactory());

break;

}

HttpSession session = request.getSession(false);

if (session == null) {

session = request.getSession();

}

setResponseHeaders(response);

String verifyCode="";

try {

// 验证码

verifyCode = EncoderHelper.getChallangeAndWriteImage(cs, "png", response.getOutputStream());

} catch (Exception e) {

log.error(e.getMessage(),e);

verifyCode="....";

}

return verifyCode;

}

} 四、测试 访问:http://localhost:8080/xxxxx/login/verfiy-code

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值