前後端分離找回密碼功能實現

流程

1.用戶名驗證

2.驗證通過後發送修改密碼連接到用戶綁定郵箱

3.用戶登錄郵箱,點擊修改密碼鏈接跳轉到修該密碼頁面

4.驗證碼源碼

4.1生成驗證碼

public void getCode(HttpServletRequest request, HttpServletResponse response) {
		// 驗證碼圖片的寬度。
		int width = 70;
		// 驗證碼圖片的高度。
		int height = 30;
		BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		Graphics2D g = buffImg.createGraphics();

		// 創建一個隨機數生成器類。
		Random random = new Random();

		// 設定圖像背景色(因爲是做背景,所以偏淡)
		g.setColor(getRandColor(200, 250));
		g.fillRect(0, 0, width, height);
		// 創建字體,字體的大小應該根據圖片的高度來定。
		Font font = new Font("Times New Roman", Font.HANGING_BASELINE, 28);
		// 設置字體。
		g.setFont(font);

		// 畫邊框。
		g.setColor(Color.BLACK);
		g.drawRect(0, 0, width - 1, height - 1);
		// 隨機產生155條幹擾線,使圖象中的認證碼不易被其它程序探測到。
		g.setColor(getRandColor(160, 200));
		for (int i = 0; i < 155; i++) {
			int x = random.nextInt(width);
			int y = random.nextInt(height);
			int xl = random.nextInt(12);
			int yl = random.nextInt(12);
			g.drawLine(x, y, x + xl, y + yl);
		}

		// randomCode用於保存隨機產生的驗證碼,以便用戶登錄後進行驗證。
		StringBuffer randomCode = new StringBuffer();

		// 設置默認生成4個驗證碼
		int length = 4;
		// 設置備選驗證碼:包括"a-z"和數字"0-9"
		String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";

		int size = base.length();

		// 隨機產生4位數字的驗證碼。
		for (int i = 0; i < length; i++) {
			// 得到隨機產生的驗證碼數字。
			int start = random.nextInt(size);
			String strRand = base.substring(start, start + 1);

			// 調用函數出來的顏色相同,可能是因爲種子太接近,所以只能直接生成
			g.setColor(new Color(20 + random.nextInt(110), 20 + random.nextInt(110), 20 + random.nextInt(110)));

			g.drawString(strRand, 15 * i + 6, 24);

			// 將產生的四個隨機數組合在一起。
			randomCode.append(strRand);
		}

		// 將四位數字的驗證碼保存到Session中。
		HttpSession session = request.getSession();
		session.setAttribute("signcode", randomCode.toString());

		// 圖象生效
		g.dispose();

		// 禁止圖像緩存。
		response.setHeader("Pragma", "no-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0);

		response.setContentType("image/jpeg");

		// 將圖像輸出到Servlet輸出流中。
		ServletOutputStream sos;
		try {
			sos = response.getOutputStream();
			ImageIO.write(buffImg, "jpeg", sos);
			sos.flush();
			sos.close();
		} catch (IOException e) {

			e.printStackTrace();
			LOG.info("getCode{}:", e);
		}
		xxxMCRequestUtil.success(response, null);
	}
// 給定範圍獲得隨機顏色
	Color getRandColor(int fc, int bc) {
		Random random = new Random();
		if (fc > 255) {
			fc = 255;
		}

		if (bc > 255) {
			bc = 255;
		}

		int r = fc + random.nextInt(bc - fc);
		int g = fc + random.nextInt(bc - fc);
		int b = fc + random.nextInt(bc - fc);
		return new Color(r, g, b);
	}

4.2驗證驗證碼

public void checkCode(HttpServletRequest request, HttpServletResponse response) {
		String signCode = RequestUtil.getParameterAndTrim(request, "signCode");
		HttpSession session = request.getSession();
		String signcodeSession = (String) session.getAttribute("signcode");

		if (StringHelper.isEmpty(signCode)) {
			BasMCRequestUtil.fail(response, "驗證碼爲不能爲空", "驗證碼不能爲空");
		}

		if (StringHelper.isEmpty(signcodeSession)) {
			BasMCRequestUtil.fail(response, "重置密碼鏈接不正確,請重新操作", "重置密碼鏈接不正確,請重新操作");
		}
		int isTrue = 1;
		// 驗證的時候不區分大小寫
		signCode = signCode.toUpperCase();
		signcodeSession = signcodeSession.toUpperCase();
		if (!signCode.equals(signcodeSession)) {
			isTrue = 0;
		}
		JSONObject json = new JSONObject();
		json.put("code", 0);
		json.put("isTrue", isTrue);
		JtableUtil.outputAsJTableJSON(response, json.toString());

	}

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章