亚洲精品中文免费|亚洲日韩中文字幕制服|久久精品亚洲免费|一本之道久久免费

<optgroup id="cczp1"><ruby id="cczp1"><cite id="cczp1"></cite></ruby></optgroup>
  • <acronym id="cczp1"></acronym>
    <acronym id="cczp1"><option id="cczp1"><ol id="cczp1"></ol></option></acronym>
    <delect id="cczp1"></delect>
    <center id="cczp1"></center>
    <delect id="cczp1"></delect><em id="cczp1"><button id="cczp1"><blockquote id="cczp1"></blockquote></button></em>
    1. <optgroup id="cczp1"><td id="cczp1"><dfn id="cczp1"></dfn></td></optgroup>

      SpringBoot+Vue項(xiàng)目中實(shí)現(xiàn)登錄驗(yàn)證碼校驗(yàn)

      SpringBoot+Vue項(xiàng)目中實(shí)現(xiàn)登錄驗(yàn)證碼校驗(yàn)

      在各大項(xiàng)目中,為保證數(shù)據(jù)的安全性,通常在登錄頁面加入驗(yàn)證碼校驗(yàn),以防止爬蟲帶來的數(shù)據(jù)泄露危機(jī)。本文將介紹在前后端分離的項(xiàng)目中,怎樣實(shí)現(xiàn)圖形驗(yàn)證碼校驗(yàn)。

      實(shí)現(xiàn)思路

      第一步:在后端創(chuàng)建一個(gè)生成隨機(jī)驗(yàn)證碼的工具類和接收請求驗(yàn)證碼的接口。工具類的主要作用生成隨機(jī)驗(yàn)證碼和對應(yīng)的圖片。接口的作用是將生成的隨機(jī)驗(yàn)證碼保存到session,同時(shí),將圖片進(jìn)行base64編碼,然后返回給前端。

      第二步:在登錄頁面創(chuàng)建的同時(shí)獲取驗(yàn)證碼,并將后端傳回來得key和編碼后的字符串拼接,綁定img標(biāo)簽的src屬性。此外,當(dāng)用戶點(diǎn)擊驗(yàn)證碼的img標(biāo)簽時(shí),重新獲取驗(yàn)證碼,后端session更新驗(yàn)證碼。

      第三步:后端登錄接口接收登錄請求時(shí),將用戶提交的驗(yàn)證碼和session中的驗(yàn)證碼進(jìn)行比對,不相同則返回相應(yīng)信息給前端進(jìn)行提示,相同則進(jìn)行賬號密碼的匹配。

      測試案例

      • 創(chuàng)建驗(yàn)證碼生成的工具類

      package com.check.utils;import java.awt.Color;import java.awt.Font;import java.awt.Graphics;import java.awt.image.BufferedImage;import java.io.IOException;import java.io.OutputStream;import java.util.Random;import javax.imageio.ImageIO;public class CreateImageCode { // 圖片的寬度。 private int width = 160; // 圖片的高度。 private int height = 40; // 驗(yàn)證碼字符個(gè)數(shù) private int codeCount = 4; // 驗(yàn)證碼干擾線數(shù) private int lineCount = 20; // 驗(yàn)證碼 private String code = null; // 驗(yàn)證碼圖片Buffer private BufferedImage buffImg = null; Random random = new Random(); public CreateImageCode() { creatImage(); } public CreateImageCode(int width, int height) { this.width = width; this.height = height; creatImage(); } public CreateImageCode(int width, int height, int codeCount) { this.width = width; this.height = height; this.codeCount = codeCount; creatImage(); } public CreateImageCode(int width, int height, int codeCount, int lineCount) { this.width = width; this.height = height; this.codeCount = codeCount; this.lineCount = lineCount; creatImage(); } // 生成圖片 private void creatImage() { int fontWidth = width / codeCount;// 字體的寬度 int fontHeight = height – 5;// 字體的高度 int codeY = height – 8; // 圖像buffer buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics g = buffImg.getGraphics(); //Graphics2D g = buffImg.createGraphics(); // 設(shè)置背景色 g.setColor(getRandColor(200, 250)); g.fillRect(0, 0, width, height); // 設(shè)置字體 //Font font1 = getFont(fontHeight); Font font = new Font(“Fixedsys”, Font.BOLD, fontHeight); g.setFont(font); // 設(shè)置干擾線 for (int i = 0; i < lineCount; i++) { int xs = random.nextInt(width); int ys = random.nextInt(height); int xe = xs + random.nextInt(width); int ye = ys + random.nextInt(height); g.setColor(getRandColor(1, 255)); g.drawLine(xs, ys, xe, ye); } // 添加噪點(diǎn) float yawpRate = 0.01f;// 噪聲率 int area = (int) (yawpRate * width * height); for (int i = 0; i < area; i++) { int x = random.nextInt(width); int y = random.nextInt(height); buffImg.setRGB(x, y, random.nextInt(255)); } String str1 = randomStr(codeCount);// 得到隨機(jī)字符 this.code = str1; for (int i = 0; i 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); } /** * 產(chǎn)生隨機(jī)字體 */ private Font getFont(int size) { Random random = new Random(); Font font[] = new Font[5]; font[0] = new Font(“Ravie”, Font.PLAIN, size); font[1] = new Font(“Antique Olive Compact”, Font.PLAIN, size); font[2] = new Font(“Fixedsys”, Font.PLAIN, size); font[3] = new Font(“Wide Latin”, Font.PLAIN, size); font[4] = new Font(“Gill Sans Ultra Bold”, Font.PLAIN, size); return font[random.nextInt(5)]; } // 扭曲方法 private void shear(Graphics g, int w1, int h1, Color color) { shearX(g, w1, h1, color); shearY(g, w1, h1, color); } private void shearX(Graphics g, int w1, int h1, Color color) { int period = random.nextInt(2); boolean borderGap = true; int frames = 1; int phase = random.nextInt(2); for (int i = 0; i > 1) * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames); g.copyArea(0, i, w1, 1, (int) d, 0); if (borderGap) { g.setColor(color); g.drawLine((int) d, i, 0, i); g.drawLine((int) d + w1, i, w1, i); } } } private void shearY(Graphics g, int w1, int h1, Color color) { int period = random.nextInt(40) + 10; // 50; boolean borderGap = true; int frames = 20; int phase = 7; for (int i = 0; i > 1) * Math.sin((double) i / (double) period + (6.2831853071795862D * (double) phase) / (double) frames); g.copyArea(i, 0, 1, h1, 0, (int) d); if (borderGap) { g.setColor(color); g.drawLine(i, (int) d, i, 0); g.drawLine(i, (int) d + h1, i, h1); } } } public void write(OutputStream sos) throws IOException { ImageIO.write(buffImg, “png”, sos); sos.close(); } public BufferedImage getBuffImg() { return buffImg; } public String getCode() { return code.toLowerCase(); }}

      • 編寫獲取驗(yàn)證碼的接口

      @RequestMapping(“/login”)@RestControllerpublic class LoginController { @GetMapping(“/getImage”) public Result getImage(HttpServletRequest request) throws IOException { Map result = new HashMap(); CreateImageCode createImageCode = new CreateImageCode(); //獲取驗(yàn)證碼 String securityCode = createImageCode.getCode(); //驗(yàn)證碼存入session String key = new SimpleDateFormat(“yyyyMMddHHmmss”).format(new Date()); request.getServletContext().setAttribute(key, securityCode); //生成圖片 BufferedImage image = createImageCode.getBuffImg(); //進(jìn)行base64編碼 ByteArrayOutputStream bos = new ByteArrayOutputStream(); ImageIO.write(image, “png”, bos); String string = Base64Utils.encodeToString(bos.toByteArray()); result.put(“key”, key); result.put(“image”, string); return ResultUtils.success(“請求成功”,result); }}

      • 編寫axios請求驗(yàn)證碼的函數(shù)

      import requests from “./request”;export const GetImage=()=>requests({ url: “/login/getImage”, method: “get”})

      • vuex中編寫異步方法

      import {GetImage} from ‘../api/login’const state={};const mutations={};const actions={ async getImage(){ let result=await GetImage().then(); return result.dataset; }};const getters={};export default{ state, mutations, actions, getters}

      • 將驗(yàn)證碼綁定給img的src

      async getimage() { let result = await this.$store.dispatch(“getImage”); this.imageurl = “data:image/png;base64,” + result.image; this.adminInfo.key = result.key; },

      • 編寫登錄接口

      @PostMapping(“/login”) public Result login(@RequestBody Login login, HttpServletRequest request){ System.out.println(login); String keyCode = (String) request.getServletContext().getAttribute(login.getKey()); if(keyCode.equals(login.getCode().toLowerCase())){ Admin admin = new Admin(); admin.setUsername(login.getUsername()); admin.setPassword(login.getPassword()); Admin admin1 = adminService.login(admin); if(admin1!=null){ return ResultUtils.success(“登錄成功”,admin1); }else { return ResultUtils.error(“賬號或密碼錯(cuò)誤!”); } }else { return ResultUtils.error(“驗(yàn)證碼錯(cuò)誤!”); }

      測試結(jié)果

      • 前端頁面
      • 輸入錯(cuò)誤驗(yàn)證碼
      鄭重聲明:本文內(nèi)容及圖片均整理自互聯(lián)網(wǎng),不代表本站立場,版權(quán)歸原作者所有,如有侵權(quán)請聯(lián)系管理員(admin#wlmqw.com)刪除。
      用戶投稿
      上一篇 2022年7月10日 12:34
      馬斯克:終止收購?fù)铺?,推特董事會:“有信心”完成交?/span>
      下一篇 2022年7月10日 12:34

      相關(guān)推薦

      • 30個(gè)無加盟費(fèi)的項(xiàng)目(茶顏悅色奶茶店加盟費(fèi)多少)

        茶顏悅色又爆了,8月18日,茶顏悅色南京門店正式開業(yè),開張不到半小時(shí),門店就人滿為患,消費(fèi)者的購買熱情十分高漲,而由于人流量過大造成擁堵,茶顏悅色也不得不暫停營業(yè)。 當(dāng)然,這里面排…

        2022年11月27日
      • 凈利潤率越高越好嗎(凈利潤率多少合適)

        一、持續(xù)增收不增利,平均凈利潤率首次跌入個(gè)位數(shù) 2021年,增收不增利依舊是行業(yè)主流。具體來看,大部分企業(yè)營業(yè)收入呈增長態(tài)勢,E50企業(yè)平均同比增速達(dá)到17.3%,但是利潤增速則明…

        2022年11月26日
      • 規(guī)范透明促PPP高質(zhì)量發(fā)展——16萬億元大市場迎來新規(guī)

        近日,財(cái)政部印發(fā)《關(guān)于進(jìn)一步推動政府和社會資本合作(PPP)規(guī)范發(fā)展、陽光運(yùn)行的通知》,從做好項(xiàng)目前期論證、推動項(xiàng)目規(guī)范運(yùn)作、嚴(yán)防隱性債務(wù)風(fēng)險(xiǎn)、保障項(xiàng)目陽光運(yùn)行四個(gè)方面進(jìn)一步規(guī)范P…

        2022年11月25日
      • 拼多多百億補(bǔ)貼預(yù)售一般多久發(fā)貨(拼多多百億補(bǔ)貼預(yù)售)

        拼多多里面有很多優(yōu)惠活動,其中百億補(bǔ)貼活動非常火爆,一些里面的東西價(jià)格比別的平臺便宜,質(zhì)量也有保障,還有預(yù)售的活動,那么拼多多百億補(bǔ)貼預(yù)售一般多久發(fā)貨?下面小編為大家?guī)砥炊喽喟賰|…

        2022年11月25日
      • 推薦3種白手起家的賺錢項(xiàng)目(白手起家賺錢項(xiàng)目有哪些)

        如今社會壓力非常的大,家有老少要養(yǎng)活,這些都加速了窮人想要?jiǎng)?chuàng)業(yè)的欲望,但是創(chuàng)業(yè)路總是那么的艱難,資金就是創(chuàng)業(yè)的重頭戲,所以選擇一個(gè)低成本又賺錢的項(xiàng)目是大多數(shù)人最期望的了,那么有哪些…

        2022年11月25日
      • 百度關(guān)鍵詞快速排名的4大原理解析(百度怎么刷關(guān)鍵詞)

        近期百度公告驚雷算法2.0,升級之快還是第一次吧,看來百度對于刷點(diǎn)擊行為是零容忍了。之前尹華峰SEO技術(shù)博客介紹過一篇如何使用刷點(diǎn)擊工具,其實(shí)市面上有很多這類SEO快速排名的軟件,…

        2022年11月25日
      • 博客營銷的3大優(yōu)勢解析(博客營銷怎么做)

        不知不覺已經(jīng)寫了24篇文章,加上這篇是第25篇了,都是自己這幾年來用過的營銷方法,如果遇到有些不懂的,我會咨詢我的朋友和同事幫忙,盡量讓每一篇有價(jià)值,哪怕是對大家有一點(diǎn)點(diǎn)幫助也行,…

        2022年11月25日
      • 成都健康碼打不開顯示接口請求未知異常怎么辦(成都健康碼打不開顯示接口請求未知異常)

        成都這幾天的疫情也是備受關(guān)注,疫情期間各地出行都是需要查看健康碼的,不過今天卻有成都的小伙伴反饋健康碼無法打開的情況。成都健康碼打不開顯示接口請求未知異常怎么辦?由于健康碼無法打開…

        2022年11月24日
      • Steam秋季特賣開啟 為Steam大獎(jiǎng)提名游戲

        Steam秋季特賣開啟 為Steam大獎(jiǎng)提名游戲 Steam秋季特賣活動現(xiàn)已正式開啟,時(shí)間從11月23日持續(xù)到11月30日(北京時(shí)間),新老游戲均有不錯(cuò)的折扣,感興趣的玩家可以前往…

        2022年11月24日
      • 閑魚運(yùn)營的4大技巧解析(閑魚運(yùn)營怎么做)

        熟悉我又來了,上一次寫的文章是爆出風(fēng)水項(xiàng)目的潛規(guī)則,但那個(gè)項(xiàng)目已經(jīng)涼涼了。 這一次我是要教一些小白,你們第一次做互聯(lián)網(wǎng)的建議做的項(xiàng)目之一,這個(gè)項(xiàng)目就是閑魚賣二手物品賺差價(jià)了!!! …

        2022年11月24日

      聯(lián)系我們

      聯(lián)系郵箱:admin#wlmqw.com
      工作時(shí)間:周一至周五,10:30-18:30,節(jié)假日休息