页面树结构
转至元数据结尾
转至元数据起始

1、背景

若要自定义手机和邮件验证码的发送、校验逻辑,对接特定的验证码平台,可以定制验证码的生成、存储、发送、校验等逻辑。

2、接口说明

2.1 gradle依赖

"smartbi:Smartbi-UserManager.Interface:${project.ext.SMARTBI_VERSION}",    

2.2 接口定义说明

// smartbi.usermanager.model.CaptchaCodeSendReq
/**
 * 发送验证码请求实体
 */
public class CaptchaCodeSendReq {
	/** 发送验证码的类型,例如登录验证时发送的验证码,修改密码时发送的验证码 */
	CaptchaSendType sendType;
	/** 用户名 */
    String userName;
	/** 手机或邮箱 */
	String phoneOrEmail;
}

// smartbi.usermanager.model.CaptchaCodeValidReq
/**
 * 校验验证码请求实体
 */
public class CaptchaCodeValidReq {
	/** 发送验证码的类型,例如登录验证时发送的验证码,修改密码时发送的验证码 */
	CaptchaSendType sendType;
	/** 用户名 */
	String userName;
	/** 手机或邮箱*/
	String phoneOrEmail;
	/** 验证码 */
	String captchaCode;
}

// smartbi.usermanager.model.CaptchaCodeValidResp
/**
 * 校验验证码结果实体
 */
public class CaptchaCodeValidResp {
	/** 验证码验证结果 */
	boolean success;
	/** 验证码验证失败时的报错信息 CAPTCHACODE_INVALID, CAPTCHACODE_ERROR, CAPTCHACODE_EXPIRED */
	UserManagerErrorCode errCode;
}

// smartbi.usermanager.service.ICaptchaCodeService
/**
 * 验证码发送和校验类
 */

public interface ICaptchaCodeService {
  /**
  * 发送验证码
  * @param req 
  * @return 发送是否成功
  */
  boolean sendCaptchaCode(CaptchaCodeSendReq req);

  /**
  * 校验验证码
  * @param req 
  * @return 验证结果
  */
  CaptchaCodeValidResp validateCaptchaCode(CaptchaCodeValidReq req);
}

3、参考例子

// 自定义实现类,需要实现验证码的生成、存储、发送、校验等逻辑
public class CustomSmsCaptchaCodeService implements ICaptchaCodeService {
  public boolean sendCaptchaCode(CaptchaCodeSendReq req) {
    // 具体实现参考产品内 SmartbiSmsCaptchaCodeService.sendCaptchaCode 方法的实现
  }
  public CaptchaCodeValidResp validateCaptchaCode(CaptchaCodeValidReq req) {
    // 具体实现参考产品内 SmartbiSmsCaptchaCodeService.validateCaptchaCode 方法的实现
  }
}

public class CustomEmailCaptchaCodeService implements AbstractCaptchaCodeService {
  public boolean sendCaptchaCode(CaptchaCodeSendReq req) {
    // 具体实现参考产品内 SmartbiEmailCaptchaCodeService.sendCaptchaCode 方法的实现
  }
  public CaptchaCodeValidResp validateCaptchaCode(CaptchaCodeValidReq req) {
    // 具体实现参考产品内 SmartbiEmailCaptchaCodeService.validateCaptchaCode 方法的实现
  }
}

// 设置自定义实现
UserManagerModule.getInstance().setSmsCaptchaCodeService(new CustomSmsCaptchaCodeService());
UserManagerModule.getInstance().setEmailCaptchaCodeService(new CustomEmailCaptchaCodeService());

也可以继承 smartbi.usermanager.service.AbstractCaptchaCodeService 类,该类提供了一些公共方法,例如生成验证码的方法 generateCaptchaCode。

注意:验证码应当限制发送频率、校验次数,避免恶意请求频繁发送验证码造成经济损失、频繁校验验证码导致被枚举。



  • 无标签