最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Java實現(xiàn)滑動驗證碼的示例代碼

 更新時間:2022年02月28日 15:52:46   作者:灰太狼_cxh  
這篇文章主要為大家介紹了如何用Java語言實現(xiàn)滑動驗證碼的生成,項目采用了springboot,maven等技術(shù),感興趣的小伙伴可以跟隨小編學(xué)習一下

功能:java實現(xiàn)滑動驗證碼

項目是采用springboot,maven

開發(fā)工具:采用idea

1.效果演示

2.后端代碼

控制層

@Controller
public class SliderCodeController {
 
    @Autowired
    ResourceLoader resourceLoader;
 
    @Autowired
    private FileUtil fileUtil;
 
    // 設(shè)置橫軸位置緩存
    public static Cache< String, Integer > cacheg = CacheBuilder.newBuilder().expireAfterWrite(60, TimeUnit.SECONDS)
            .maximumSize(666666).build();
 
    @GetMapping
    @RequestMapping("index")
    public String test(HttpServletRequest request, Model model) throws IOException {
        return "index";
    }
 
 
    @GetMapping
    @RequestMapping("getImg")
    public @ResponseBody
    Map< String, Object > getPic(HttpServletRequest request) throws IOException {
        try {
            File targetFile = fileUtil.getFile("target");
            File tempImgFile = fileUtil.getFile("temp");
            Map < String, Object > resultMap = VerifyImageUtil.pictureTemplatesCut(tempImgFile, targetFile);
            // 生成流水號,這里就使用時間戳代替
            String lno = Calendar.getInstance().getTimeInMillis() + "";
            cacheg.put(lno, Integer.valueOf(resultMap.get("xWidth") + ""));
            resultMap.put("capcode", lno);
            // 移除橫坐標送前端
            resultMap.remove("xWidth");
            return resultMap;
        }
        catch (Exception e) {
            e.printStackTrace();
            return null;
        }
 
    }
 
 
    @GetMapping
    @RequestMapping("checkImgCode")
    public @ResponseBody Map < String, Object > checkcapcode(@RequestParam("xpos") int xpos,
                                                             @RequestParam("capcode") String capcode, HttpServletRequest request) throws IOException {
        Map < String, Object > result = new HashMap< String, Object >();
        Integer x = cacheg.getIfPresent(capcode);
        if (x == null) {
            // 超時
            result.put("code", 3);
        }
        else if (xpos - x > 5 || xpos - x < -5) {
            // 驗證失敗
            result.put("code", 2);
        }
        else {
            // 驗證成功
            result.put("code", 1);
        }
        return result;
    }
}

工具類

@Component
public class FileUtil {
 
    @Value("${file.path}")
    private String filePath;
 
    @Value("${file.target.path}")
    private String targetFilePath;
 
    @Value("${file.target.num}")
    private Integer targetfileNum;
 
    @Value("${file.temp.path}")
    private String tempFilePath;
 
    @Value("${file.temp.num}")
    private Integer tempfileNum;
 
    public File getFile(String type){
        int num = 0;
        String imgType = ".jpg";
        String oldFilePath = "";
        if(type.equals("target")){
            num = new Random().nextInt(targetfileNum)  + 1;
            oldFilePath = targetFilePath;
        } else  if(type.equals("temp")){
            num = new Random().nextInt(tempfileNum)  + 1;
            imgType = "-w.png";
            oldFilePath = tempFilePath;
        }
        String path = filePath;
        String fileImg =   num + imgType;
        String filePath = path + fileImg;
        File pathFile = new File(path);
        if(!pathFile.exists()){
            pathFile.mkdirs();
        }
        File file = new File(filePath);
        if(!file.exists()){
            try {
                file.createNewFile();
                ClassPathResource classPathResource = new ClassPathResource(oldFilePath + fileImg);
                InputStream inputStream = classPathResource.getInputStream();
                if(inputStream.available() != 0){
                    FileUtils.copyInputStreamToFile(inputStream, file);
                }
                inputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }
 
}

3.前端頁面

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>滑動驗證碼</title>
<link rel="stylesheet" href="/css/slide.css" rel="external nofollow" >
<script src="/js/jquery-1.11.1.min.js"></script>
<script src="/js/jquery.lgyslide.js"></script>
</head>
<body>
	<div id="imgscode"></div>
	<script>
		$(function() {
			setTimeout(function() {
				createcode();
			}, 1000)
		}());
		//顯示驗證碼
		function createcode() {
			$
					.ajax({
						type : 'POST',
						url : '/getImg',
						dataType : 'json',
						success : function(data) {
							if (data != null) {
								$("#imgscode")
										.imgcode(
												{
													frontimg : 'data:image/png;base64,'
															+ data.slidingImage,
													backimg : 'data:image/png;base64,'
															+ data.backImage,
													yHeight : data.yHeight,
													refreshcallback : function() {
														//刷新驗證碼
														createcode();
													},
													callback : function(msg) {
														console.log(msg);
														var $this = this;
														$
																.ajax({
																	type : 'POST',
																	url : '/checkImgCode',
																	data : {
																		xpos : msg.xpos,
																		capcode : data.capcode
																	},
																	dataType : 'json',
																	success : function(
																			data) {
																		console
																				.log(data)
																		if (data.code == 1) {
																			$this
																					.getsuccess();
																		} else {
																			if (data.code == 4) {
																				createcode();
																			} else if (data.code == 3) {
																				$this
																						.getfail("驗證碼過期,請刷新");
																			} else {
																				$this
																						.getfail("驗證不通過");
																			}
																		}
 
																	}
																})
													}
												});
							}
						}
					})
		}
	</script>
</body>
</html>

到此這篇關(guān)于Java實現(xiàn)滑動驗證碼的示例代碼的文章就介紹到這了,更多相關(guān)Java滑動驗證碼內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Mybatis-Plus自動填充更新操作相關(guān)字段的實現(xiàn)

    Mybatis-Plus自動填充更新操作相關(guān)字段的實現(xiàn)

    數(shù)據(jù)庫表中應(yīng)該都要有create_time、update_time字段;那么在開發(fā)中,對于這些共有字段的處理應(yīng)該要進行統(tǒng)一,這樣就可以簡化我們的開發(fā)過程。那么本文就對Mybatis-Plus中的字段自動填充進行記錄
    2021-11-11
  • 淺談Java中強制類型轉(zhuǎn)換的問題

    淺談Java中強制類型轉(zhuǎn)換的問題

    下面小編就為大家?guī)硪黄獪\談Java中強制類型轉(zhuǎn)換的問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2016-05-05
  • Java多線程下載的實現(xiàn)方法

    Java多線程下載的實現(xiàn)方法

    復(fù)習多線程的時候,練習了下,順便記錄一下:
    2013-03-03
  • 詳解Java中格式化日期的DateFormat與SimpleDateFormat類

    詳解Java中格式化日期的DateFormat與SimpleDateFormat類

    DateFormat其本身是一個抽象類,SimpleDateFormat 類是DateFormat類的子類,一般情況下來講DateFormat類很少會直接使用,而都使用SimpleDateFormat類完成,下面我們具體來看一下兩個類的用法:
    2016-05-05
  • Java的AQS基本原理詳細分析

    Java的AQS基本原理詳細分析

    這篇文章主要介紹了Java的AQS基本原理詳細分析,AQS是Abstract Queued Synchronizer的簡稱,AQS提供了一種實現(xiàn)阻塞鎖和一系列依賴FIFO等待隊列的同步器的框架,本文主要講解分析其基本原理,需要的朋友可以參考下
    2024-01-01
  • SpringBoot預(yù)防XSS攻擊的實現(xiàn)

    SpringBoot預(yù)防XSS攻擊的實現(xiàn)

    XSS攻擊是一種在web應(yīng)用中的計算機安全漏洞,它允許惡意web用戶將代碼植入到提供給其它用戶使用的頁面,本文主要介紹了SpringBoot預(yù)防XSS攻擊的實現(xiàn),感興趣的可以了解一下
    2023-08-08
  • springboot2.x實現(xiàn)oauth2授權(quán)碼登陸的方法

    springboot2.x實現(xiàn)oauth2授權(quán)碼登陸的方法

    這篇文章主要介紹了springboot2.x實現(xiàn)oauth2授權(quán)碼登陸的方法,本文給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下
    2019-08-08
  • Java棧的應(yīng)用之括號匹配算法實例分析

    Java棧的應(yīng)用之括號匹配算法實例分析

    這篇文章主要介紹了Java棧的應(yīng)用之括號匹配算法,結(jié)合實例形式分析了Java使用棧實現(xiàn)括號匹配算法的相關(guān)原理、操作技巧與注意事項,需要的朋友可以參考下
    2020-03-03
  • 解決try-catch捕獲異常信息后Spring事務(wù)失效的問題

    解決try-catch捕獲異常信息后Spring事務(wù)失效的問題

    這篇文章主要介紹了解決try-catch捕獲異常信息后Spring事務(wù)失效的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-06-06
  • 淺析SpringBoot多數(shù)據(jù)源實現(xiàn)方案

    淺析SpringBoot多數(shù)據(jù)源實現(xiàn)方案

    現(xiàn)在很多項目的開發(fā)過程中,可能涉及到多個數(shù)據(jù)源,像讀寫分離的場景,或者因為業(yè)務(wù)復(fù)雜,導(dǎo)致不同的業(yè)務(wù)部署在不同的數(shù)據(jù)庫上,那么這樣的場景,我們應(yīng)該如何在代碼中簡潔方便的切換數(shù)據(jù)源呢,本文介紹SpringBoot多數(shù)據(jù)源實現(xiàn)方案,感興趣的朋友跟隨小編一起看看吧
    2024-02-02

最新評論

广南县| 买车| 亳州市| 海安县| 大渡口区| 龙陵县| 密云县| 鲁山县| 若尔盖县| 浮山县| 东山县| 青铜峡市| 紫金县| 宁陕县| 庆元县| 三台县| 景洪市| 搜索| 鹰潭市| 鄱阳县| 吉安市| 那坡县| 镇远县| 铜梁县| 洛宁县| 睢宁县| 时尚| 南投市| 鄂伦春自治旗| 长治县| 香河县| 吉林市| 阿瓦提县| 黑水县| 扬中市| 张家口市| 西畴县| 于都县| 神池县| 江安县| 平潭县|