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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

spirng mvc 中使用验证码

發(fā)布時間:2024/7/19 c/c++ 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spirng mvc 中使用验证码 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

生成驗證碼方法:

@RequestMapping("/vCode")@ResponseBodypublic Map<String, String> vCode(HttpServletRequest request,HttpServletResponse response){//設(shè)置不緩存圖片response.setHeader("Pragma", "No-cache");response.setHeader("Cache-Control", "No-cache");response.setDateHeader("Expires", 0);//設(shè)置生成類型response.setContentType("image/jpeg");int width = 85;int height = 25;BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);Graphics g = image.getGraphics();Graphics2D g2d = (Graphics2D)g;Font font = new Font("正楷", Font.BOLD, 16);g.setColor(getRandColor(200, 250));g.fillRect(0, 0, width, height);g.setFont(font);g.setColor(getRandColor(180, 200));String vCode = "";String msg = "";Random random= new Random ();int length = Math.abs(random.nextInt(2));for (int i = 0; i < 4 +length; i++) {random = new Random ();int check = Math.abs(random.nextInt(3));if (check == 0) {msg = StringUtil.getNum()+"";}if (check == 1) {msg = StringUtil.getChar()+"";}if (check == 2) {msg = StringUtil.getChinese();}Color color = new Color(20+random.nextInt(110),20+random.nextInt(110),random.nextInt(110));g.setColor(color);g.drawString(msg, 15*i+5, 14);vCode += msg; }HttpSession session = request.getSession(true);session.setAttribute("vCode", vCode);g.dispose();try {ImageIO.write(image, "JPEG", response.getOutputStream());} catch (IOException e) {e.printStackTrace();}return null;} public Color getRandColor(int s,int e){ Random random=new Random (); if(s>255) s=255; if(e>255) e=255; int r,g,b; r=s+random.nextInt(e-s); //隨機生成RGB顏色中的r值 g=s+random.nextInt(e-s); //隨機生成RGB顏色中的g值 b=s+random.nextInt(e-s); //隨機生成RGB顏色中的b值 return new Color(r,g,b); } public class StringUtil {public static void main(String[] args) {System.out.println(getValidCode());}public static String getValidCode() {String ret = "";Random random = new Random();for (int i = 0; i < 4 + Math.abs(random.nextInt(3)); i++) {int check = Math.abs(random.nextInt(3));if (check == 0) {ret += getNum();}if (check == 1) {ret += getChar();}if (check == 2) {ret += getChinese();}}return ret;}public static String getChinese() {Random random = new Random();byte[] b = new byte[2];b[0] = (new Integer(176 + Math.abs(random.nextInt(39))).byteValue());// 獲取高位值b[1] = (new Integer(161 + Math.abs(random.nextInt(93))).byteValue());// 獲取低位值try {return new String(b, "GBk"); // 轉(zhuǎn)成中文} catch (UnsupportedEncodingException ex) {ex.printStackTrace();}return null;}public static char getChar() {Random random = new Random();int index = Math.abs(random.nextInt(26));return (char) (index + 'A');}public static int getNum() {Random random = new Random();int index = Math.abs(random.nextInt(10));return index;}}

?

轉(zhuǎn)載于:https://www.cnblogs.com/phyxis/p/5424267.html

總結(jié)

以上是生活随笔為你收集整理的spirng mvc 中使用验证码的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。