JA Plus 开发者文档 JA Plus 开发者文档
首页
📖 白皮书 (opens new window)
  • 名词解释
  • 快速开始

    • 使用jap-simple
    • 使用jap-social
    • 使用jap-oauth2
    • 使用jap-oidc
    • 使用jap-sso
    • 使用jap-mfa
    • 使用jap-http-api
    • 使用jap-ldap
    • 错误代码
  • IDS

    • 简介
    • 快速开始
    • 自定义scope
    • 自定义登录页面
    • 自定义确认授权页面
    • 自定义缓存
    • 自定义Token加密密钥
    • 使用PKCE模式
    • 自动授权
    • 错误代码
  • starter

    • jap-spring-boot-starter
    • jap-simple-spring-boot-starter
    • jap-social-spring-boot-starter
    • jap-oauth2-spring-boot-starter
    • jap-oidc-spring-boot-starter
  • jap-ids的demo
  • 前后端分离架构中使用JAP
  • SpringBoot中使用JAP
  • 问题反馈
  • 项目问题
  • 异常问题
  • 功能问题
  • 数据看板🔥
  • 贡献指南
  • 行为准则
  • 用户权益
  • 贡献者们
  • 社区配套 (opens new window)
  • 教程
  • 投稿
  • 资讯
  • 关于
  • 友情链接
  • 捐赠列表
  • 其他开源
  • 更新记录
收藏
GitHub (opens new window)

FuJie Team

以开源的形式赋能开发者. Just auth into any app.
首页
📖 白皮书 (opens new window)
  • 名词解释
  • 快速开始

    • 使用jap-simple
    • 使用jap-social
    • 使用jap-oauth2
    • 使用jap-oidc
    • 使用jap-sso
    • 使用jap-mfa
    • 使用jap-http-api
    • 使用jap-ldap
    • 错误代码
  • IDS

    • 简介
    • 快速开始
    • 自定义scope
    • 自定义登录页面
    • 自定义确认授权页面
    • 自定义缓存
    • 自定义Token加密密钥
    • 使用PKCE模式
    • 自动授权
    • 错误代码
  • starter

    • jap-spring-boot-starter
    • jap-simple-spring-boot-starter
    • jap-social-spring-boot-starter
    • jap-oauth2-spring-boot-starter
    • jap-oidc-spring-boot-starter
  • jap-ids的demo
  • 前后端分离架构中使用JAP
  • SpringBoot中使用JAP
  • 问题反馈
  • 项目问题
  • 异常问题
  • 功能问题
  • 数据看板🔥
  • 贡献指南
  • 行为准则
  • 用户权益
  • 贡献者们
  • 社区配套 (opens new window)
  • 教程
  • 投稿
  • 资讯
  • 关于
  • 友情链接
  • 捐赠列表
  • 其他开源
  • 更新记录
收藏
GitHub (opens new window)
  • 使用指南
  • 名词解释
  • 快速开始

    • 使用jap-simple
    • 使用jap-social
    • 使用jap-oauth2
    • 使用jap-oidc
    • 使用jap-sso
    • 使用jap-mfa
      • 1. 引入依赖
      • 2. 实现 JapMfaService 接口
      • 3. 初始化 JapMfa
      • 4. 生成 TOTP 绑定二维码
      • 5. 扫码绑定 TOTP
      • 6. 校验 TOTP 动态码
      • 官方推荐
    • 使用jap-http-api
    • 使用jap-ldap
    • JAP 错误代码
  • IDS

  • starter

  • 指南
  • 快速开始
FuJie Team
2021-09-20

使用jap-mfa

提示

jap-mfa 是为了方便快速的实现多因素认证中的 TOTP(Time based one-time password) 认证。

# 1. 引入依赖


<dependency>
  <groupId>com.fujieid</groupId>
  <artifactId>jap-mfa</artifactId>
  <version>{latest}</version>
</dependency>
1
2
3
4
5
6

# 2. 实现 JapMfaService 接口

public static class JapMfaServiceImpl implements JapMfaService {

  /**
   * 根据帐号查询 secretKey
   *
   * @param userName 申请 secretKey 的用户
   * @return secretKey
   */
  @Override
  public String getSecretKey(String userName) {
    // do something 
    return secretKey;
  }

  /**
   * 将 secretKey 关联 userName 后进行保存,可以存入数据库也可以存入其他缓存媒介中
   *
   * @param userName       用户名
   * @param secretKey      申请到的 secretKey
   * @param validationCode 当前计算出的 TOTP 验证码
   * @param scratchCodes   scratch 码
   */
  @Override
  public void saveUserCredentials(String userName, String secretKey, int validationCode, List<Integer> scratchCodes) {
    // do something 
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

# 3. 初始化 JapMfa

JapMfa japMfa = new JapMfa(new JapMfaServiceImpl());
1

# 4. 生成 TOTP 绑定二维码

jap-mfa 提供了四种创建二维码的方式:

  1. 生成 File - File otpQrCodeFile = japMfa.getOtpQrcodeFile(username, issuer);
  2. 生成图片的 base64 字符串(可直接通过 img 标签的 src 属性显示) - String otpQrCodeBase64 = japMfa.getOtpQrcodeFileBase64(username, issuer, true);
  3. 生成可直接访问的图片 URL - String otpQrCodeUrl = japMfa.getOtpQrCodeUrl(username, issuer);
  4. 直接通过 HttpServletResponse 返回图片 - japMfa.createOtpQrcode(username, issuer, HttpServletResponse);

其中,username 为当前需要绑定的用户名,issuer 一般为服务提供者,比如 Baidu、baidu.com、JAP 等等

# 5. 扫码绑定 TOTP

手机下载安装 MFA 工具,比如:Google authenticator、TOTP Authenticator等等,打开安装好的 APP,选择扫描二维码(或者 Scan QR Code)。然后扫描第 4 步生成的二维码文件即可完成绑定。

# 6. 校验 TOTP 动态码

jap-mfa 提供了两种校验 TOTP 动态码的方式:

  1. 通过用户名校验 - boolean verifyByUsernameResult = japMfa.verifyByUsername(username, consoleInput)
  2. 通过 secretKey 校验 - boolean verifyBySecretResult = japMfa.verifyBySecret(secretKey, consoleInput);

校验通过返回 true。

# 官方推荐

  • 普通示例项目:jap-demo (opens new window)
  • 前后端分离项目示例:jap-demo-vue (opens new window)
编辑 (opens new window)
#jap-mfa#MFA#多因素#OTP#TOTP
Last Updated: 2021/10/07, 18:03:43
使用jap-sso
使用jap-http-api

← 使用jap-sso 使用jap-http-api→

最近更新
01
经验总结:关于为 JAP 开发不同语言的 Demo 的总结
11-02
02
jap-spring-boot-starter 使用帮助
10-28
03
使用jap-ldap
10-25
更多文章>
Theme by Vdoing | Copyright © 2021-2022

友情链接:UniAdmin | 江如意的博客

Copyright © 2021-2040 FUJIE. All rights reserved. 北京符节科技有限公司版权所有 | 京ICP备2020044519号-4
  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式