国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

后臺(tái)生成隨機(jī)驗(yàn)證碼驗(yàn)證登錄

這篇具有很好參考價(jià)值的文章主要介紹了后臺(tái)生成隨機(jī)驗(yàn)證碼驗(yàn)證登錄。希望對(duì)大家有所幫助。如果存在錯(cuò)誤或未考慮完全的地方,請(qǐng)大家不吝賜教,您也可以點(diǎn)擊"舉報(bào)違法"按鈕提交疑問。

后臺(tái)生成隨機(jī)驗(yàn)證碼驗(yàn)證登錄,java,java

?web get請(qǐng)求獲取圖片

<div class="p2">
    <img id="imgId" src="/get/code">
    <a href="#">看不清,換一張</a>
</div>

后臺(tái)代碼:

/*獲取動(dòng)態(tài)驗(yàn)證碼*/
@ResponseBody
@RequestMapping(value = "/get/code", method = {RequestMethod.POST, RequestMethod.GET})
public void getCode(HttpServletResponse response) {
	creatImg(response);
}
private void creatImg(HttpServletResponse response) {
        int width = 120;
        int height = 30;
        // 在內(nèi)存中生成圖片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        // 先獲取畫筆對(duì)象
        Graphics2D g = (Graphics2D) image.getGraphics();
        // 設(shè)置灰色
        g.setColor(Color.GRAY);
        // 畫填充的矩形
        g.fillRect(0, 0, width, height);
        // 設(shè)置顏色
        g.setColor(Color.BLUE);
        // 畫邊框
        g.drawRect(0, 0, width - 1, height - 1);
        // 準(zhǔn)備數(shù)據(jù),隨機(jī)獲取4個(gè)字符
        String words = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
        // 設(shè)置顏色
        g.setColor(Color.white);
        // 設(shè)置字體
        g.setFont(new Font("隸書", Font.BOLD, 20));
        String code = "";
        //構(gòu)造存儲(chǔ)字符的數(shù)組
        char[] a = {};
        //構(gòu)造存儲(chǔ)字符串的集合
        List<String> list = new ArrayList<String>();
        Random random = new Random();
        int x = 20;
        int y = 20;
        for (int i = 0; i < 4; i++) {
            // void rotate(double theta, double x, double y)
            // theta 弧度
            // hudu = jiaodu * Math.PI / 180;
            // 獲取正負(fù)30之間的角度
            int jiaodu = random.nextInt(60) - 30;
            double hudu = jiaodu * Math.PI / 180;
            g.rotate(hudu, x, y);
            // 獲取下標(biāo)
            int index = random.nextInt(words.length());
            // 返回指定下標(biāo)位置的字符,隨機(jī)獲取下標(biāo)
            char ch = words.charAt(index);
            //將字符存入字符數(shù)組中
            char[] chc = {ch};
            //使用字符數(shù)組構(gòu)造字符串
            String string = new String(chc);
            //將構(gòu)造好的字符串添加進(jìn)list集合中
            list.add(string);
            // 寫字符串
            g.drawString("" + ch, x, y);
            g.rotate(-hudu, x, y);
            x += 20;
        }
        for (int i = 0; i < list.size(); i++) {
            code += list.get(i);
        }
        //將驗(yàn)證碼存入上下文中
        servletContext.setAttribute("code", code);
        // 設(shè)置顏色
        g.setColor(Color.GREEN);
        int x1, x2, y1, y2;
        // 畫干擾線
        for (int i = 0; i < 4; i++) {
            x1 = random.nextInt(width);
            y1 = random.nextInt(height);
            x2 = random.nextInt(width);
            y2 = random.nextInt(height);
            g.drawLine(x1, y1, x2, y2);
        }
        // 輸出到客戶端
        try {
            ImageIO.write(image, "jpg", response.getOutputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

登錄驗(yàn)證:

@RequestMapping(value = "/user/login", method = {RequestMethod.POST, RequestMethod.GET})
    public void Login(@RequestBody UserBean userBean, HttpSession session, HttpServletResponse response) throws Exception {
        String code = userBean.getCode();
        //從上下文獲取存儲(chǔ)的驗(yàn)證碼
        String code1 = (String) servletContext.getAttribute("code");
        //賬號(hào)密碼為admin.且驗(yàn)證碼(忽略大小寫)輸入正確,則跳轉(zhuǎn)到登陸成功頁(yè)面
        if ("".equals(code) || code == null || !code.equalsIgnoreCase(code1)) {
            HttpServletResponseUtil.back(response, 202, "驗(yàn)證碼錯(cuò)誤!", null);
            return;
        }
        QueryWrapper<UserBean> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("username", userBean.getUsername());
        queryWrapper.eq("password", userBean.getPassword());
        List<UserBean> beans = mapper.selectList(queryWrapper);
        if (beans != null && beans.size() > 0) {
            log.info("登陸成功,用戶名:" + userBean.getUsername());
            log.info("登陸成功,密碼:" + userBean.getPassword());
            session.setAttribute("loginUser", userBean);
            HttpServletResponseUtil.back(response, 200, "登錄成功!", null);
        } else {
            HttpServletResponseUtil.back(response, -1, "賬號(hào)密碼錯(cuò)誤", null);
        }
 
    }

?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-794485.html

到了這里,關(guān)于后臺(tái)生成隨機(jī)驗(yàn)證碼驗(yàn)證登錄的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來(lái)自互聯(lián)網(wǎng)用戶投稿,該文觀點(diǎn)僅代表作者本人,不代表本站立場(chǎng)。本站僅提供信息存儲(chǔ)空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請(qǐng)注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實(shí)不符,請(qǐng)點(diǎn)擊違法舉報(bào)進(jìn)行投訴反饋,一經(jīng)查實(shí),立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費(fèi)用

相關(guān)文章

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請(qǐng)作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包