登录时获取验证码

简单的保存一下获取登录时的验证码    以后我们都可用:

首先:这是一个忘记密码的验证码;

<div class="form-group">
      <label for="lastname" class="col-sm-2 control-label">验证码:</label>
      <div class="col-sm-10">
             <div class="input-group input-group-md">
                    <input type="text" class="form-control" id="lastname" name="lastname" placeholder="请输入验证码">

                    <span class="input-group-addon" style="cursor: pointer;">
                         <img alt="换一张" src="${pageContext.request.contextPath}/getcode" id="validateCodeImg" οnclick="changeImg()">
                    </span>
             </div>
      </div>
</div>

下面是

    //刷新验证码
      function changeImg(){
          document.getElementById("validateCodeImg").src="${pageContext.request.contextPath}/getcode?"+Math.random();
     }

然后跳转到Action层

/**
 * @author Administrator
 * 验证码生成类
 */
@Controller
@RequestMapping
public class SecCodeAction {
    
    @RequestMapping("/getcode")
    public void generate(HttpSession session,HttpServletResponse response){
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        String code = drawImg(output);
        session.setAttribute(Const.SESSION_SECURITY_CODE, code); //放入session
        //System.out.println(session.getAttribute(Const.SESSION_SECURITY_CODE));
        try {
            ServletOutputStream out = response.getOutputStream();
            output.writeTo(out);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    
    private String drawImg(ByteArrayOutputStream output){
        String code = "";
        for(int i=0; i<4; i++){
            code += randomChar();
        }
        int width = 70;
        int height = 25;

     // 1.创建一个不带透明色的BufferedImage对象

        BufferedImage bi = new BufferedImage(width,height,BufferedImage.TYPE_3BYTE_BGR);
        Font font = new Font("Times New Roman",Font.PLAIN,20);
        Graphics2D g = bi.createGraphics();
        g.setFont(font);
        Color color = new Color(66,2,82);
        g.setColor(color);
        g.setBackground(new Color(226,226,240));
        g.clearRect(0, 0, width, height);
        FontRenderContext context = g.getFontRenderContext();
        Rectangle2D bounds = font.getStringBounds(code, context);
        double x = (width - bounds.getWidth()) / 2;
        double y = (height - bounds.getHeight()) / 2;
        double ascent = bounds.getY();
        double baseY = y - ascent;
        g.drawString(code, (int)x, (int)baseY);
        g.dispose();
        try {
            ImageIO.write(bi, "jpg", output);
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return code;
    }
    //获得一个随机的字符  
    private char randomChar(){
        Random r = new Random();//随机类
        String s = "ABCDEFGHJKLMNPRSTUVWXYZ0123456789"; 
        return s.charAt(r.nextInt(s.length()));
    }
}


r.nextInt(s.length()) 是从0 到s的长度的随机数,

public static final String SESSION_SECURITY_CODE = "sessionSecCode";  //常量值

BufferedImage: 图片的缓冲流,应该可以将图片信息读到内存中,专门用来处理图片的。

BufferedImage.TYPE_3BYTE_BGR : 表示一个具有 8 位rgb颜色分量的图像,对应于 windows 风格的 bgr 颜色模型,具有用 3 字节存储的 blue、green 和red 三种颜色。不存在alpha。该图像具有一个 componentcolormodel。当具有透明 alpha 的数据存储在此类型的图像中时,必须将颜色数据调整为非预乘形式并丢弃 alpha,如 alphacomposite 文档中的描述。

 Font.PLAIN :Font是JAVA中的字体类,PLAIN是Font类中的静态常量( static final ) ,表示是:普通样式常量。其他可用样式为:BOLD :粗体样式常量 ,ITALIC: 斜体样式常量.如可以如下初始化对象:Font textFont = new Font("宋体" , Font.BOLD , 23);该字体表示23磅粗体的宋体字。

Graphics2D类(我也是搜的具体不太了解 大神可以解释解释):就是一个绘图类 Graphics2D继承自Graphics

bi.createGraphics():就是在bi对象绘图。

FontRenderContext 类是正确测量文本所需的信息容器。

 g.getFontRenderContext();获取FontRenderContext对象。

g.drawString(code, (int)x, (int)baseY);可能是生成图片的额位置。(我也是猜的)

Dispose和Close应该是一样的,(具体百度去)。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
获取验证码的测试用例可以包括以下几个方面的情况: 1. 手机号或验证码输入为空,点击获取验证码,应该提示手机号码或验证码不能为空。[3.1] 2. 输入不符合手机号规则的数字,例如10位或12位数字,或者11位非手机号码,点击获取验证码,应该提示手机号码不正确。[3.2] 3. 验证码长度应该符合设计要求,并且验证码的内容可以包含数字、字母等字符,是否区分大小写取决于设计要求。前端页面应该根据设计要求限制验证码的长度。[3.3] 4. 输入未注册过的手机号,点击发送短信,可以有两种情况:如果提示未注册,则说明手机号码未注册;如果自动注册并登陆成功,则需要检查数据库中该用户信息是否已经落库。[3.4] 5. 输入错误的验证码,应该给予友好的提示,说明验证码输入错误。尝试输入多一位或少一位验证码,应该无法成功获取验证码。[3.5] 6. 如果输入错误的验证码,是否可以再次点击发送短信,以及是否可以正常发送和接收验证码。[3.6] 7. 如果存在失效长,当验证码在失效长外输入,应该提示该验证码已失效,请重新获取验证码。[3.7] 8. 验证码已经使用后,不应该再次使用。如果存在失效长,验证码应该在失效长内有效。[3.8] 9. 输入正确验证码后,再更换另一个已注册过的手机号,应该能够登录成功。而输入正确验证码后,再更换另一个未注册过的手机号,不应该能够登录成功。[3.9] 10. 重复发送验证码,前一个验证码应该会失效。[3.10] 11. 若用户重复点击发送验证码,是否会有限制次数及友好提示。[3.11]<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [手机验证码的测试用例梳理](https://blog.csdn.net/chenlei_525/article/details/119885088)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [短信登录功能测试用例](https://blog.csdn.net/xun_zhao_t521/article/details/119914148)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值