首页 > 编程语言 >Java 图形验证码

Java 图形验证码

时间:2023-02-07 10:25:32浏览次数:39  
标签:HttpServletRequest Java request 验证码 param HttpServletResponse 图形 response

简介

Java 图形验证码,支持 Gif、中文、算术等类型,可用于 Java Web、JavaSE 等项目。

效果展

 

 

 

集成项目

  • maven 方式引入:
<dependency>
       <groupId>com.github.whvcse</groupId>
       <artifactId>easy-captcha</artifactId>
       <version>1.6.2</version>
</dependency>
  • 在 SpringBoot 中使用
package com.hikvision.websocketparent.controller;

import com.wf.captcha.utils.CaptchaUtil;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author dongliang7
 * @projectName
 * @ClassName CaptchaController.java
 * @description: 图形验证码控制层
 * @createTime 2022年12月28日 12:30:00
 */
@RestController
@RequestMapping("/captcha")
public class CaptchaController {

    /**
     * 获取验证码
     * @param request
     * @param response
     * @throws IOException
     */
    @GetMapping("/getCaptcha")
    public void captcha(HttpServletRequest request, HttpServletResponse response) throws IOException {
        CaptchaUtil.out(request, response);
    }

    /**
     * 验证码校验
     * @param verCode
     * @param request
     * @return
     */
    @GetMapping("/verification")
    public String verification(@RequestParam String verCode, HttpServletRequest request){
        if (!CaptchaUtil.ver(verCode, request)) {
            CaptchaUtil.clear(request);  // 清除session中的验证码
            return "验证码不正确";
        }
        return "验证码验证成功";
    }
}
  • 图片验证码公用类如果想改造,可以自行实现CaptchaUtil
package com.wf.captcha.utils;

import java.awt.*;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.wf.captcha.base.Captcha;
import com.wf.captcha.SpecCaptcha;

/**
 * 图形验证码工具类
 * Created by 王帆 on 2018-07-27 上午 10:08.
 */
public class CaptchaUtil {
    private static final String SESSION_KEY = "captcha";
    private static final int DEFAULT_LEN = 4;  // 默认长度
    private static final int DEFAULT_WIDTH = 130;  // 默认宽度
    private static final int DEFAULT_HEIGHT = 48;  // 默认高度

    /**
     * 输出验证码
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @throws IOException IO异常
     */
    public static void out(HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        out(DEFAULT_LEN, request, response);
    }

    /**
     * 输出验证码
     *
     * @param len      长度
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @throws IOException IO异常
     */
    public static void out(int len, HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        out(DEFAULT_WIDTH, DEFAULT_HEIGHT, len, request, response);
    }

    /**
     * 输出验证码
     *
     * @param width    宽度
     * @param height   高度
     * @param len      长度
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @throws IOException IO异常
     */
    public static void out(int width, int height, int len, HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        out(width, height, len, null, request, response);
    }

    /**
     * 输出验证码
     *
     * @param font     字体
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @throws IOException IO异常
     */
    public static void out(Font font, HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        out(DEFAULT_WIDTH, DEFAULT_HEIGHT, DEFAULT_LEN, font, request, response);
    }

    /**
     * 输出验证码
     *
     * @param len      长度
     * @param font     字体
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @throws IOException IO异常
     */
    public static void out(int len, Font font, HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        out(DEFAULT_WIDTH, DEFAULT_HEIGHT, len, font, request, response);
    }

    /**
     * 输出验证码
     *
     * @param width    宽度
     * @param height   高度
     * @param len      长度
     * @param font     字体
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @throws IOException IO异常
     */
    public static void out(int width, int height, int len, Font font, HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        SpecCaptcha specCaptcha = new SpecCaptcha(width, height, len);
        if (font != null) {
            specCaptcha.setFont(font);
        }
        out(specCaptcha, request, response);
    }


    /**
     * 输出验证码
     *
     * @param captcha  Captcha
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @throws IOException IO异常
     */
    public static void out(Captcha captcha, HttpServletRequest request, HttpServletResponse response)
            throws IOException {
        setHeader(response);
        request.getSession().setAttribute(SESSION_KEY, captcha.text().toLowerCase());
        captcha.out(response.getOutputStream());
    }

    /**
     * 验证验证码
     *
     * @param code    用户输入的验证码
     * @param request HttpServletRequest
     * @return 是否正确
     */
    public static boolean ver(String code, HttpServletRequest request) {
        if (code != null) {
            String captcha = (String) request.getSession().getAttribute(SESSION_KEY);
            return code.trim().toLowerCase().equals(captcha);
        }
        return false;
    }

    /**
     * 清除session中的验证码
     *
     * @param request HttpServletRequest
     */
    public static void clear(HttpServletRequest request) {
        request.getSession().removeAttribute(SESSION_KEY);
    }

    /**
     * 设置相应头
     *
     * @param response HttpServletResponse
     */
    public static void setHeader(HttpServletResponse response) {
        response.setContentType("image/gif");
        response.setHeader("Pragma", "No-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
    }

}

源码:https://gitee.com/whvse/EasyCaptcha

标签:HttpServletRequest,Java,request,验证码,param,HttpServletResponse,图形,response
From: https://www.cnblogs.com/dongl961230/p/17097456.html

相关文章

  • java stream
    多字段mapMap<Integer,String>map=list.stream().collect(Collectors.toMap(Entity::getId,Entity::getType));//可改成Map<Integer,String>map=list.strea......
  • java.net.BindException: Cannot assign requested address
    这个错误如果从没见过,绝对会浪费大家好多时间去查找,而且网上千篇文章都没有一个真正的解决办法!欢迎请大家转载!!新服务器配置tomcat7.0环境,配置完毕后,......
  • JNI调用java中的类方法和静态方法
    在JNI调用中,肯定会涉及到本地方法操作Java类中数据和方法。在Java1.0中“原始的”Java到C的绑定中,程序员可以直接访问对象数据域。然而,直接方法要求虚......
  • java GZIPOutputStream,GZIPInputStream 用法
    GZIP常常用在linxu环境下,是一种非常简单的压缩算法。在Java实现API中,它仅仅包含两个实现类:GZIPInputStream和GZIPOutputStream。GZIPOutputStream类......
  • 掌握java枚举类型(enum type)
    1背景在java语言中还没有引入枚举类型之前,表示枚举类型的常用模式是声明一组具有int常量。之前我们通常利用publicfinalstatic方法定义的代码如......
  • [java] JSON格式校验
    对JSON字符串进行格式校验,不依赖于第三方包packagecom.iaiai.test;importjava.text.CharacterIterator;importjava.text.StringCharacterIterator......
  • [java] 判断两台机器之间网络是否可达
    Java类库判断两台机器之间网络是否可达,常用ping方法来实现。packagecom.iaiai.test;importjava.net.InetAddress;/****<br/>*Title:Test.ja......
  • 用java实现zip压缩
    本来是写到spaceslive上的,可是代码的显示效果确实不怎么好看。在javaeye上试了试代码显示的顺眼多了。今天写了个用java压缩的功能,可以实现对文件......
  • java.lang.SecurityException: Access to default session denied
    {exception=java.lang.SecurityException:Accesstodefaultsessiondenied}java.lang.SecurityException:Accesstodefaultsessiondeniedat......
  • 根据经纬度求两点间距离实现源码(java)
    研究了一下GoogleMap上的根据经纬度求地球表面两点间距离的实现,用java实现了一把,对我国境内的Beijing54,Xian80,WGS84三种坐标系的空间距离计算感......