SSM登录-验证码

3 篇文章 0 订阅
1 篇文章 0 订阅

这个验证码方法只要添加一个jar,在到登录方法里插入验证码代码就可以了,方法简单

  1. 下载jar
    在这里插入图片描述
    百度网盘下载:hutool-all-4.6.1 提取码:0ssc
  2. 创一个 utils包 在创一个类TestHuTool
package com.zx.Exprot_utils;

import cn.hutool.captcha.CaptchaUtil;
import cn.hutool.captcha.LineCaptcha;
/**
 * 生存验证码
 * @author Administrator
 *
 */
public class TestHuTool {

	public static void main(String[] args) {
		// 定义图形验证码的长和宽
		LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(200, 100);
		
		// 图形验证码写出,可以写出到文件,也可以写出到流
		lineCaptcha.write("C:/line.png");//也可以把在验证码放到一个盘里
		System.out.println(lineCaptcha.getCode());//输出
	}
	
	
}

  1. 在登录方法里写验证码
    先得到验证码:
/**
	 * 得到登陆验证码
	 * @param response
	 * @param session
	 * @throws IOException
	 */
	@RequestMapping("getCode")
	public void getCode(HttpServletResponse response,HttpSession session) throws IOException {
		// 定义图形验证码的长和宽
		LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36,4,5);//高宽和清晰度
		session.setAttribute("code", lineCaptcha.getCode());
		ServletOutputStream outputStream = response.getOutputStream();
		ImageIO.write(lineCaptcha.getImage(), "JPEG", outputStream);
	}

登录放法里插入验证码 代码

/**
	 * 登录方法
	 * @param sysUserVo
	 * @param model
	 * @return
	 */
	@RequestMapping("/loginuser")
	public String login(SysUserVo sysUserVo,Model model){
		String code=WebUtils.getHttpSession().getAttribute("code").toString();//验证码
		if(sysUserVo.getCode().equals(code)) {//判断验证码是否正确
			SysUser sysUser=this.sysUserService.login(sysUserVo);
			if (sysUser!=null) {
				//放到session
				WebUtils.getHttpSession().setAttribute("sysuser", sysUser);
				
				//记录登陆日志 向sys_login_log里面插入数据
				LoginfoVo logInfoVo=new LoginfoVo();
				logInfoVo.setLogintime(new Date());//插入当前时间
				logInfoVo.setLoginname(sysUser.getRealname()+"-"+sysUser.getLoginname());//用session插入这两个名称
				logInfoVo.setLoginip(WebUtils.getHttpServletRequest().getRemoteAddr());//插入IP地址
				
				sysLoginfoService.addLogInfo(logInfoVo);
				return "/carrent/login/index";
			} else {
				model.addAttribute("error", SysConstast.USER_LOGIN_ERROR_MSG);
				return "/carrent/login/login";
			}
		}else {
			model.addAttribute("error", SysConstast.USER_LOGIN_CODE_ERROR_MSG);
			return "/carrent/login/login";//判断验证码不正确反回
		} 
	}

图形参考:
在这里插入图片描述

  1. VO里添加 code
package com.zx.vo;

import com.zx.po.SysUser;

public class SysUserVo extends SysUser{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	/**
	 * 分页参数
	 */
	private Integer page;
	private Integer limit;
	private String code;
	
	public String getCode() {
		return code;
	}
	public void setCode(String code) {
		this.code = code;
	}
	
	
	//接收多个角色id 用于批量删除
	private Integer [] ids;
	
	public Integer[] getIds() {
		return ids;
	}
	public void setIds(Integer[] ids) {
		this.ids = ids;
	}
	public Integer getPage() {
		return page;
	}
	public void setPage(Integer page) {
		this.page = page;
	}
	public Integer getLimit() {
		return limit;
	}
	public void setLimit(Integer limit) {
		this.limit = limit;
	}
}

在这里插入图片描述

  1. Jsp页面
<div class="layui-form-item input-item" id="imgCode">
	<label for="code">验证码</label>
	<input type="text" placeholder="请输入验证码"  autocomplete="off" id="code" name="code"  class="layui-input" lay-verify="required">
	<img src="${ctx}/login/getCode.do" onclick="this.src=this.src+'?'">
</div>
  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
SSM(Spring+SpringMVC+MyBatis)登录验证码的实现步骤如下: 1. 在登录页面上添加一个验证码输入框和一个验证码图片; 2. 随机生成一个验证码,并将其存储到 session 中; 3. 将验证码图片输出到页面上,并将验证码图片的 URL 存储到 session 中; 4. 用户输入验证码后,将其与 session 中的验证码进行比对; 5. 如果比对成功,则继续进行登录操作;否则,提示用户验证码错误。 下面是一个简单的示例代码: 验证码生成器: ```java public class VerifyCodeUtils { private static final String VERIFY_CODES = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"; public static String generateVerifyCode(int verifySize) { StringBuilder sb = new StringBuilder(); Random random = new Random(); for (int i = 0; i < verifySize; i++) { sb.append(VERIFY_CODES.charAt(random.nextInt(VERIFY_CODES.length()))); } return sb.toString(); } public static BufferedImage generateImage(String verifyCode) { int width = 100; int height = 40; BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, width, height); g.setColor(Color.BLACK); g.setFont(new Font("Arial", Font.BOLD, 20)); for (int i = 0; i < verifyCode.length(); i++) { g.drawString(String.valueOf(verifyCode.charAt(i)), 20 * i + 10, 25); } g.dispose(); return image; } } ``` 登录页面: ```html <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title>登录页面</title> </head> <body> <form action="${pageContext.request.contextPath}/login" method="post"> <label>用户名:</label> <input type="text" name="username"><br> <label>密码:</label> <input type="password" name="password"><br> <label>验证码:</label> <input type="text" name="verifyCode"> <img src="${pageContext.request.contextPath}/verifyCode" onclick="this.src='${pageContext.request.contextPath}/verifyCode?time='+new Date().getTime()"><br> <input type="submit" value="登录"> </form> </body> </html> ``` 登录控制器: ```java @Controller public class LoginController { @RequestMapping("/login") public String login(String username, String password, String verifyCode, HttpSession session) { String realVerifyCode = (String) session.getAttribute("verifyCode"); if (!realVerifyCode.equalsIgnoreCase(verifyCode)) { return "redirect:/login"; } // 进行登录操作 return "redirect:/index"; } @RequestMapping("/verifyCode") public void verifyCode(HttpSession session, HttpServletResponse response) throws IOException { // 生成验证码 String verifyCode = VerifyCodeUtils.generateVerifyCode(4); session.setAttribute("verifyCode", verifyCode); // 输出验证码图片 BufferedImage image = VerifyCodeUtils.generateImage(verifyCode); response.setContentType("image/png"); OutputStream os = response.getOutputStream(); ImageIO.write(image, "png", os); os.flush(); os.close(); } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值