zoukankan      html  css  js  c++  java
  • hutool工具之验证码登录

    package com.baizhi.action;
    
    import java.io.ByteArrayInputStream;
    
    import javax.servlet.http.HttpSession;
    
    import org.apache.struts2.ServletActionContext;
    
    import cn.hutool.captcha.CaptchaUtil;
    import cn.hutool.captcha.LineCaptcha;
    
    public class CodeAction {
        private ByteArrayInputStream imageStream;
        //画验证码,并通过流返回到调用位置,将验证码对象放入session中
        public String code() {
            LineCaptcha captcha = CaptchaUtil.createLineCaptcha(150, 50, 4, 6);
            imageStream = new ByteArrayInputStream(captcha.getImageBytes());
            HttpSession session = ServletActionContext.getRequest().getSession();
            session.setAttribute("cap", captcha);
    //将验证码写进浏览器
        Response response = ServletActionContext.getResponse();
        ServletOutputStream outputStream = response.getOutputStream();
        captcha.write(outputStream);
        //
    return "ok";
        return null; }
    public ByteArrayInputStream getImageStream() { return imageStream; } public void setImageStream(ByteArrayInputStream imageStream) { this.imageStream = imageStream; } }
    package com.baizhi.action;
    
    import javax.servlet.http.HttpSession;
    
    import org.apache.struts2.ServletActionContext;
    
    import cn.hutool.captcha.LineCaptcha;
    
    public class UserAction {
        private String code;
        public String login() {
            
            //判断账号和密码
            
            //从session中获取验证码对象
            HttpSession session = ServletActionContext.getRequest().getSession();
            LineCaptcha captcha = (LineCaptcha)session.getAttribute("cap");
            //判断验证码是否输入正确
            boolean verify = captcha.verify(code);
            System.out.println("用户输入的验证码为:"+verify);
            
            return "loginOk";
            
        }
        
        
        public String getCode() {
            return code;
        }
        public void setCode(String code) {
            this.code = code;
        }    
        
    }
    package com.baizhi.test;
    
    import java.util.Scanner;
    
    import cn.hutool.captcha.CaptchaUtil;
    import cn.hutool.captcha.CircleCaptcha;
    import cn.hutool.captcha.LineCaptcha;
    import cn.hutool.captcha.ShearCaptcha;
    
    public class CodeTest {
        public static void main(String[] args) {
            //使用糊涂工具包画验证码
            //CircleCaptcha captcha = CaptchaUtil.createCircleCaptcha(200, 80, 4, 100);
            LineCaptcha captcha = CaptchaUtil.createLineCaptcha(200, 80, 6, 6);
            //ShearCaptcha captcha = CaptchaUtil.createShearCaptcha(200, 80, 4, 1);
            //将验证码保存到d://a.jpg
            captcha.write("D://a.jpg");
            
            Scanner sc = new Scanner(System.in);
            String next = sc.next();
            //判断用户输入是否正确
            boolean verify = captcha.verify(next);
            System.out.println(verify);
        }
    }

     jsp页面

    <%@ page language="java" contentType="text/html; charset=UTF-8"
        pageEncoding="UTF-8"%>
    <!DOCTYPE html>
    <html>
    <head>
    <meta charset="UTF-8">
    <title>Insert title here</title>
    </head>
    <body>
        <form action="${pageContext.request.contextPath }/user/login">
            账户:<input type="text"   /><br/>
            密码:<input type="text"   /><br/>
             <img src='${pageContext.request.contextPath}/vericode/Code_showCode' alt="" id="sr" onclick='chance()' height='50px' >
            <input  type="submit" />
        </form>
        
    </body>
    </html>

    mapper.xml

    <package name="code" extends="struts-default">
            <action name="code" class="com.baizhi.action.CodeAction" method="code">
               <!-- <result name="ok" type="stream">
                    <param name="contentType">image/jpeg</param>
                    <param name="inputName">imageStream</param>
                    <param name="bufferSize">2048</param>
                </result> -->
            </action>
        </package>

    其实利用的是src可以的路径可以请求action

    利用jQuery绑定点击事件可以实现点击更换验证码

    <script type="text/javascript">
            function chance() {
                var img=document.getElementById("sr");
                img.src="${pageContext.request.contextPath }/validateCode?a="+ Math.random();
            }
    </script>
    以粮为纲全面发展
  • 相关阅读:
    百度富文本编辑器的上传图片的路径问题
    laravel初次学习总结及一些细节
    macOS apache配置及开启虚拟服务器的开启,apache开启重写模式
    类似于qq空间类型的评论和回复
    向php提交数据及json
    mac 初次配置apache,及mac下安装mysql
    C#连接mysql数据库插入数据后获取自增长主键ID值
    PHP 真正多线程的使用
    C# 连接mysql数据库
    MySql状态查看方法 MySql如何查看连接数和状态?
  • 原文地址:https://www.cnblogs.com/alexliuf/p/13641200.html
Copyright © 2011-2022 走看看