日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

不使用session,借助redis实现验证码

發布時間:2025/3/8 编程问答 72 豆豆
生活随笔 收集整理的這篇文章主要介紹了 不使用session,借助redis实现验证码 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1.首先看一下基本的流程

2.看一下代碼

   注:其中用到的一些工具類,可以到我的github上去下載

    ?https://github.com/hjzgg/usually_util/tree/master/utils

    windows 下的 redis下載

    https://github.com/hjzgg/redis

  獲取驗證碼的tooken

   @RequestMapping(value="loginCode")@ResponseBodypublic String getCode(){PrintWriter out = null;JSONObject jsono = new JSONObject();try {
       //驗證碼工具類ValidateCode vCode
= new ValidateCode(55,25,4,80);String randomCode = vCode.randomCode();String encCode = DesUtil.strEnc(randomCode+System.currentTimeMillis(), "1", "2", "3");//存儲驗證碼字符串,過期時間為1分鐘 redisTemplate.opsForValue().set(encCode, randomCode);redisTemplate.expire(encCode, 1, TimeUnit.MINUTES);//存儲驗證碼生成器,過期時間為1分鐘redisTemplate.opsForValue().set(encCode+"ValidateCode", SerializeUtil.serialize(vCode));redisTemplate.expire(encCode+"ValidateCode", 1, TimeUnit.MINUTES);jsono.put("success", true);jsono.put("message", encCode);} catch (Exception e) {e.printStackTrace();jsono.put("success", true);jsono.put("message", "inner error.");} finally{if(out != null) {out.flush();out.close();}}return jsono.toString();}

  本例中的tooken是通過加密生成的,加密串為 驗證碼+當前時間。或者采用UUID生成唯一tooken,都是可以得。生成ValidateCode(驗證碼工具類),然后將鍵值對(tooken,ValidateCode)放入redis中。

  ?獲取驗證碼圖片

  @RequestMapping(value="loginCodeImage")public void getCodeImage(String codeAuth, HttpServletResponse response){if(codeAuth == null) return;String randomCode = (String) redisTemplate.opsForValue().get(codeAuth);if(randomCode == null) return;ValidateCode vCode = (ValidateCode)SerializeUtil.unserialize((byte[])redisTemplate.opsForValue().get(codeAuth+"ValidateCode"));//產生圖片 vCode.createCode(randomCode);if(vCode == null) return;// 設置響應的類型格式為圖片格式 response.setContentType("image/jpeg"); //禁止圖像緩存。 response.setHeader("Pragma", "no-cache"); response.setHeader("Cache-Control", "no-cache"); response.setDateHeader("Expires", 0); try {vCode.write(response.getOutputStream());} catch (IOException e) {e.printStackTrace();} }

  根據tooken,在redis中找到對應的ValidateCode(驗證碼工具類),生成驗證碼圖片。

3.前臺獲取驗證碼

  網頁中獲取

    <img src="htpp://......"/>

? ? ? java中獲取

  public static ImageIcon getCodeImage(){String data = JavaRequest.sendPost("loginCode", null);JSONObject result = JSONObject.fromObject(data);if((Boolean) result.get("success")){JavaRequest.codeAuth = result.getString("message");ImageIcon codeImg = null;try{codeImg = new ImageIcon(new URL(“.....”));} catch (Exception e) {e.printStackTrace();return null;}return codeImg;} else {System.out.println("獲取驗證碼圖片: " + result);return null;}}ImageIcon codeImg = JavaRequest.getCodeImage();if(codeImg == null){codeImg = new ImageIcon("獲取失敗的圖片.png");}
  /
JLable codeImgLabel = new JLabel(codeImg);

?

轉載于:https://www.cnblogs.com/hujunzheng/p/5490899.html

總結

以上是生活随笔為你收集整理的不使用session,借助redis实现验证码的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。