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

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 前端技术 > javascript >内容正文

javascript

Spring MVC 验证码

發(fā)布時(shí)間:2023/12/10 javascript 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Spring MVC 验证码 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

頁(yè)面

<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>

<%

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>

? <head>

? ? <base href="<%=basePath%>">

? ??

? ? <title>My JSP 'code.jsp' starting page</title>

? ??

<meta http-equiv="pragma" content="no-cache">

<meta http-equiv="cache-control" content="no-cache">

<meta http-equiv="expires" content="0"> ? ?

<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">

<meta http-equiv="description" content="This is my page">

<!--

<link rel="stylesheet" type="text/css" href="styles.css">

-->

<script src="js/jquery-1.7.2.min.js"></script>

<script type="text/javascript">

?function changeImg() {

? var imgSrc = $("#imgObj");

? var src = imgSrc.attr("src");

? //alert(src);

? imgSrc.attr("src", chgUrl(src));

? alert($("#code").val());

? //$("#code").val("123456");?

? //<c:out value="${sessionScope.code}"/>

?}

?//時(shí)間戳 ?

?//為了使每次生成圖片不一致,即不讓瀏覽器讀緩存,所以需要加上時(shí)間戳 ?

?function chgUrl(url) {

? var timestamp = (new Date()).valueOf();

? url = url.substring(0, 17);

? if ((url.indexOf("&") >= 0)) {

? ?url = url + "×tamp=" + timestamp;

? } else {

? ?url = url + "?timestamp=" + timestamp;

? }

? return url;

?}

?function refresh(obj) {

? ? ? ? obj.src = "code.html";

? ? }

</script>

? </head>

??

? <body>?

? ? ? ? 驗(yàn)證碼<input id="index_code" name="code" type="text" />?

? ? ? ? <img id="imgObj" alt="驗(yàn)證碼" src="code.html" οnclick="javascript:changeImg();" />?

? ? ? ? <!-- <a href="" οnclick="changeImg()"οnclick="javascript:refresh(this);">換一張</a> -->

? ? ? ? <input type="button" οnclick="javascript:changeImg();" value="看不清換一張">

? ? ? ? <input type="hidden" value="123456" id="code"/>

? </body>

</html>

后臺(tái)

/**

?* 用戶驗(yàn)證碼

?* @author Administrator

?*

?*/

@Controller

public class CodeController {

private int width = 90;//定義圖片的width ?

? ?private int height = 20;//定義圖片的height ?

? ?private int codeCount = 4;//定義圖片上顯示驗(yàn)證碼的個(gè)數(shù) ?

? ?private int xx = 15;?

? ?private int fontHeight = 18;?

? ?private int codeY = 16;?

? ?char[] codeSequence = {?

? ? 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',

? ? 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',

? ? '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };?

?

? ?@RequestMapping("/code")?

? ?public void getCode(HttpServletRequest req, HttpServletResponse resp)?

? ? ? ? ? ?throws IOException {?

?

? ? ? ?// 定義圖像buffer ?

? ? ? ?BufferedImage buffImg = new BufferedImage(width, height,?

? ? ? ? ? ? ? ?BufferedImage.TYPE_INT_RGB);?

? ? ? ?//Graphics2D gd = buffImg.createGraphics(); ?

? ? ? ?//Graphics2D gd = (Graphics2D) buffImg.getGraphics(); ?

? ? ? ?Graphics gd = buffImg.getGraphics();?

? ? ? ?// 創(chuàng)建一個(gè)隨機(jī)數(shù)生成器類 ?

? ? ? ?Random random = new Random();?

? ? ? ?// 將圖像填充為白色 ?

? ? ? ?gd.setColor(Color.WHITE);?

? ? ? ?gd.fillRect(0, 0, width, height);?

?

? ? ? ?// 創(chuàng)建字體,字體的大小應(yīng)該根據(jù)圖片的高度來(lái)定。 ?

? ? ? ?Font font = new Font("Fixedsys", Font.BOLD, fontHeight);?

? ? ? ?// 設(shè)置字體。 ?

? ? ? ?gd.setFont(font);?

?

? ? ? ?// 畫邊框。 ?

? ? ? ?gd.setColor(Color.BLACK);?

? ? ? ?gd.drawRect(0, 0, width - 1, height - 1);?

?

? ? ? ?// 隨機(jī)產(chǎn)生40條干擾線,使圖象中的認(rèn)證碼不易被其它程序探測(cè)到。 ?

? ? ? ?gd.setColor(Color.BLACK);?

? ? ? ?for (int i = 0; i < 40; i++) {?

? ? ? ? ? ?int x = random.nextInt(width);?

? ? ? ? ? ?int y = random.nextInt(height);?

? ? ? ? ? ?int xl = random.nextInt(12);?

? ? ? ? ? ?int yl = random.nextInt(12);?

? ? ? ? ? ?gd.drawLine(x, y, x + xl, y + yl);?

? ? ? ?}?

?

? ? ? ?// randomCode用于保存隨機(jī)產(chǎn)生的驗(yàn)證碼,以便用戶登錄后進(jìn)行驗(yàn)證。 ?

? ? ? ?StringBuffer randomCode = new StringBuffer();?

? ? ? ?int red = 0, green = 0, blue = 0;?

?

? ? ? ?// 隨機(jī)產(chǎn)生codeCount數(shù)字的驗(yàn)證碼。 ?

? ? ? ?for (int i = 0; i < codeCount; i++) {?

? ? ? ? ? ?// 得到隨機(jī)產(chǎn)生的驗(yàn)證碼數(shù)字。 ?

? ? ? ? ? ?String code = String.valueOf(codeSequence[random.nextInt(62)]);?

? ? ? ? ? ?// 產(chǎn)生隨機(jī)的顏色分量來(lái)構(gòu)造顏色值,這樣輸出的每位數(shù)字的顏色值都將不同。 ?

? ? ? ? ? ?red = random.nextInt(255);?

? ? ? ? ? ?green = random.nextInt(255);?

? ? ? ? ? ?blue = random.nextInt(255);?

?

? ? ? ? ? ?// 用隨機(jī)產(chǎn)生的顏色將驗(yàn)證碼繪制到圖像中。 ?

? ? ? ? ? ?gd.setColor(new Color(red, green, blue));?

? ? ? ? ? ?gd.drawString(code, (i + 1) * xx, codeY);?

?

? ? ? ? ? ?// 將產(chǎn)生的四個(gè)隨機(jī)數(shù)組合在一起。 ?

? ? ? ? ? ?randomCode.append(code);?

? ? ? ?}?

? ? ? ?// 將四位數(shù)字的驗(yàn)證碼保存到Session中。 ?

? ? ? ?HttpSession session = req.getSession();?

? ? ? ?System.out.println(randomCode);?

? ? ? ?session.setAttribute("code", randomCode.toString());?

?

? ? ? ?// 禁止圖像緩存。 ?

? ? ? ?resp.setHeader("Pragma", "no-cache");?

? ? ? ?resp.setHeader("Cache-Control", "no-cache");?

? ? ? ?resp.setDateHeader("Expires", 0);?

?

? ? ? ?resp.setContentType("image/jpeg");?

?

? ? ? ?// 將圖像輸出到Servlet輸出流中。 ?

? ? ? ?ServletOutputStream sos = resp.getOutputStream();?

? ? ? ?ImageIO.write(buffImg, "jpeg", sos);?

? ? ? ?sos.close();?

? ?}?

/**********************************調(diào)到驗(yàn)證碼**************************************/

@RequestMapping("/tocode")

public ModelAndView tologin(HttpServletRequest request,HttpServletResponse response) {

ModelAndView mav = new ModelAndView();

mav.setViewName("/user/code");

return mav;

}

}


總結(jié)

以上是生活随笔為你收集整理的Spring MVC 验证码的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

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