首页 > 编程语言 >Javaweb 登陆与验证码

Javaweb 登陆与验证码

时间:2022-12-21 22:33:57浏览次数:36  
标签:Javaweb random request 验证码 nextInt 登陆 import response

  本次记录分角色登陆以及验证码的Servlet。

1.登陆验证


<html>
<%--
Created by IntelliJ IDEA.
User: jiachenglin
Date: 2022/11/11
Time: 14:31
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<head>
<title>Title</title>
</head>

<html>
<head>
<title>顾客登陆</title>
<h1 style="text-align: center">学生登陆</h1>
</head>
<body>
<form action="${pageContext.request.contextPath}/StudentServlet" method="post">
<div style="color: red;text-align: center">${login_err}</div><br>//从Servlet中传回登陆错误信息
学号:<input type="text" id="userne" name="id" maxlength="20"><br> //maxlength是指此处最长输入20个字符
<span id="ne_err" style="color: red;display:none">输入有误</span><br>
<span id="ne_num" style="display: none">最长20位</span>
密码:<input type="password" id="pswd" name="password" maxlength="20"><br>
<span id="pswd_err" style="color: red;display:none">输入有误</span><br>
验证码:<input type="text" name="valistr">
<image src="code" id="identity" onl oad="btn.disable=false;" style="cursor:pointer; vertical-align:middle"/>
<input type="button" value="看不清,更换验证码" onclick="changeImageCode()" id="btn" ><br>
<div style="color: red">${yan}</div><br>
<input type="submit" value="登陆"><input type="reset" value="重制"><br>
<input type="hidden" name="method" value="login"><br>
<a href="index.jsp">点击返回</a>

</form>
<script type="text/javascript">
<!--验证码切换-->
function changeImageCode() {
document.getElementById('btn').isDisabled=true;
document.getElementById('identity').src='code?ts='+new Date().getTime();
}
let ne_num=document.getElementById("ne_num");
ne_num.onblur=function (){
let ne_num1=ne_num.value.trim();
if(ne_num1.length==20){
document.getElementById("ne_num").style.display='';
}else{
document.getElementById("ne_num").style.display='none';
}
}

</script>
</body>
</html>
___________________________________________________________________________________________________________________
Servlet内容
case "login":
String id = request.getParameter("id");
String password = request.getParameter("password");
String cher = request.getParameter("valistr");
String cher2 = request.getSession().getAttribute("randomCode").toString();
List<Student> list1 = studentDao.login(id, password);
if (list1.isEmpty()) {
request.setAttribute("login_err", "用户名或密码错误");
request.getRequestDispatcher("student.jsp").forward(request, response);
} else if (!cher.toLowerCase().equals(cher2.toLowerCase())) {//校验验证码
request.setAttribute("yan", "验证码不一致!");
request.getRequestDispatcher("student.jsp").forward(request, response);
} else {
request.setAttribute("user", id);
request.getRequestDispatcher("studentjm.jsp").forward(request, response);
}
break;
___________________________________________________________________________________________________________________

2.验证码Servlet
import javax.imageio.ImageIO;

import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Random;

@WebServlet(name = "VerifyCodeServlet", urlPatterns = "/code")
public class VerifyCodeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//防止浏览器缓存验证码
response.setDateHeader("Expires",-1);
response.setHeader("Cache-Control","no-Cache");
response.setHeader("pragma","no-Cache");
int width = 60;//验证码总宽度
int height = 20;//验证码总高度
// 随机数
char[] codeChar = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789".toCharArray();//字符串转换成字符数组
String captcha = "";//定义一个临时路径
Random random = new Random();//new 随机类Random
// 生成验证码图片
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
// 绘制矩形
Graphics2D graphics = image.createGraphics();//绘制环境对象
graphics.setColor(Color.WHITE);//背景色
graphics.fillRect(0, 0, width, height);//绘制矩形
Font font=new Font("Times New Roman",Font.PLAIN,18);
graphics.setFont(font);
//画边框
graphics.setColor(Color.BLACK);
graphics.drawRect(0,0,-1,-1);
//随机产生160条干扰线
graphics.setColor(Color.LIGHT_GRAY);
for(int i=0;i<160;i++){
int x=random.nextInt(width);
int y=random.nextInt(height);
int x1=random.nextInt(12);
int y1=random.nextInt(12);
graphics.drawLine(x,y,x+x1,y+y1);
}
//randomCode用于保存随机产生的验证码
StringBuffer randomCode=new StringBuffer();
int red=0,green=0,blue=0;
// 生成验证码图片
for (int i = 0; i < 4; i++) {
int strRand=random.nextInt(codeChar.length)+1;
String s= String.valueOf(codeChar[strRand]);
//产生随机的颜色分量来构造颜色值
red=random.nextInt(110);
green=random.nextInt(50);
blue=random.nextInt(50);
graphics.setColor(new Color(red,green,blue));
graphics.drawString(s,13*i+6,16);
randomCode.append(s);
// int index = random.nextInt(codeChar.length);
// // 设置验证码字体图片颜色
// graphics.setColor(new Color(random.nextInt(110), random.nextInt(150), random.nextInt(200)));
// // 每个验证码字体的宽间距,高度
// graphics.drawString(codeChar[index] + "", (i * 20) + 15, 20);
// captcha += codeChar[index];//把生成的验证码保存在临时变量中
}
//将四位验证码保存在session中
HttpSession session= request.getSession();
session.setAttribute("randomCode",randomCode.toString());
//防止浏览器缓存验证码
response.setDateHeader("Expires",-1);
response.setHeader("Cache-Control","no-Cache");
response.setHeader("pragma","no-Cache");
response.setContentType("image/jpeg");
ServletOutputStream sos=response.getOutputStream();
// request.getSession().setAttribute("codes", captcha); //把验证码保存在Session对象中
// 利用ImageI0输出验证码
ImageIO.write(image, "jpeg", sos);
sos.close();

}
}

标签:Javaweb,random,request,验证码,nextInt,登陆,import,response
From: https://www.cnblogs.com/joranger/p/16997382.html

相关文章

  • 机器学习————验证码图片识别
    (一)选题背景首先,验证码是最初的设定是通过验证码对人类和非人类行为进行区分;大多数的网站在进行注册或者登录时都需要用到图片验证码,这都是为了防止用户通过机器人......
  • 彩虹女神跃长空,Go语言进阶之Go语言高性能Web框架Iris项目实战-登录与图形验证码(capt
    书接上回,上一回我们按照“低耦合高内聚”的组织架构方针对项目的整体结构进行了优化,本回将会继续编写业务,那就是用户的登录逻辑,将之前用户管理模块中添加的用户账号进行账......
  • 验证码、MD5加密
    验证码生成验证码帮助类点击查看代码VerifyCodeServlet.javapackagecom.situ.web.servlet;importjava.awt.Color;importjava.awt.Font;importjava.awt.Graphic......
  • 【验证码逆向专栏】某片滑块、点选验证码逆向分析
    声明本文章中所有内容仅供学习交流使用,不用于其他任何目的,不提供完整代码,抓包内容、敏感网址、数据接口等均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后......
  • 【验证码逆向专栏】某片滑块、点选验证码逆向分析
    声明本文章中所有内容仅供学习交流使用,不用于其他任何目的,不提供完整代码,抓包内容、敏感网址、数据接口等均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切......
  • 前后分离 laravel对接验证码功能
    2022年12月19日16:30:52因为最近在做等保三级,之前接口只做了错误5次,就禁止一个小时登录,但是发现还是不好,这次添加验证码功能composerrequiremews/captcha找到config/......
  • 记录--教你用three.js写一个炫酷的3D登陆页面
    这里给大家分享我在网上总结出来的一些知识,希望对大家有所帮助前言:该篇文章用到的主要技术:vue3、three.js我们先看看成品效果:高清大图预览(会有些慢):座机小图预览:......
  • 编写代码实现用户登陆账号三次的情况
    详细叙述:编写代码实现,模拟用户登录情景,并且只能登录三次(只允许输入三次密码,如果密码正确则提示登录成功,如果三次均输入错误,则退出程序)答案:#include<stdio.h>#include<string......
  • WEB Service产生随机验证码图片
     WEB服务端方法:[WebMethod]publicbyte[]GenerateVerifyImage(intnLen,refstringstrKey){intnBmpWidth=13*nLen+5;i......
  • IDE登陆提示SSL证书过期&重建证书
        Rhapsody 引擎自己登陆的证书早期是5年过期一次,从6.7版本开始为2年过期一次【原因:由于Mac等Apple设备,无法再通过Mac上的Chrome访问管理控制台,因此我们......