Java 后端、滑块验证码实现 亲测可用(项目不同需要小改)

滑块验证码 主要就是一个X、Y轴多少的问题

X轴,控制图片缺口的横向移动距离

Y轴,控制你缺口滑块的高度,然后X轴移动到指定距离才对的上

话不多说,上代码,具体请看注释,或者下方提问!欢迎各位大佬提出更好的意见!

controller层

@RequestMapping("/get")
	public Result<Object> getCode(HttpServletRequest request, HttpServletResponse response, String tempCode) throws Exception {
		Result<Object> result = new Result<>();
		log.info("getCode start");
		Random random = new Random();
        //有多少张图片 设置多少个随机数,随机选出一张图
		int i = random.nextInt(19) + 1;
        //拼接绝对路径 我这里放在了项目部署的服务器上,你们可以随意
		String path = "/home/ftp/identify/verification/original/bg" + i + ".png";
		//String path = "C:\\Users\\admin\\Desktop\\original\\bg" + i + ".png";
		log.info("path:{}", path);
		VerifyImage verifyImage = VerifyImageUtil.getVerifyImage(path);
		log.info("X坐标:{}", verifyImage.getX());
		String s = DigestUtils.md5Hex(sKey + verifyImage.getY());
		redisMapper.set("code::" + tempCode, String.valueOf(verifyImage.getX()), codeExpireTime);
		redisMapper.set("code::" + tempCode + "_Y", s, codeExpireTime);
		verifyImage.setX(null);
		result.setDatas(verifyImage);
		return result;
	}

实体类:

package com.idc.utils;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author jiexu
 * @version 1.0.0
 * @ClassName VerifyImage.java
 * @Description TODO
 * @createTime 2021年04月29日 18:35:00
 */
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class VerifyImage {
    private String srcImage;
    private String markImage;
    private Integer x;
    private Integer y;
}

工具类:

package com.java.utils;

import lombok.extern.slf4j.Slf4j;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * @author wcybaonier
 * @version 1.0.0
 * @ClassName VerifyImageUtil.java
 * @Description TODO
 * @createTime 2021年04月29日 18:34:00
 */
@Slf4j
public class VerifyImageUtil {
    /**
     * 源文件宽度
     */
    private static int ORI_WIDTH = 300;
    /**
     * 源文件高度
     */
    private static int ORI_HEIGHT = 150;
    /**
     * 模板图宽度
     */
    private static int CUT_WIDTH = 50;
    /**
     * 模板图高度
     */
    private static int CUT_HEIGHT = 50;
    /**
     * 抠图凸起圆心
     */
    private static int circleR = 5;
    /**
     * 抠图内部矩形填充大小
     */
    private static int RECTANGLE_PADDING = 8;
    /**
     * 抠图的边框宽度
     */
    private static int SLIDER_IMG_OUT_PADDING = 1;


    /**
     * 根据传入的路径生成指定验证码图片
     *
     * @param filePath
     * @return
     * @throws IOException
     */
    public static VerifyImage getVerifyImage( String filePath) throws IOException {
        log.info("----------------img---"+filePath);
        BufferedImage srcImage = ImageIO.read(new File(filePath));
        int locationX = CUT_WIDTH + new Random().nextInt(srcImage.getWidth() - CUT_WIDTH * 3);
        int locationY = CUT_HEIGHT + new Random().nextInt(srcImage.getHeight() - CUT_HEIGHT) / 2;
        System.out.println(locationX);
        System.out.println(locationY);
        BufferedImage markImage = new BufferedImage(CUT_WIDTH,CUT_HEIGHT,BufferedImage.TYPE_4BYTE_ABGR);
        int[][] data = getBlockData();
        cutImgByTemplate(srcImage, markImage, data, locationX, locationY);
        return new VerifyImage(getImageBASE64(srcImage),getImageBASE64(markImage),locationX,locationY);
    }


    /**
     * 生成随机滑块形状
     * <p>
     * 0 透明像素
     * 1 滑块像素
     * 2 阴影像素
     * @return int[][]
     */
    private static int[][] getBlockData() {
        int[][] data = new int[CUT_WIDTH][CUT_HEIGHT];
        Random random = new Random();
        //(x-a)²+(y-b)²=r²
        //x中心位置左右5像素随机
        double x1 = RECTANGLE_PADDING + (CUT_WIDTH - 2 * RECTANGLE_PADDING) / 2.0 - 5 + random.nextInt(10);
        //y 矩形上边界半径-1像素移动
        double y1_top = RECTANGLE_PADDING - random.nextInt(3);
        double y1_bottom = CUT_HEIGHT - RECTANGLE_PADDING + random.nextInt(3);
        double y1 = random.nextInt(2) == 1 ? y1_top : y1_bottom;


        double x2_right = CUT_WIDTH - RECTANGLE_PADDING - circleR + random.nextInt(2 * circleR - 4);
        double x2_left = RECTANGLE_PADDING + circleR - 2 - random.nextInt(2 * circleR - 4);
        double x2 = random.nextInt(2) == 1 ? x2_right : x2_left;
        double y2 = RECTANGLE_PADDING + (CUT_HEIGHT - 2 * RECTANGLE_PADDING) / 2.0 - 4 + random.nextInt(10);

        double po = Math.pow(circleR, 2);
        for (int i = 0; i < CUT_WIDTH; i++) {
            for (int j = 0; j < CUT_HEIGHT; j++) {
                //矩形区域
                boolean fill;
                if ((i >= RECTANGLE_PADDING && i < CUT_WIDTH - RECTANGLE_PADDING)
                        && (j >= RECTANGLE_PADDING && j < CUT_HEIGHT - RECTANGLE_PADDING)) {
                    data[i][j] = 1;
                    fill = true;
                } else {
                    data[i][j] = 0;
                    fill = false;
                }
                //凸出区域
                double d3 = Math.pow(i - x1, 2) + Math.pow(j - y1, 2);
                if (d3 < po) {
                    data[i][j] = 1;
                } else {
                    if (!fill) {
                        data[i][j] = 0;
                    }
                }
                //凹进区域
                double d4 = Math.pow(i - x2, 2) + Math.pow(j - y2, 2);
                if (d4 < po) {
                    data[i][j] = 0;
                }
            }
        }
        //边界阴影
        for (int i = 0; i < CUT_WIDTH; i++) {
            for (int j = 0; j < CUT_HEIGHT; j++) {
                //四个正方形边角处理
                for (int k = 1; k <= SLIDER_IMG_OUT_PADDING; k++) {
                    //左上、右上
                    if (i >= RECTANGLE_PADDING - k && i < RECTANGLE_PADDING
                            && ((j >= RECTANGLE_PADDING - k && j < RECTANGLE_PADDING)
                            || (j >= CUT_HEIGHT - RECTANGLE_PADDING - k && j < CUT_HEIGHT - RECTANGLE_PADDING +1))) {
                        data[i][j] = 2;
                    }

                    //左下、右下
                    if (i >= CUT_WIDTH - RECTANGLE_PADDING + k - 1 && i < CUT_WIDTH - RECTANGLE_PADDING + 1) {
                        for (int n = 1; n <= SLIDER_IMG_OUT_PADDING; n++) {
                            if (((j >= RECTANGLE_PADDING - n && j < RECTANGLE_PADDING)
                                    || (j >= CUT_HEIGHT - RECTANGLE_PADDING - n && j <= CUT_HEIGHT - RECTANGLE_PADDING ))) {
                                data[i][j] = 2;
                            }
                        }
                    }
                }

                if (data[i][j] == 1 && j - SLIDER_IMG_OUT_PADDING > 0 && data[i][j - SLIDER_IMG_OUT_PADDING] == 0) {
                    data[i][j - SLIDER_IMG_OUT_PADDING] = 2;
                }
                if (data[i][j] == 1 && j + SLIDER_IMG_OUT_PADDING > 0 && j + SLIDER_IMG_OUT_PADDING < CUT_HEIGHT && data[i][j + SLIDER_IMG_OUT_PADDING] == 0) {
                    data[i][j + SLIDER_IMG_OUT_PADDING] = 2;
                }
                if (data[i][j] == 1 && i - SLIDER_IMG_OUT_PADDING > 0 && data[i - SLIDER_IMG_OUT_PADDING][j] == 0) {
                    data[i - SLIDER_IMG_OUT_PADDING][j] = 2;
                }
                if (data[i][j] == 1 && i + SLIDER_IMG_OUT_PADDING > 0 && i + SLIDER_IMG_OUT_PADDING < CUT_WIDTH && data[i + SLIDER_IMG_OUT_PADDING][j] == 0) {
                    data[i + SLIDER_IMG_OUT_PADDING][j] = 2;
                }
            }
        }
        return data;
    }

    /**
     * 裁剪区块
     * 根据生成的滑块形状,对原图和裁剪块进行变色处理
     * @param oriImage    原图
     * @param targetImage 裁剪图
     * @param blockImage  滑块
     * @param x           裁剪点x
     * @param y           裁剪点y
     */
    private static void cutImgByTemplate(BufferedImage oriImage, BufferedImage targetImage, int[][] blockImage, int x, int y) {
        for (int i = 0; i < CUT_WIDTH; i++) {
            for (int j = 0; j < CUT_HEIGHT; j++) {
                int _x = x + i;
                int _y = y + j;
                int rgbFlg = blockImage[i][j];
                int rgb_ori = oriImage.getRGB(_x, _y);
                // 原图中对应位置变色处理
                if (rgbFlg == 1) {
                    //抠图上复制对应颜色值
                    targetImage.setRGB(i,j, rgb_ori);
                    //原图对应位置颜色变化
                    oriImage.setRGB(_x, _y, Color.LIGHT_GRAY.getRGB());
                } else if (rgbFlg == 2) {
                    targetImage.setRGB(i, j, Color.WHITE.getRGB());
                    oriImage.setRGB(_x, _y, Color.GRAY.getRGB());
                }else if(rgbFlg == 0){
                    //int alpha = 0;
                    targetImage.setRGB(i, j, rgb_ori & 0x00ffffff);
                }
            }

        }
    }


    /**
     * 随机获取一张图片对象
     * @param path
     * @return
     * @throws IOException
     */
    public static BufferedImage getRandomImage(String path) throws IOException {
        File files = new File(path);
        File[] fileList = files.listFiles();
        List<String> fileNameList = new ArrayList<>();
        if (fileList!=null && fileList.length!=0){
            for (File tempFile:fileList){
                if (tempFile.isFile() && tempFile.getName().endsWith(".jpg")){
                    fileNameList.add(tempFile.getAbsolutePath().trim());
                }
            }
        }
        Random random = new Random();
        File imageFile = new File(fileNameList.get(random.nextInt(fileNameList.size())));
        return ImageIO.read(imageFile);
    }

    /**
     * 将IMG输出为文件
     * @param image
     * @param file
     * @throws Exception
     */
    public static void writeImg(BufferedImage image, String file) throws Exception {
        byte[] imagedata = null;
        ByteArrayOutputStream bao=new ByteArrayOutputStream();
        ImageIO.write(image,"png",bao);
        imagedata = bao.toByteArray();
        FileOutputStream out = new FileOutputStream(new File(file));
        out.write(imagedata);
        out.close();
    }

    /**
     * 将图片转换为BASE64
     * @param image
     * @return
     * @throws IOException
     */
    public static String getImageBASE64(BufferedImage image) throws IOException {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        ImageIO.write(image,"png",out);
        //转成byte数组
        byte[] bytes = out.toByteArray();
        BASE64Encoder encoder = new BASE64Encoder();
        //生成BASE64编码
        return encoder.encode(bytes);
    }

    /**
     * 将BASE64字符串转换为图片
     * @param base64String
     * @return
     */
    public static BufferedImage base64StringToImage(String base64String) {
        try {
            BASE64Decoder decoder=new BASE64Decoder();
            byte[] bytes1 = decoder.decodeBuffer(base64String);
            ByteArrayInputStream bais = new ByteArrayInputStream(bytes1);
            return ImageIO.read(bais);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    public static boolean GenerateImage(String imgData, String imgFilePath) throws IOException { // 对字节数组字符串进行Base64解码并生成图片
        if (imgData == null) // 图像数据为空
            return false;
        BASE64Decoder decoder = new BASE64Decoder();
        OutputStream out = null;
        try {
            out = new FileOutputStream(imgFilePath);
            // Base64解码
            byte[] b = decoder.decodeBuffer(imgData);
            for (int i = 0; i < b.length; ++i) {
                if (b[i] < 0) {// 调整异常数据
                    b[i] += 256;
                }
            }
            out.write(b);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } finally {
            out.flush();
            out.close();
            return true;
        }
    }

    public static void main(String[] args) throws IOException {

        BufferedImage srcImage = null;
        try {
            srcImage = ImageIO.read(new File("C:\\Users\\Administrator\\Pictures\\11\\11.jpg"));
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(srcImage.toString());
    }
}

滑块图片包:

滑块验证码图片包,测试用-Java文档类资源-CSDN下载

滑块验证码是一种常见的人机交互验证方式,主要用于防止恶意攻击和机器人攻击。下面是一个简单的Java实现滑块验证码的示例代码: 1. 首先,需要在前端页面上实现一个滑块组件,并在后台生成一个随机的验证码图片。 2. 然后,用户需要按住滑块并将其拖动到正确的位置,以验证自己是一个真正的人类用户。 3. 在后台,需要验证用户拖动滑块的位置是否正确,以确保用户通过了验证。 下面是一个基于Spring Boot框架的简单示例代码: 1. 在前端页面中添加如下代码: ```html <div class="slider-container"> <div class="slider-background"></div> <div class="slider-handle"></div> </div> ``` 2. 在后台代码中,需要生成一个随机的验证码图片,并将验证码信息保存在Session中,以便后续验证。以下是一个简单的验证码生成器示例代码: ```java import java.awt.*; import java.awt.image.BufferedImage; import java.util.Random; public class CaptchaGenerator { private static final int IMAGE_WIDTH = 200; private static final int IMAGE_HEIGHT = 80; private static final int LINE_COUNT = 20; private static final int CHAR_COUNT = 4; private static final String CHAR_SET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; private static final int CHAR_SPACE = 20; private static final int CHAR_FONT_SIZE = 50; public static BufferedImage generate(String captcha) { BufferedImage image = new BufferedImage(IMAGE_WIDTH, IMAGE_HEIGHT, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, IMAGE_WIDTH, IMAGE_HEIGHT); g.setColor(Color.BLACK); Random random = new Random(); for (int i = 0; i < LINE_COUNT; i++) { int x1 = random.nextInt(IMAGE_WIDTH); int y1 = random.nextInt(IMAGE_HEIGHT); int x2 = random.nextInt(IMAGE_WIDTH); int y2 = random.nextInt(IMAGE_HEIGHT); g.drawLine(x1, y1, x2, y2); } Font font = new Font("Arial", Font.BOLD, CHAR_FONT_SIZE); g.setFont(font); int x = (IMAGE_WIDTH - CHAR_COUNT * CHAR_FONT_SIZE - (CHAR_COUNT - 1) * CHAR_SPACE) / 2; int y = (IMAGE_HEIGHT - CHAR_FONT_SIZE) / 2 + CHAR_FONT_SIZE; for (int i = 0; i < captcha.length(); i++) { char c = captcha.charAt(i); g.drawString(String.valueOf(c), x, y); x += CHAR_FONT_SIZE + CHAR_SPACE; } g.dispose(); return image; } public static String generateCaptcha() { Random random = new Random(); StringBuilder sb = new StringBuilder(); for (int i = 0; i < CHAR_COUNT; i++) { char c = CHAR_SET.charAt(random.nextInt(CHAR_SET.length())); sb.append(c); } return sb.toString(); } } ``` 3. 在Controller中,需要处理滑块验证请求,并进行验证码验证。以下是一个简单的Controller示例代码: ```java import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.*; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import java.awt.image.BufferedImage; import java.io.IOException; @Controller public class CaptchaController { @GetMapping("/captcha") public void captcha(HttpServletRequest request, HttpServletResponse response) throws IOException { String captcha = CaptchaGenerator.generateCaptcha(); BufferedImage image = CaptchaGenerator.generate(captcha); HttpSession session = request.getSession(); session.setAttribute("captcha", captcha); response.setContentType("image/png"); response.getOutputStream().write(ImageUtil.toByteArray(image)); } @PostMapping("/captcha/verify") @ResponseBody public boolean verify(@RequestParam String captcha, HttpSession session) { String expectedCaptcha = (String) session.getAttribute("captcha"); return captcha.equals(expectedCaptcha); } } ``` 以上是一个简单的Java实现滑块验证码的示例代码。为了实现更好的安全性,实际应用中需要进一步加强验证机制,例如添加时间限制、IP限制等。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

wcybaonier

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值