spirng mvc 中使用验证码
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                spirng mvc 中使用验证码
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                生成驗證碼方法:
@RequestMapping("/vCode")@ResponseBodypublic Map<String, String> vCode(HttpServletRequest request,HttpServletResponse response){//設置不緩存圖片response.setHeader("Pragma", "No-cache");response.setHeader("Cache-Control", "No-cache");response.setDateHeader("Expires", 0);//設置生成類型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"); // 轉成中文} 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;}}?
轉載于:https://www.cnblogs.com/phyxis/p/5424267.html
總結
以上是生活随笔為你收集整理的spirng mvc 中使用验证码的全部內容,希望文章能夠幫你解決所遇到的問題。
                            
                        - 上一篇: git Please move or r
 - 下一篇: php抽象 与接口