亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

vue+springboot實現登錄驗證碼

瀏覽:2日期:2022-09-29 10:49:42

本文實例為大家分享了vue+springboot實現登錄驗證碼的具體代碼,供大家參考,具體內容如下

先看效果圖

vue+springboot實現登錄驗證碼

在login頁面添加驗證碼html

vue+springboot實現登錄驗證碼

在后端pom文件添加kaptcha依賴

<dependency> <groupId>com.github.penggle</groupId> <artifactId>kaptcha</artifactId> <version>2.3.2</version></dependency>

創建KaptchaConfig工具類

package com.brilliance.module.system.controller.util; import com.google.code.kaptcha.impl.DefaultKaptcha;import com.google.code.kaptcha.util.Config;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration; import java.util.Properties; @Configurationpublic class KaptchaConfig { @Bean public DefaultKaptcha getDefaultKaptcha() {DefaultKaptcha defaultKaptcha = new DefaultKaptcha();Properties properties = new Properties();// 圖片寬properties.setProperty('kaptcha.image.width', '180');// 圖片高properties.setProperty('kaptcha.image.height', '50');// 圖片邊框properties.setProperty('kaptcha.border', 'yes');// 邊框顏色properties.setProperty('kaptcha.border.color', '105,179,90');// 字體顏色properties.setProperty('kaptcha.textproducer.font.color', 'blue');// 字體大小properties.setProperty('kaptcha.textproducer.font.size', '40');// session keyproperties.setProperty('kaptcha.session.key', 'code');// 驗證碼長度properties.setProperty('kaptcha.textproducer.char.length', '4');// 字體properties.setProperty('kaptcha.textproducer.font.names', '宋體,楷體,微軟雅黑');Config config = new Config(properties);defaultKaptcha.setConfig(config);return defaultKaptcha; }}

Controller中,生成的驗證碼存儲在了redis中, 用于以后作校驗(redis的配置以及依賴自行百度)

@RestController@RequestMapping('/api/user')@Api(tags = '系統用戶管理')public class SysUserController extends AbstractController{ @Autowired private SysUserService sysUserService; @Autowired private DefaultKaptcha defaultKaptcha; @Autowired RedisTemplate redisTemplate; @GetMapping('/createImageCode') public void createImageCode(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setHeader('Cache-Control', 'no-store, no-cache'); response.setContentType('image/jpeg'); // 生成文字驗證碼 String text = defaultKaptcha.createText(); // 生成圖片驗證碼 BufferedImage image = defaultKaptcha.createImage(text); // 這里我們使用redis緩存驗證碼的值,并設置過期時間為60秒 redisTemplate.opsForValue().set('imageCode',text,60, TimeUnit.SECONDS); ServletOutputStream out = response.getOutputStream(); ImageIO.write(image, 'jpg', out); out.flush(); out.close(); }

生成之后,在登錄界面輸入驗證碼需要進行校驗輸入的是否正確

vue+springboot實現登錄驗證碼

在登錄按鈕外層加一次請求判斷,驗證輸入的驗證碼是否正確,根據返回值提示錯誤信息

vue+springboot實現登錄驗證碼

@PostMapping('/compareCode')public RESULT compareCode(@RequestBody String verificationCode) { if(!redisTemplate.hasKey('imageCode')) { return RESULT.error(500,'驗證碼已過期'); } String code = redisTemplate.opsForValue().get('imageCode').toString(); if(StringUtils.equals(verificationCode,code)) { return RESULT.ok(); } else { return RESULT.error(500,'驗證碼輸入錯誤'); }}

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Spring
相關文章:
主站蜘蛛池模板: 特黄视频| 日韩亚洲欧美一区二区三区 | 日韩精品视频在线 | 国产成人综合高清在线观看 | 日韩黄色小视频 | 亚洲综合黄色 | 国产三级日本三级在线播放 | 黄色片aaa | 在线观看黄色一级片 | 亚洲日本高清成人aⅴ片 | 国产在线麻豆精品 | 国产福利视频在线观看 | 寡妇一级a毛片免费播放 | 色成人亚洲 | 免费看av在线网站网址 | 黄色在线视频免费看 | 进来综合网 | 日本一级特黄aa大片在线观看 | 美国一级大黄大黄大色毛片a | 欧美国产人妖另类色视频 | 欧美r级毛片在线播放 | 久久久久久久久综合 | 免费中文字幕乱码电影麻豆网 | 一本伊大人香蕉在线观看 | www日韩免费高清视频 | 国产女人综合久久精品视 | 亚洲黄色片 | 国产精品成人aaaaa网站 | 午夜成人免费影院 | 黄色网址在线免费观看 | 国产精品热久久毛片 | 大尺度一级毛片波多野结衣 | 韩国r级福利视频在线观看网站 | 亚洲色图综合区 | 久久久久国产精品免费免费不卡 | 狠狠色噜噜狠狠狠97影音先锋 | 久久99国产精品久久99果冻传媒 | 国产大片91精品免费观看男同 | 免费一级片在线 | 国产s色| 国产午夜精品一区二区三区嫩草 |