使用HuTool工具类,实现登录验证码

精选30+云产品,助力企业轻松上云!>>> hot3.png

1.首先引入HuTool的依赖包

        <!--hutool-->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.6.8</version>
        </dependency>

2.登录页面

		<div class="layui-form-item input-item" id="imgCode">
			<label for="code">验证码</label>
			<input type="text" placeholder="请输入验证码" name="code" autocomplete="off" id="code" class="layui-input">
			<img th:src="@{/login/getCode}" onclick="this.src=this.src+'?'">
		</div>

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();
        }
    }

}
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Coding路人王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值