欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java實(shí)現(xiàn)通過(guò)時(shí)間獲取8位驗(yàn)證碼

 更新時(shí)間:2023年11月10日 10:05:23   作者:シ風(fēng)箏  
這篇文章主要為大家詳細(xì)介紹了Java如何通過(guò)時(shí)間獲取8位驗(yàn)證碼(每?jī)蓚€(gè)小時(shí)生成一個(gè)),文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

1.需求

要求是很簡(jiǎn)單的,每個(gè)驗(yàn)證碼的有效時(shí)間是2小時(shí),這個(gè)并不是收到驗(yàn)證碼開(kāi)始計(jì)時(shí)的,而是每個(gè)兩小時(shí)的時(shí)間段使用的是相同的驗(yàn)證碼。

2.代碼實(shí)現(xiàn)

2.1 依賴

<dependency>
	<groupId>gov.nist.math</groupId>
	<artifactId>jama</artifactId>
	<version>1.0.3</version>
</dependency>

<dependency>
	<groupId>commons-codec</groupId>
	<artifactId>commons-codec</artifactId>
	<version>1.6</version>
</dependency>

2.2 時(shí)間參數(shù)處理方法

將2個(gè)小時(shí)處理為相同的值:

@Slf4j
public class VerificationCodeUtil {

    /**
     * 時(shí)間字符串
     *
     * @param dateStr yyyy-MM-dd HH:mm:ss
     */
    public static String getCode(String dateStr) {
        int dataStrLength = 13;
        try {
            if (dateStr.length() >= dataStrLength) {
                String yearMonthDay = dateStr.substring(0, 10);
                int hour = Integer.parseInt(dateStr.substring(11, 13));
                int twoHour = 2;
                if (hour % twoHour != 0) {
                    hour--;
                }
                String md5Str = DigestUtils.md5Hex("vc#" + yearMonthDay + hour);
                return getCodeByMd5(md5Str);
            } else {
                log.error("dateStr [{}] not match format [yyyy-MM-dd HH:mm:ss]!", dateStr);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("dateStr [{}] not match format [yyyy-MM-dd HH:mm:ss]!", dateStr);
        }
        return dateStr;
    }
}

2.3 截取驗(yàn)證碼方法

@Slf4j
public class VerificationCodeUtil {
    // 對(duì)指定字符串生成驗(yàn)證碼
    private static String getCodeByMd5(String md5Str) {
        try {
            byte[] md5 = md5Str.getBytes();
            double[][] preMatrix = new double[4][8];
            for (int j = 0; j < 4; j++) {
                for (int k = 0; k < 8; k++) {
                    preMatrix[j][k] = md5[j * 8 + k];
                }
            }
            Matrix matrix = new Matrix(preMatrix);
            Matrix matrix1 = matrix.getMatrix(1, 2, 2, 5);
            Matrix matrix2 = matrix.transpose();
            Matrix matrix21 = matrix2.getMatrix(0, 3, 0, 3);
            Matrix matrix22 = matrix2.getMatrix(4, 7, 0, 3);
            Matrix matrix3 = matrix21.plus(matrix22);
            Matrix result = matrix1.times(matrix3);

            double[][] re = result.getArray();
            StringBuilder str = new StringBuilder();
            for (double[] doubles : re) {
                for (double aDouble : doubles) {
                    int a = (int) aDouble % 16;
                    str.append(Integer.toHexString(a));
                }
            }
            return str.toString().toUpperCase();
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

2.4 驗(yàn)證方法

@Slf4j
public class VerificationCodeUtil {
    public static void main(String[] args) {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        DateTime parse = DateUtil.parse("2023-11-09 23:59:59", "yyyy-MM-dd HH:mm:ss");
        String format = df.format(parse);
        System.out.println(getCode(format));
        // 00:00 3A756DFC
        // 00:59 3A756DFC
        // 01:59 3A756DFC
        // 01:59 3A756DFC
        // 02:00 9E937D4B
        // 02:59 9E937D4B
        // 03:00 9E937D4B
        // 22:00 D014DD79
        // 23:59 D014DD79        
    }
}

3.總結(jié)

很簡(jiǎn)單的算法分享。優(yōu)點(diǎn):

  • 不需要將生成的驗(yàn)證碼緩存。
  • 時(shí)間入?yún)ⅲ軌蛑貜?fù)獲取相同的值。

以上就是Java實(shí)現(xiàn)通過(guò)時(shí)間獲取8位驗(yàn)證碼的詳細(xì)內(nèi)容,更多關(guān)于Java獲取驗(yàn)證碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • Windows系統(tǒng)下如何查找JDK的安裝路徑

    Windows系統(tǒng)下如何查找JDK的安裝路徑

    這篇文章主要介紹了Windows系統(tǒng)下如何查找JDK的安裝路徑,文中介紹了三種方法,分別是通過(guò)命令行檢查、使用verbose選項(xiàng)查找jre目錄、以及查看環(huán)境變量,需要的朋友可以參考下
    2025-03-03
  • Java簡(jiǎn)單數(shù)據(jù)加密方法DES實(shí)現(xiàn)過(guò)程解析

    Java簡(jiǎn)單數(shù)據(jù)加密方法DES實(shí)現(xiàn)過(guò)程解析

    這篇文章主要介紹了Java簡(jiǎn)單數(shù)據(jù)加密方法DES實(shí)現(xiàn)過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-12-12
  • SpringBoot集成和使用RabbitMQ方式

    SpringBoot集成和使用RabbitMQ方式

    本文介紹了如何在SpringBoot項(xiàng)目中集成RabbitMQ,并結(jié)合死信隊(duì)列實(shí)現(xiàn)延時(shí)消息,通過(guò)這些配置和機(jī)制,開(kāi)發(fā)者可以在分布式系統(tǒng)中構(gòu)建更為靈活和可靠的消息傳遞系統(tǒng)
    2024-12-12
  • SpringBoot項(xiàng)目中改變web服務(wù)的路徑的兩種方案

    SpringBoot項(xiàng)目中改變web服務(wù)的路徑的兩種方案

    這篇文章主要介紹了SpringBoot項(xiàng)目中改變web服務(wù)的路徑的兩種方案,通過(guò)代碼示例講解的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下
    2024-08-08
  • java socket 詳細(xì)介紹

    java socket 詳細(xì)介紹

    本篇文章小編為大家介紹,java socket 詳細(xì)介紹。需要的朋友參考下
    2013-04-04
  • Java8 Zip 壓縮與解壓縮的實(shí)現(xiàn)

    Java8 Zip 壓縮與解壓縮的實(shí)現(xiàn)

    這篇文章主要介紹了Java8 Zip 壓縮與解壓縮的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2020-03-03
  • SpringMVC+Shiro的基本使用及功能介紹

    SpringMVC+Shiro的基本使用及功能介紹

    本文給大家介紹SpringMVC+Shiro的基本使用,Apache?Shiro是Java的一個(gè)安全框架,Shiro本身無(wú)法知道所持有令牌的用戶是否合法,因?yàn)槌隧?xiàng)目的設(shè)計(jì)人員恐怕誰(shuí)都無(wú)法得知,本文只介紹最常見(jiàn)也最重要的一種實(shí)現(xiàn)方式數(shù)據(jù)庫(kù)查詢
    2022-04-04
  • Springboot異步事件配置和使用示例詳解

    Springboot異步事件配置和使用示例詳解

    Spring框架提供了一套事件處理機(jī)制,允許應(yīng)用在各個(gè)組件之間傳遞狀態(tài)信息,自定義事件通常繼承自ApplicationEvent類,Springboot通過(guò)自動(dòng)配置簡(jiǎn)化了異步處理的配置,實(shí)現(xiàn)開(kāi)箱即用,Spring事件模型核心是觀察者模式,適用于解耦和提高響應(yīng)速度
    2024-10-10
  • java中@EnableAutoConfiguration注解使用

    java中@EnableAutoConfiguration注解使用

    在Spring Boot框架中,@EnableAutoConfiguration是一種非常重要的注解,本文就來(lái)介紹一下java中@EnableAutoConfiguration注解使用,感興趣的可以了解一下
    2023-11-11
  • java 取模與取余的區(qū)別說(shuō)明

    java 取模與取余的區(qū)別說(shuō)明

    這篇文章主要介紹了java 取模與取余的區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-10-10

最新評(píng)論