使用HuTool工具类,实现登录验证码
发布日期:2021-06-28 14:04:05 浏览次数:3 分类:技术文章

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

1.首先引入HuTool的依赖包

cn.hutool
hutool-all
4.6.8

2.登录页面

3.LoginController.java 登录控制器

package com.wang.springboot.sys.controller;import cn.hutool.captcha.CaptchaUtil;import cn.hutool.captcha.LineCaptcha;import com.wang.springboot.sys.common.ActiveUser;import com.wang.springboot.sys.common.ResultObj;import com.wang.springboot.sys.common.WebUtils;import com.wang.springboot.sys.domain.User;import org.apache.shiro.SecurityUtils;import org.apache.shiro.authc.AuthenticationException;import org.apache.shiro.authc.AuthenticationToken;import org.apache.shiro.authc.UsernamePasswordToken;import org.apache.shiro.subject.Subject;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RestController;import javax.servlet.ServletOutputStream;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;import javax.swing.*;import java.io.IOException;/** * @author 王一宁 * @since 2019-11-21 */@RestController@RequestMapping("/login")public class LoginController {    @RequestMapping("/login")    public ResultObj login(String loginname, String pwd, String code,HttpSession session){        //获得存储在session中的验证码        String sessionCheckCode = (String) session.getAttribute("code");        //判断验证码是否正确        if (code!=null && sessionCheckCode.equals(code)){			//登录成功,返回json的提示。             return ResultObj.LOGIN_SUCCESS;        }else {            //登陆失败,提示验证码不正确!            return ResultObj.LOGIN_CHECKCODE_ERROR_PASS;        }    }    /**     * 得到登陆验证码     * @param response     * @param session     * @throws     */    @RequestMapping("/getCode")    public void getCode(HttpServletResponse response, HttpSession session) throws IOException {        //HuTool定义图形验证码的长和宽,验证码的位数,干扰线的条数        LineCaptcha lineCaptcha = CaptchaUtil.createLineCaptcha(116, 36,4,10);        //将验证码放入session        session.setAttribute("code",lineCaptcha.getCode());        try {            ServletOutputStream outputStream = response.getOutputStream();            lineCaptcha.write(outputStream);            outputStream.close();        } catch (IOException e) {            e.printStackTrace();        }    }}

转载地址:https://blog.csdn.net/wyn_365/article/details/107315706 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:springboot+mybatisplus+layui+restful实现弹出层异步提交表单
下一篇:java创建线程的几种方式

发表评论

最新留言

初次前来,多多关照!
[***.217.46.12]2024年04月09日 21时16分31秒