ssm框架中验证码功能的使用

1、pom.xml导入validatecode包(此包需要手动导入进maven仓库)

<!--    验证码包-->
    <dependency>
      <groupId>com.validatecode</groupId>
      <artifactId>validatecode</artifactId>
      <version>1.1.0</version>
    </dependency>

如何手动导入validatecode包:(在黑窗口运行下面指令,首先在网上寻找validatecode.jar并下载到指定位置)

maven安装本地jar

 mvn install:install-file

 -Dfile=jar包的位置

 -DgroupId=上面的groupId

 -DartifactId=上面的artifactId

 -Dversion=上面的version

 -Dpackaging=jar

 ----------------------------------------------------------------

指令实例: mvn install:install-file -Dfile=D:\ValidateCode.jar -DgroupId=com.validatecode  -DartifactId=validatecode  -Dversion=1.1.0  -Dpackaging=jar

2、validatecode在web层的书写

package com.buka.web;

import cn.dsna.util.images.ValidateCode;
import com.sun.deploy.net.HttpResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;


import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/val")
public class CodeWeb {

    @RequestMapping("/Code")
    public void getCode(HttpServletResponse response, HttpSession httpSession){
        try {
//            //解决:浏览器缓存
//            //设置浏览器端缓存信息
//            response.setHeader("Cache-Control", "no-store");
//            response.setHeader("Pragrma", "no-cache");
//            response.setDateHeader("Expires", 0);
            //生成验证码图片(1,宽2,高,3个数,4,干扰线)
            ValidateCode validateCode = new ValidateCode(163,64,4,50);
            //记录图片答案
            String code = validateCode.getCode();
            //存进session域对象
            httpSession.setAttribute("code",code);
            //返回前段页面
            validateCode.write(response.getOutputStream());
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //判断验证码是否正确
    @RequestMapping("/checkValidate")
    @ResponseBody
    public String checkValidate(String code,HttpSession httpSession){
        String code1 = (String) httpSession.getAttribute("code");

        //此equals不区分大小写
        if(code1.equalsIgnoreCase(code)){
            return "ok";
        }else{
            return "no";
        }

    }

}

3、在前端页面的书写

代码在jsp页面进行书写的,前端用el表达式进行前端数据的传输获取


        <div class="psw psw3">
            <p class="psw-p1">验证码</p>
            <input type="text" placeholder="请输入验证码" name="validate" id="validate" required/>
            <span  id="codeMsg" name="msg">请输入验证码</span>
        </div>
        <div class="yanzhentu">
            <div class="yz-tu f-l">
                <img src="${pageContext.request.contextPath}/val/Code" id="code"/>
            </div>
            <p class="f-l">看不清?<a href="javaScript:;" id="changeCode">换张图</a></p>
            <div style="clear:both;"></div>
        </div>

4、结果展示

 

  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值