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

SpringBoot項目添加2FA雙因素身份認證

這篇具有很好參考價值的文章主要介紹了SpringBoot項目添加2FA雙因素身份認證。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

什么是 2FA(雙因素身份驗證)?

雙因素身份驗證(2FA)是一種安全系統(tǒng),要求用戶提供兩種不同的身份驗證方式才能訪問某個系統(tǒng)或服務。國內(nèi)普遍做短信驗證碼這種的用的比較少,不過在國外的網(wǎng)站中使用雙因素身份驗證的還是很多的。用戶通過使用驗證器掃描二維碼,就能在app上獲取登錄的動態(tài)口令,進一步加強了賬戶的安全性。

主要步驟

pom.xml中增加依賴

<!-- 用于SecureKey生成 -->
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.15</version>
</dependency>
<!-- 二維碼依賴 -->
<dependency>
    <groupId>org.iherus</groupId>
    <artifactId>qrext4j</artifactId>
    <version>1.3.1</version>
</dependency>

用戶表中增加secretKey列

為用戶綁定secretKey字段,用以生成二維碼及后期校驗
SpringBoot項目添加2FA雙因素身份認證

工具類

谷歌身份驗證器工具類


/**
 * 谷歌身份驗證器工具類
 */
public class GoogleAuthenticator {

    /**
     * 時間前后偏移量
     * 用于防止客戶端時間不精確導致生成的TOTP與服務器端的TOTP一直不一致
     * 如果為0,當前時間為 10:10:15
     * 則表明在 10:10:00-10:10:30 之間生成的TOTP 能校驗通過
     * 如果為1,則表明在
     * 10:09:30-10:10:00
     * 10:10:00-10:10:30
     * 10:10:30-10:11:00 之間生成的TOTP 能校驗通過
     * 以此類推
     */
    private static int WINDOW_SIZE = 0;

    /**
     * 加密方式,HmacSHA1、HmacSHA256、HmacSHA512
     */
    private static final String CRYPTO = "HmacSHA1";

    /**
     * 生成密鑰,每個用戶獨享一份密鑰
     *
     * @return
     */
    public static String getSecretKey() {
        SecureRandom random = new SecureRandom();
        byte[] bytes = new byte[20];
        random.nextBytes(bytes);
        Base32 base32 = new Base32();
        String secretKey = base32.encodeToString(bytes);
        // make the secret key more human-readable by lower-casing and
        // inserting spaces between each group of 4 characters
        return secretKey.toUpperCase();
    }

    /**
     * 生成二維碼內(nèi)容
     *
     * @param secretKey 密鑰
     * @param account   賬戶名
     * @param issuer    網(wǎng)站地址(可不寫)
     * @return
     */
    public static String getQrCodeText(String secretKey, String account, String issuer) {
        String normalizedBase32Key = secretKey.replace(" ", "").toUpperCase();
        try {
            return "otpauth://totp/"
                    + URLEncoder.encode((!StringUtils.isEmpty(issuer) ? (issuer + ":") : "") + account, "UTF-8").replace("+", "%20")
                    + "?secret=" + URLEncoder.encode(normalizedBase32Key, "UTF-8").replace("+", "%20")
                    + (!StringUtils.isEmpty(issuer) ? ("&issuer=" + URLEncoder.encode(issuer, "UTF-8").replace("+", "%20")) : "");
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException(e);
        }
    }

    /**
     * 獲取驗證碼
     *
     * @param secretKey
     * @return
     */
    public static String getCode(String secretKey) {
        String normalizedBase32Key = secretKey.replace(" ", "").toUpperCase();
        Base32 base32 = new Base32();
        byte[] bytes = base32.decode(normalizedBase32Key);
        String hexKey = Hex.encodeHexString(bytes);
        long time = (System.currentTimeMillis() / 1000) / 30;
        String hexTime = Long.toHexString(time);
        return TOTP.generateTOTP(hexKey, hexTime, "6", CRYPTO);
    }

    /**
     * 檢驗 code 是否正確
     *
     * @param secret 密鑰
     * @param code   code
     * @param time   時間戳
     * @return
     */
    public static boolean checkCode(String secret, long code, long time) {
        Base32 codec = new Base32();
        byte[] decodedKey = codec.decode(secret);
        // convert unix msec time into a 30 second "window"
        // this is per the TOTP spec (see the RFC for details)
        long t = (time / 1000L) / 30L;
        // Window is used to check codes generated in the near past.
        // You can use this value to tune how far you're willing to go.
        long hash;
        for (int i = -WINDOW_SIZE; i <= WINDOW_SIZE; ++i) {
            try {
                hash = verifyCode(decodedKey, t + i);
            } catch (Exception e) {
                // Yes, this is bad form - but
                // the exceptions thrown would be rare and a static
                // configuration problem
                // e.printStackTrace();
                throw new RuntimeException(e.getMessage());
            }
            if (hash == code) {
                return true;
            }
        }
        return false;
    }

    /**
     * 根據(jù)時間偏移量計算
     *
     * @param key
     * @param t
     * @return
     * @throws NoSuchAlgorithmException
     * @throws InvalidKeyException
     */
    private static long verifyCode(byte[] key, long t) throws NoSuchAlgorithmException, InvalidKeyException {
        byte[] data = new byte[8];
        long value = t;
        for (int i = 8; i-- > 0; value >>>= 8) {
            data[i] = (byte) value;
        }
        SecretKeySpec signKey = new SecretKeySpec(key, CRYPTO);
        Mac mac = Mac.getInstance(CRYPTO);
        mac.init(signKey);
        byte[] hash = mac.doFinal(data);
        int offset = hash[20 - 1] & 0xF;
        // We're using a long because Java hasn't got unsigned int.
        long truncatedHash = 0;
        for (int i = 0; i < 4; ++i) {
            truncatedHash <<= 8;
            // We are dealing with signed bytes:
            // we just keep the first byte.
            truncatedHash |= (hash[offset + i] & 0xFF);
        }
        truncatedHash &= 0x7FFFFFFF;
        truncatedHash %= 1000000;
        return truncatedHash;
    }

    public static void main(String[] args) {
        for (int i = 0; i < 100; i++) {
            String secretKey = getSecretKey();
            System.out.println("secretKey:" + secretKey);
            String code = getCode(secretKey);
            System.out.println("code:" + code);
            boolean b = checkCode(secretKey, Long.parseLong(code), System.currentTimeMillis());
            System.out.println("isSuccess:" + b);
        }
    }
}

二維碼工具類

/**
 * 驗證碼生成工具類
 */
public class TOTP {

    private static final int[] DIGITS_POWER = {1, 10, 100, 1000, 10000, 100000, 1000000, 10000000, 100000000};

    /**
     * This method uses the JCE to provide the crypto algorithm. HMAC computes a
     * Hashed Message Authentication Code with the crypto hash algorithm as a
     * parameter.
     *
     * @param crypto   : the crypto algorithm (HmacSHA1, HmacSHA256, HmacSHA512)
     * @param keyBytes : the bytes to use for the HMAC key
     * @param text     : the message or text to be authenticated
     */
    private static byte[] hmac_sha(String crypto, byte[] keyBytes, byte[] text) {
        try {
            Mac hmac;
            hmac = Mac.getInstance(crypto);
            SecretKeySpec macKey = new SecretKeySpec(keyBytes, "RAW");
            hmac.init(macKey);
            return hmac.doFinal(text);
        } catch (GeneralSecurityException gse) {
            throw new UndeclaredThrowableException(gse);
        }
    }

    /**
     * This method converts a HEX string to Byte[]
     *
     * @param hex : the HEX string
     * @return: a byte array
     */
    private static byte[] hexStr2Bytes(String hex) {
        // Adding one byte to get the right conversion
        // Values starting with "0" can be converted
        byte[] bArray = new BigInteger("10" + hex, 16).toByteArray();

        // Copy all the REAL bytes, not the "first"
        byte[] ret = new byte[bArray.length - 1];
        System.arraycopy(bArray, 1, ret, 0, ret.length);
        return ret;
    }

    /**
     * This method generates a TOTP value for the given set of parameters.
     *
     * @param key          : the shared secret, HEX encoded
     * @param time         : a value that reflects a time
     * @param returnDigits : number of digits to return
     * @param crypto       : the crypto function to use
     * @return: a numeric String in base 10 that includes
     */
    public static String generateTOTP(String key, String time, String returnDigits, String crypto) {
        int codeDigits = Integer.decode(returnDigits);
        String result = null;

        // Using the counter
        // First 8 bytes are for the movingFactor
        // Compliant with base RFC 4226 (HOTP)
        while (time.length() < 16) {
            time = "0" + time;
        }

        // Get the HEX in a Byte[]
        byte[] msg = hexStr2Bytes(time);
        byte[] k = hexStr2Bytes(key);
        byte[] hash = hmac_sha(crypto, k, msg);

        // put selected bytes into result int
        int offset = hash[hash.length - 1] & 0xf;

        int binary = ((hash[offset] & 0x7f) << 24)
                | ((hash[offset + 1] & 0xff) << 16)
                | ((hash[offset + 2] & 0xff) << 8) | (hash[offset + 3] & 0xff);

        int otp = binary % DIGITS_POWER[codeDigits];

        result = Integer.toString(otp);
        while (result.length() < codeDigits) {
            result = "0" + result;
        }
        return result;
    }
}

Service

@Transactional(isolation = Isolation.REPEATABLE_READ, propagation = Propagation.REQUIRED)
@Service
public class TwoFAService {
    @Autowired
    private UserMapper userMapper;

   /**
    * 獲取SecureKey
    */
    public String getSecureKey(Integer userId) {
        User user = userMapper.selectUserById(userId);
        return user.getSecretKey();
    }

    /**
     * 更新secureKey
     */
    public Integer updateSecureKey(Integer userId, String secureKey) {
        return userMapper.updateSecureKeyById(userId, secureKey);
    }

   /**
    * 校驗動態(tài)碼
    */
    public boolean chek2FACode(User user, String twoFACode) throws Exception {
        String secretKey = user.getSecretKey();
        // 沒綁定設備就先驗證通過
        if(secretKey == null || secretKey.isEmpty()) {
            return true;
        } else  {
           if(twoFACode.isEmpty()) { throw new Exception("已綁定設備,請輸入動態(tài)碼"); }
           boolean checkRes = GoogleAuthenticator.checkCode(secretKey, Long.parseLong(twoFACode), System.currentTimeMillis());
           if(!checkRes) {
               throw new Exception("動態(tài)碼錯誤");
           } else {
               return true;
           }
        }
    }
}

Controller

用戶登錄中增加兩步驗證:

@Controller
@RequestMapping(value = "/mgr")
public class UserController {
    @Autowired
    private UserService userService;
    @Autowired
    private LogService logService;
    @Autowired
    private TwoFAService twoFAService;

    /**
     * @Description: 用戶登錄
     */
    @RequestMapping(value = "/user/login", method = RequestMethod.POST)
    @ResponseBody
    public GlobalResult login(String userCode, String userPwd, String twoFACode) {
        try {
            UsernamePasswordToken token = new UsernamePasswordToken(userCode, userPwd);
            Subject subject = SecurityUtils.getSubject();
            subject.login(token);

            // 2FA驗證
            User user = (User) subject.getPrincipal();
            twoFAService.chek2FACode(user, twoFACode);

            Log log = new Log();
            .......
        }
    }
}

兩步驗證的Controler文章來源地址http://www.zghlxwxcb.cn/news/detail-857589.html

@RestController
@RequestMapping(value = "/2fa")
public class TwoFAController {
    @Autowired
    private TwoFAService twoFAService;

    /**
     * 生成二維碼信息對象
     */
    @GetMapping("/getQrcode")
    public QrCodeResponse getQrcode(@RequestParam("userId") Integer userId, @RequestParam("userCode") String userCode, HttpServletResponse response) throws Exception {
        try {
            String secretKey = twoFAService.getSecureKey(userId);
            QrCodeResponse qrCodeResponse = new QrCodeResponse();
            if(secretKey == null || secretKey.isEmpty()) {
                secretKey = GoogleAuthenticator.getSecretKey();
                qrCodeResponse.setBind(false);
                // userMapper.updateSecureKeyById(userId, secretKey);
            } else {
                qrCodeResponse.setBind(true);
            }

            // 生成二維碼內(nèi)容
            String qrCodeText = GoogleAuthenticator.getQrCodeText(secretKey, userCode, "suggest-mgr");
            // 以流的形式返回生成二維碼輸出
            // new SimpleQrcodeGenerator().generate(qrCodeText).toStream(response.getOutputStream());
            BufferedImage image = new SimpleQrcodeGenerator().generate(qrCodeText).getImage();
            // 將圖片轉(zhuǎn)換為Base64字符串
            String base64Image = convertImageToBase64(image);
            qrCodeResponse.setQrCodeText(secretKey);
            qrCodeResponse.setBase64Image(base64Image);

            return qrCodeResponse;
        } catch (Exception e) {
            // 處理異常
            e.printStackTrace();
            return null; // 或者返回適當?shù)腻e誤信息
        }
    }

    /**
     * 更新SecretKey
     * @param userId
     * @param secretKey
     */
    @GetMapping("/updateSecretKey")
    public void updateSecretKey(@RequestParam("userId") Integer userId, @RequestParam("secretKey") String secretKey) {
        twoFAService.updateSecureKey(userId, secretKey);
    }

    /**
     * 獲取新的secretKey 重置用
     * @param userId
     * @param userCode
     * @return
     */
    @GetMapping("/getNewSecretKey")
    public QrCodeResponse getNewSecretKey(@RequestParam("userId") Integer userId, @RequestParam("userCode") String userCode, HttpServletResponse response) throws Exception {
        try {
            String secretKey = secretKey = GoogleAuthenticator.getSecretKey();
            QrCodeResponse qrCodeResponse = new QrCodeResponse();
            qrCodeResponse.setBind(false);

            // 生成二維碼內(nèi)容
            String qrCodeText = GoogleAuthenticator.getQrCodeText(secretKey, userCode, "xxx-site");
            BufferedImage image = new SimpleQrcodeGenerator().generate(qrCodeText).getImage();
            // 將圖片轉(zhuǎn)換為Base64字符串
            String base64Image = convertImageToBase64(image);
            qrCodeResponse.setQrCodeText(secretKey);
            qrCodeResponse.setBase64Image(base64Image);

            // 返回包含qrCodeText和Base64編碼圖片的信息
            return qrCodeResponse;
        } catch (Exception e) {
            // 處理異常
            e.printStackTrace();
            return null; // 或者返回適當?shù)腻e誤信息
        }
    }

    /**
     * 將圖片文件流轉(zhuǎn)為base64
     */
    private String convertImageToBase64(BufferedImage image) {
        try {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            ImageIO.write(image, "png", baos);
            byte[] imageBytes = baos.toByteArray();
            return Base64.getEncoder().encodeToString(imageBytes);
        } catch (Exception e) {
            // 處理異常
            return "";
        }
    }

    static public class QrCodeResponse {
        private String secretKey;
        private String base64Image;
        private boolean isBind;

        public String getSecretKey() {
            return secretKey;
        }

        public void setSecretKeyt(String secretKey) {
            this.secretKey = secretKey;
        }

        public String getBase64Image() {
            return base64Image;
        }

        public void setBase64Image(String base64Image) {
            this.base64Image = base64Image;
        }

        public boolean isBind() {
            return isBind;
        }

        public void setBind(boolean bind) {
            isBind = bind;
        }
    }
}

常用2FA驗證工具

  1. Google Authenticator: google play, apple store
  2. Microsoft Authenticator: google play , apple store
  3. AuthenticatorPro(開源):https://github.com/jamie-mh/AuthenticatorPro

到了這里,關于SpringBoot項目添加2FA雙因素身份認證的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關文章,希望大家以后多多支持TOY模板網(wǎng)!

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

領支付寶紅包贊助服務器費用

相關文章

  • 3-2. SpringBoot項目集成【用戶身份認證】實戰(zhàn) 【實戰(zhàn)核心篇】基于JWT生成和校驗Token

    3-2. SpringBoot項目集成【用戶身份認證】實戰(zhàn) 【實戰(zhàn)核心篇】基于JWT生成和校驗Token

    書接上文 技術選型篇,我們做了【用戶身份認證】的技術選型說明,對基于 Session、Token、JWT 的方案進行了詳細的對比分析,詳細說明了它們都是什么和各自的優(yōu)缺點!這些是實戰(zhàn)的基礎,還沒看過的同學,建議先看上文。最終采用的是目前流行的 基于JWT的Token用戶身份認證

    2023年04月08日
    瀏覽(27)
  • 3-1. SpringBoot項目集成【用戶身份認證】實戰(zhàn) 【技術選型篇】基于Session、Token、JWT怎么選?

    3-1. SpringBoot項目集成【用戶身份認證】實戰(zhàn) 【技術選型篇】基于Session、Token、JWT怎么選?

    通過第二章2-2. SpringBoot API開發(fā)詳解 --SpringMVC注解+封裝結(jié)果+支持跨域+打包,我們實現(xiàn)了基于SpringBoot項目的 API接口開發(fā) ,并實現(xiàn) API結(jié)果統(tǒng)一封裝、支持跨域請求 等等功能,接下來開始第三章,主要做用戶身份認證,主要實現(xiàn)一套 統(tǒng)一鑒權的用戶身份認證的機制 。 我已經(jīng)提

    2024年01月22日
    瀏覽(26)
  • 3-3. SpringBoot項目集成【用戶身份認證】實戰(zhàn) 【全流程篇】基于JWT+雙重檢查的登錄+登出+攔截器

    書接上文 實戰(zhàn)核心篇,我們已經(jīng) 把JWT的核心代碼實現(xiàn)了! 文中不止是代碼實現(xiàn),更是使用到了設計原則,提升大家的內(nèi)功心法。并且拋轉(zhuǎn)引玉的實現(xiàn)了RSA和HMAC兩種算法,還沒看過的同學,建議先看上文。所以對于 基于JWT的Token用戶身份認證機制 來說,剩下的就是與接口結(jié)

    2023年04月16日
    瀏覽(18)
  • SpringBoot 基于 OAuth2 統(tǒng)一身份認證流程詳解

    SpringBoot 基于 OAuth2 統(tǒng)一身份認證流程詳解

    了解OAUTH2統(tǒng)一認證基本概念 了解OAUTH2協(xié)議流程 了解OAUTH2各種模式類型 了解Spring Security OAuth設計 2. 分析 傳統(tǒng)登陸認證介紹 單點登陸認證介紹 OAuth2簡介 OAuth2角色 OAuth2協(xié)議流程介紹 OAuth2授權類型 OAuth2授權碼模式流程 OAuth2簡化模式 OAuth2密碼模式 OAuth2客戶端模式 Spring Security

    2024年02月15日
    瀏覽(28)
  • GitHub 開啟 2FA 雙重身份驗證的方法

    GitHub 開啟 2FA 雙重身份驗證的方法

    自2023年3月13日起,我們登錄 GitHub 都會看到一個要求 Enable 2FA 的重要提示,具體如下: GitHub users are now required to enable two-factor authentication as an additional security measure. Your activity on GitHub includes you in this requirement. You will need to enable two-factor authentication on your account before May 04, 2023,

    2024年02月15日
    瀏覽(19)
  • 部署開源項目 Casdoor 身份認證管理系統(tǒng)到本地

    部署開源項目 Casdoor 身份認證管理系統(tǒng)到本地

    Casdoor是一個基于OAuth 2.0、OIDC、SAML 和 CAS 的,UI-first的身份和訪問管理(IAM)/單點登錄(SSO)平臺。使用 Go 和react開發(fā),前后端分離,內(nèi)置第三方應用登錄服務。 Casdoor 有四個核心概念,分別是 組織(Organization) , 用戶(User) , 應用(Application) 和 提供商(Provider) 。組織承載用戶和應用

    2024年02月16日
    瀏覽(24)
  • 學習node之——如何在項目中使用MySQL、前后端的身份認證

    學習node之——如何在項目中使用MySQL、前后端的身份認證

    上一篇文章只寫了一丟丟,這篇才是正片,look look look 這里連接數(shù)據(jù)庫的用戶和密碼都是我們在安裝mysql時配置的密碼。每個人的users表格里面數(shù)據(jù)不同,結(jié)果也會不一樣喲! 巧2小黑板啦!這里有兩個知識點: 1、可以通過英文?符號占位符指定具體的值 2、用insert into插入數(shù)

    2024年02月10日
    瀏覽(20)
  • 在springBoot中使用JWT實現(xiàn)1.生成token,2.接收前端token進行身份認證,3.通過token獲取對象信息

    在springBoot中使用JWT實現(xiàn)1.生成token,2.接收前端token進行身份認證,3.通過token獲取對象信息

    第一步:引入依賴 第二步:創(chuàng)建工具類 在until包下創(chuàng)建TokenUntil類,用于生成token 利用id,和password作為參數(shù)生成token JWt為這個包下的對象 第三步:token使用 在向前端返回的數(shù)據(jù)對象中添加token屬性 ?是serve層中調(diào)用工具類方法將生成的token放到返回的數(shù)據(jù)中 注意:這里獲取到

    2024年02月04日
    瀏覽(93)
  • GitHub如何驗證2FA,煩人的認證,看完幾分鐘解鎖

    GitHub如何驗證2FA,煩人的認證,看完幾分鐘解鎖

    今天需要使用GitHub,還是不能用,需要2FA認證,沒辦法,還是讓2FA認證流程來,一一解決,在解決這認證問題之前,先說說2FA認證是什么? 2FA 是指兩步驗證(Two-Factor Authentication)的縮寫。它是一種用于增強賬戶安全性的身份驗證方法。傳統(tǒng)的身份驗證通常只需要輸入用戶名

    2024年04月23日
    瀏覽(17)
  • 統(tǒng)一身份認證,構(gòu)建數(shù)字時代的安全壁壘——統(tǒng)一身份認證介紹、原理和實現(xiàn)方法

    隨著數(shù)字化時代的來臨,個人和機構(gòu)在互聯(lián)網(wǎng)上的活動越來越頻繁,對于身份認證的需求也愈發(fā)迫切。為了有效應對身份欺詐、數(shù)據(jù)泄露等問題,統(tǒng)一身份認證(Unified Identity Authentication)應運而生。 在本文博主將介紹統(tǒng)一身份認證的概念、原理以及其具體的實現(xiàn)方案。 統(tǒng)一

    2024年02月03日
    瀏覽(29)

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

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

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

二維碼1

領取紅包

二維碼2

領紅包