使用Springboot3.x结合美学与功能的设计实现艺术风格验证码

2024年 5月 14日 49.0k 0

这个专题深入浅出地探讨了各类验证码的生成和在Springboot3.x中的实践,从基础的滑动、点选、算术运算验证码到创新的艺术风格、水印、二维码验证码,适合所有Java开发者阅读。在这个专题中,不仅可以学习到技术实践,更能领略到验证码的美学魅力。让我们一起探索验证码的无尽可能性。

什么是艺术风格验证码

验证码,全名叫做 Completely Automated Public Turing Test to tell Computers and Humans Apart(全自动区分计算机和人类的图灵测试)。其主要目标是阻止机器自动进行某些操作,例如注册用户、提交表单等。

而艺术风格验证码,可以看作是验证码的一种创新形式,它将数字艺术融入到这项安全措施中。艺术风格验证码的外观吸引人,增强了用户体验,同时也提高了验证码的安全等级。因为这种验证码在视觉上的差异性和复杂性使得对验证码的自动识别变得更加困难,提高了安全性。

所谓艺术风格,包括但不限于各种视觉艺术形式,例如流行艺术、抽象艺术、最小主义艺术等。验证码的颜色、形状、过滤效果等都可以根据特定的艺术风格来设计。例如,我们可能将验证码中的数字或字母渲染成流行艺术风格,或者给验证码背景添加抽象艺术元素。

艺术风格验证码的运行机制

艺术风格验证码的运行机制同普通验证码非常相似,但是它引入了额外的步骤来添加艺术效果。以下是其一般的工作流程:

  • 生成一组随机的字母或数字作为验证码的原始文本。
  • 为每个字符生成一个基本的图形表示,通常是在图片中为每个字符分配一个特定的位置并进行绘制。
  • 对生成的图片应用一系列艺术效果。这些效果可以包含颜色变换、模糊处理、波纹效果、旋转变形等。
  • 将完成艺术效果处理的验证码图片展示给用户,并存储原始的验证码文本以供用户提交后进行比对验证。
  • 下面通过一个基本的例子演示在Java环境下如何通过代码实现这个流程:

    public void generateArtisticVerificationCode() {
        String verificationCode = RandomStringUtils.randomAlphanumeric(5); // 生成原始验证码文本
    
        BufferedImage image = new BufferedImage(100, 40, BufferedImage.TYPE_INT_ARGB); // 创建图片对象
        Graphics graphics = image.getGraphics(); // 获取画布
        graphics.setFont(new Font("TimesRoman", Font.BOLD, 20)); // 设定字体
        graphics.setColor(Color.BLACK); // 设定颜色
    
        for (int i = 0; i < verificationCode.length(); i++) {
            graphics.drawString(verificationCode.charAt(i) + "", 10 + i * 16, 28); // 在画布上绘制每个字符
        }
    
        applyArtisticEffects(image); // 应用艺术效果
    
        // 将图片展示给用户,同时保留原始验证码文本以供后续验证
    }
    
    // 示例艺术效果应用函数
    public void applyArtisticEffects(BufferedImage image) {
        // 这个函数会对图片应用各种艺术效果,包括但不限于颜色变换、模糊处理、波纹效果等
        // 具体实现取决于你希望生成验证码的艺术风格
    }

    在生成艺术风格验证码的过程中,我们首先生成了原始的验证码文本,并为每一个字符在图片上绘制了基本的图形表示。然后我们对图片应用了艺术效果处理。最后我们将处理过的验证码图片展示给用户,并保留原始的验证码文本,这样用户在提交时我们就可以对提交的验证码和原始的进行比对。

    技术实现:在Springboot3.x中如何生成艺术风格验证码

    在Springboot3.x中生成艺术风格验证码,我们主要分为以下几步:

  • 创建验证码Controller
  • 实现一个验证码服务
  • 实现一个艺术效果应用服务
  • 以下是详细的实现步骤和示例代码:

    创建验证码Controller

    首先,我们需要创建一个Controller用于处理验证码相关的请求。这个Controller将和我们的验证码服务进行交互,接收用户请求并返回生成的验证码。

    @RestController
    public class VerificationCodeController {
    
        @Autowired
        private VerificationCodeService verificationCodeService;
    
        @RequestMapping("/verificationCode")
        public void getVerificationCode(HttpServletResponse response, HttpSession session) {
            BufferedImage image = verificationCodeService.createVerificationImage();
            session.setAttribute("VERIFICATION_CODE", verificationCodeService.getVerificationCode());
            ImageIO.write(image, "jpeg", response.getOutputStream());
        }
    }

    在上述代码中,我们创建了一个名为VerificationCodeController的Controller。我们注入了VerificationCodeService用于生成验证码。我们定义了一个路由/verificationCode,用于接收HTTP请求并返回生成的验证码图片。

    实现验证码服务

    验证码服务的责任是生成原始的验证码文本和验证码图片。

    @Service
    public class VerificationCodeService {
    
        @Autowired
        private ArtisticEffectService artisticEffectService;
    
        private String verificationCode;
    
        public BufferedImage createVerificationImage() {
            verificationCode = RandomStringUtils.randomAlphanumeric(5);
    
            BufferedImage image = new BufferedImage(100, 40, BufferedImage.TYPE_INT_ARGB);
            Graphics graphics = image.getGraphics();
            graphics.setFont(new Font("TimesRoman", Font.BOLD, 20));
            graphics.setColor(Color.BLACK);
    
            for (int i = 0; i < verificationCode.length(); i++) {
                graphics.drawString(verificationCode.charAt(i) + "", 10 + i * 16, 28);
            }
    
            artisticEffectService.applyArtisticEffects(image);
    
            return image;
        }
    
        public String getVerificationCode() {
            return verificationCode;
        }
    }

    实现艺术效果应用服务

    艺术效果应用服务用于对验证码图片应用艺术效果。

    @Service
    public class import java.awt.AlphaComposite;
    import java.awt.Color;
    import java.awt.GradientPaint;
    import java.awt.Graphics2D;
    import java.awt.image.BufferedImage;
    import java.awt.image.ConvolveOp;
    import java.awt.image.Kernel;
    import org.springframework.stereotype.Service;
    
    @Service
    public class ArtisticEffectService {
    
        public void applyArtisticEffects(BufferedImage image) {
            Graphics2D graphics = (Graphics2D) image.getGraphics();
    
            // 添加线性渐变效果
            GradientPaint paint = new GradientPaint(0, 0, Color.BLUE, image.getWidth(), 0, Color.RED);
            graphics.setPaint(paint);
            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, 0.5f));
            graphics.fillRect(0, 0, image.getWidth(), image.getHeight());
    
            // 添加模糊效果
            float ninth = 1.0f/9.0f;
            float[] blurKernel = {
                ninth, ninth, ninth,
                ninth, ninth, ninth,
                ninth, ninth, ninth
            };
            ConvolveOp op = new ConvolveOp(new Kernel(3, 3, blurKernel));
            BufferedImage blurredImage = op.filter(image, null);
            graphics.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER));
            graphics.drawImage(blurredImage, 0, 0, null);
        }
    }

    在上述代码中,我们首先使用GradientPaint创建了一个从左边的蓝色向右边的红色逐渐变化的线性渐变效果,然后使用AlphaComposite将这个渐变效果和原来的图像合成在一起。

    接着,我们创建了一个模糊核(blur kernel)并使用ConvolveOp将模糊效果应用到现有的图像上。

    实战应用:艺术风格验证码的应用示例

    在接下来的示例中,我们将实现一个功能更为完善的Spring Boot应用程序,该程序包含一个Web页面,用户可以从该页面请求新的艺术风格验证码,并提交输入以进行验证。

    以下是我们的应用程序的主要组件:

  • 验证码生成服务
  • Web控制器
  • Vue.js前端应用
  • 验证码生成服务

    我们先前已经实现了一个验证码服务和艺术效果服务,现在我们可以将其集成到我们的Spring Boot应用中。

    @Service
    public class VerificationCodeService {
    
        private String verificationCode;
    
        @Autowired
        private ArtisticEffectService artisticEffectService;
    
        public String createVerificationCode() {
            verificationCode = RandomStringUtils.randomAlphanumeric(5);
            return verificationCode;
        }
    
        public BufferedImage createVerificationImage() {
            String code = createVerificationCode();
            BufferedImage image = new BufferedImage(100, 40, BufferedImage.TYPE_INT_ARGB);
    
            Graphics graphics = image.getGraphics();
            graphics.setFont(new Font("Arial", Font.BOLD, 24));
            graphics.setColor(Color.BLACK);
    
            for (int i = 0; i < verificationCode.length(); i++) {
                graphics.drawString(code.charAt(i) + "", 10 + i * 16, 32);
            }
    
            artisticEffectService.applyArtisticEffects(image);
    
            return image;
        }
    
        public boolean verifyCode(String userInput) {
            return userInput.equals(verificationCode);
        }
    }

    在这里,我们将为这个方法实现倾斜角度变化和图片抖动这两种常见的艺术样式。

    import java.awt.Color;
    import java.awt.Font;
    import java.awt.FontMetrics;
    import java.awt.Graphics2D;
    import java.awt.RenderingHints;
    import java.awt.image.BufferedImage;
    import java.util.Random;
    import org.springframework.stereotype.Service;
    
    @Service
    public class ArtisticEffectService {
    
        // 旋转给定的图像
        public BufferedImage rotateImage(BufferedImage image) {
            int width = image.getWidth();
            int height = image.getHeight();
    
            BufferedImage rotatedImage = new BufferedImage(width, height, image.getType());
            Graphics2D graphics2D = rotatedImage.createGraphics();
    
            double theta = Math.toRadians(new Random().nextInt(40) - 20);  // 在-20到20度之间随机旋转
            graphics2D.rotate(theta, width / 2, height / 2);
            graphics2D.drawImage(image, 0, 0, null);
            graphics2D.dispose();
    
            return rotatedImage;
        }
    
        // 对给定的字符串应用底噪音和干扰线
        public BufferedImage applyArtisticEffects(BufferedImage image) {
            int width = image.getWidth();
            int height = image.getHeight();
    
            Random random = new Random();
    
            // 底部噪声
            for (int i = 0; i < 30; i++) {
                int x = random.nextInt(width);
                int y = random.nextInt(height);
                int rgb = getRandomRgb();
                image.setRGB(x, y, rgb);
            }
    
            // 干扰线
            Graphics2D graphics2D = image.createGraphics();
            for (int i = 0; i  {
              if (response.data) {
                alert("The captcha is correct.");
              } else {
                alert("The captcha is incorrect.");
                this.refreshCaptcha();
              }
            });
        }
      },
      mounted() {
        this.refreshCaptcha();
      }
    }
    

    在上述代码中,我们使用Vue.js提供的两个生命周期钩子:methods中的refreshCaptcha方法获取新的验证码,mounted中的refreshCaptcha在页面加载时调用。在验证码提交后,一个警告会告诉用户提交的验证码是否正确。如果验证码不正确,将会刷新新的验证码。

    通过这种方式,我们成功创建了一个艺术风格验证码的完整应用示例,包含后端的验证码生成、前端的验证码展示和用户输入验证等完整流程。

    本文主要介绍了如何实现一个艺术风格的验证码系统,过程包含生成验证码、应用艺术效果、及其在前后端的实现。验证码生成部分,通过Java的RandomStringUtils工具生成随机字符串作为验证码。艺术效果应用部分,实现了噪点扰动和模糊效果,来增强验证码的安全性同时赋予其独特的艺术风格。在后端,我们创建了一个Spring Boot应用,实现了验证码的生成并返回给前端;在前端部分,我们使用Vue.js创建了一个用户界面单元,用户可以进行验证码的获取与输入验证。这样的系统结构使得验证码的生成及验证过程更为灵活与高效。

    相关文章

    JavaScript2024新功能:Object.groupBy、正则表达式v标志
    PHP trim 函数对多字节字符的使用和限制
    新函数 json_validate() 、randomizer 类扩展…20 个PHP 8.3 新特性全面解析
    使用HTMX为WordPress增效:如何在不使用复杂框架的情况下增强平台功能
    为React 19做准备:WordPress 6.6用户指南
    如何删除WordPress中的所有评论

    发布评论