patchca生成图形验证码

最近在搞图形验证码,

 两个思路, 可以服务端生成验证码 ,放在云上(七牛云或者oss)上下发客户端地址 ;

也可以直接让客户端段访问 以IO流的方式传输到页面,并在页面直接展示;
而服务端产生图形验证码的方式有很多种 ,主要都是通过java.swing 来画, 
      下面着重介绍patchca产生验证码的过程;

 

      首先pom 引入


		
			net.pusuo
			patchca
			0.5.0
		

 然后引入工具方法类

private static final String DEFAULT_CHARACTERS = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; //
    private static int DEFAULT_FONT_SIZE = 25;
    private static int DEFAULT_WORD_LENGTH = 6;
    private static int DEFAULT_WIDTH = 80;
    private static int DEFAULT_HEIGHT = 35;
    public static CaptchaService create(int fontSize, int wordLength, String characters, int width, int height) {
        ConfigurableCaptchaService service = null;
        // 字体大小设置
        RandomFontFactory ff = new RandomFontFactory();
        ff.setMinSize(fontSize);
        ff.setMaxSize(fontSize);
        // 生成的单词设置
        RandomWordFactory rwf = new RandomWordFactory();
        rwf.setCharacters(characters);
        rwf.setMinLength(wordLength);
        rwf.setMaxLength(wordLength);
        // 干扰线波纹
        CurvesRippleFilterFactory crff = new CurvesRippleFilterFactory();
        // 处理器
        service = new ExConfigurableCaptchaService();
        service.setFontFactory(ff);
        service.setWordFactory(rwf);
        service.setFilterFactory(crff);
// 生成图片大小(像素)
        service.setWidth(width);
        service.setHeight(height);
        return service;
    }
    public static CaptchaService create() {
        return create(DEFAULT_FONT_SIZE, DEFAULT_WORD_LENGTH, DEFAULT_CHARACTERS, DEFAULT_WIDTH, DEFAULT_HEIGHT);
    }
    // 随机变色处理
    static class ExConfigurableCaptchaService extends ConfigurableCaptchaService {
        private static final Random RANDOM = new Random();
        private List colorList = new ArrayList(); // 为了性能
        public ExConfigurableCaptchaService() {
            colorList.add(new SingleColorFactory(Color.blue));
            colorList.add(new SingleColorFactory(Color.black));
            colorList.add(new SingleColorFactory(Color.red));
            colorList.add(new SingleColorFactory(Color.pink));
            colorList.add(new SingleColorFactory(Color.orange));
            colorList.add(new SingleColorFactory(Color.green));
            colorList.add(new SingleColorFactory(Color.magenta));
            colorList.add(new SingleColorFactory(Color.cyan));
        }
        public ColorFactory nextColorFactory() {
            int index = RANDOM.nextInt(colorList.size());
            return (ColorFactory)colorList.get(index);
        }
        @Override
        public Captcha getCaptcha() {
            ColorFactory cf = nextColorFactory();
            CurvesRippleFilterFactory crff = (CurvesRippleFilterFactory) this.getFilterFactory();
            crff.setColorFactory(cf);
            this.setColorFactory(cf);
            return super.getCaptcha();
        }
    }

然后 controller层直接调用方法 把流文件写到response里面
 

      @RequestMapping(value = "/getcaptach", method = RequestMethod.GET)
    @ResponseBody
    public void captach(HttpServletRequest request, HttpServletResponse response) throws Exception{
        CaptchaService service = GlobalController.create();
        FileOutputStream fos = null;
        try {
            String token=EncoderHelper.getChallangeAndWriteImage(service, "png", response.getOutputStream());

            System.out.println(token);
        } finally {
            if (fos != null) {
                try {
                    fos.close();
                } catch (Exception e) {
                }
            }
        }
    }

运行效果展示

patchca生成图形验证码_第1张图片

你可能感兴趣的:(工具使用)