博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
登录界面验证码的实现
阅读量:3946 次
发布时间:2019-05-24

本文共 7175 字,大约阅读时间需要 23 分钟。

文章目录

Javaweb实现验证码

前端

添加样式

FM_blog

编写代码

锋芒博客登录

配置web.xml文件

com.FM.servlet
com.FM.servlet.ValidationCodeServlet
ValidationCodeServlet
com.FM.servlet.ValidationCodeServlet
ValidationCodeServlet
/ValidationCodeServlet

后台

package com.FM.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.imageio.ImageIO;import javax.servlet.http.HttpSession;import java.awt.*;import java.awt.image.BufferedImage;import java.io.OutputStream;import java.util.Random;public class ValidationCodeServlet extends HttpServlet {
private static final long serialVersionUID = 1L; public ValidationCodeServlet() {
super(); } protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//获得验证码集合的长度 int charsLength = codeChars.length(); //下面3条记录是关闭客户端浏览器的缓冲区 //这3条语句都可以关闭浏览器的缓冲区,但是由于浏览器的版本不同,对这3条语句的支持也不同 //因此,为了保险起见,同时使用这3条语句来关闭浏览器的缓冲区 resp.setHeader("ragma", "No-cache"); resp.setHeader("Cache-Control", "no-cache"); resp.setDateHeader("Expires", 0); //设置图形验证码的长和宽 int width = 90, height = 30; BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB); Graphics g = image.getGraphics(); //获得用于输出文字的Graphics对象 Random random = new Random(); g.setColor(getRandomColor(180, 250)); g.fillRect(0, 0, width, height); g.setFont(new Font("Times New Roman",Font.ITALIC,height)); g.setColor(getRandomColor(120, 180)); //用户保存最后随机生成的验证码 StringBuilder validationCode = new StringBuilder(); //验证码的随机字体 String[] fontNames = {
"Times New Roman","Book antiqua","Arial"}; //随机生成4个验证码 for(int i = 0; i < 4; i++){
//随机设置当前验证码的字符的字体 g.setFont(new Font(fontNames[random.nextInt(3)],Font.ITALIC,height)); //随机获得当前验证码的字符 char codeChar = codeChars.charAt(random.nextInt(charsLength)); validationCode.append(codeChar); //随机设置当前验证码字符的颜色 g.setColor(getRandomColor(10, 100)); //在图形上输出验证码字符,x和y都是随机生成的 g.drawString(String.valueOf(codeChar), 16*i + random.nextInt(7), height-random.nextInt(6)); } //获得HttpSession对象 HttpSession session = req.getSession(); //设置session对象5分钟失效 session.setMaxInactiveInterval(5*60); //将验证码保存在session对象中,key为validation_code session.setAttribute("validation_code", validationCode.toString()); //关闭Graphics对象 g.dispose(); OutputStream outS = resp.getOutputStream(); ImageIO.write(image, "JPEG", outS); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response); } //图形验证码的字符集,系统将随机从这个字符串中选择一些字符作为验证码 private static String codeChars = "123456789abcdefghijklmnpqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ"; //返回一个随机颜色 private static Color getRandomColor(int minColor, int maxColor){
Random random = new Random(); if(minColor > 255){
minColor = 255; } if(maxColor > 255){
maxColor = 255; } //获得r的随机颜色值 int red = minColor + random.nextInt(maxColor-minColor); //g int green = minColor + random.nextInt(maxColor-minColor); //b int blue = minColor + random.nextInt(maxColor-minColor); return new Color(red,green,blue); }}

效果图:

在这里插入图片描述

Springboot添加验证码

项目结构

在这里插入图片描述

依赖

com.github.penggle
kaptcha
2.3.2

控制类

CommonController

package com.habse.car_position.Controller;import com.google.code.kaptcha.impl.DefaultKaptcha;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Component;import org.springframework.stereotype.Controller;import org.springframework.web.bind.annotation.GetMapping;import javax.imageio.ImageIO;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.awt.image.BufferedImage;import java.io.ByteArrayOutputStream;/*验证码 */@Controllerpublic class CommonController {
@Autowired private DefaultKaptcha captchaProducer; @GetMapping("/common/kaptcha") public void defaultKaptcha(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) throws Exception {
byte[] captchaOutputStream = null; ByteArrayOutputStream imgOutputStream = new ByteArrayOutputStream(); try {
//生产验证码字符串并保存到session中 String verifyCode = captchaProducer.createText(); httpServletRequest.getSession().setAttribute("verifyCode", verifyCode); BufferedImage challenge = captchaProducer.createImage(verifyCode); ImageIO.write(challenge, "jpg", imgOutputStream); } catch (IllegalArgumentException e) {
httpServletResponse.sendError(HttpServletResponse.SC_NOT_FOUND); return; } captchaOutputStream = imgOutputStream.toByteArray(); httpServletResponse.setHeader("Cache-Control", "no-store"); httpServletResponse.setHeader("Pragma", "no-cache"); httpServletResponse.setDateHeader("Expires", 0); httpServletResponse.setContentType("image/jpeg"); ServletOutputStream responseOutputStream = httpServletResponse.getOutputStream(); responseOutputStream.write(captchaOutputStream); responseOutputStream.flush(); responseOutputStream.close(); }}

KaptchaConfig

package com.habse.car_position.config;import com.google.code.kaptcha.impl.DefaultKaptcha;import com.google.code.kaptcha.util.Config;import org.springframework.context.annotation.Bean;import org.springframework.stereotype.Component;import java.util.Properties;@Componentpublic class KaptchaConfig {
@Bean public DefaultKaptcha getDefaultKaptcha(){
DefaultKaptcha defaultKaptcha = new DefaultKaptcha(); Properties properties = new Properties(); properties.put("kaptcha.border", "no"); properties.put("kaptcha.textproducer.font.color", "blue"); properties.put("kaptcha.image.width", "140"); properties.put("kaptcha.image.height", "40"); properties.put("kaptcha.textproducer.font.size", "30"); properties.put("kaptcha.session.key", "verifyCode"); properties.put("kaptcha.textproducer.char.space", "5"); Config config = new Config(properties); defaultKaptcha.setConfig(config); return defaultKaptcha; }}

前端页面

效果图

在这里插入图片描述

转载地址:http://weawi.baihongyu.com/

你可能感兴趣的文章
JavaScript的一些基础-数据类型
查看>>
JavaScript基础知识(2)
查看>>
转载一个webview开车指南以及实际项目中的使用
查看>>
android中对于非属性动画的整理
查看>>
一个简单的TabLayout的使用
查看>>
ReactNative使用Redux例子
查看>>
Promise的基本使用
查看>>
coursesa课程 Python 3 programming 统计文件有多少单词
查看>>
coursesa课程 Python 3 programming 输出每一行句子的第三个单词
查看>>
Returning a value from a function
查看>>
coursesa课程 Python 3 programming Functions can call other functions 函数调用另一个函数
查看>>
coursesa课程 Python 3 programming The while Statement
查看>>
course_2_assessment_6
查看>>
coursesa课程 Python 3 programming course_2_assessment_7 多参数函数练习题
查看>>
coursesa课程 Python 3 programming course_2_assessment_8 sorted练习题
查看>>
在unity中建立最小的shader(Minimal Shader)
查看>>
1.3 Debugging of Shaders (调试着色器)
查看>>
关于phpcms中模块_tag.class.php中的pc_tag()方法的含义
查看>>
vsftp 配置具有匿名登录也有系统用户登录,系统用户有管理权限,匿名只有下载权限。
查看>>
linux安装usb wifi接收器
查看>>