【短信防火墙】后端接入---JAVA接入指引
发布时间:2021-01-27 点击数:18716
后端接入---JAVA接入指引
本篇教程介绍如何在Java后端接入防火墙。
快速接入步骤
1、 配置必要依赖包
nxt-sms-client-1.2.jar
commons-codec-1.4.jar
commons-lang3-3.6.jar
commons-logging.jar
fastjson-1.2.31.jar
logback-core-1.0.13.jar
slf4j-api-1.6.6.jar
logback-classic-1.0.13.jar
2、 配置相关配置文件
newxt.ini
修改参数(fireWareUrl)-->http://localhost:7502 (本地服务路径或云端服务地址:http://fw.newxtc.com)
(isNginx)-->true (有无反向代理)
3、 各场景示例代码(注:短信下发和短信验证须成对出现)
1. 短信下发
public RetMsg smsSend(HttpServletRequest request, HttpServletResponse response, String clientMobile) {
RetMsg retMsg = new RetMsg(-1, "系统异常");
FwClient fwClient = new FwClientImpl();
try {
// 1 调用【短信防火墙】短信发送请求
HashMap < String, Object > paramMap = fwClient.getSendReq(request, clientMobile);
String jsonReq = fwClient.execReq(paramMap);
String smsSendRet = fwClient.getRetVaule(jsonReq, "riskResult");
if("REJECT".equals(smsSendRet)) {
retMsg.setRet(3);
retMsg.setMsg("请求过于频繁");
}
else {
// 业务 TODO
// 业务调用短信接口 TODO
// 调用短信接口 结束
if(smsRetMsg != null && smsRetMsg.getRet() == 0) {
// 2 调用【短信防火墙】成功结果
fwClient.execSucc(paramMap);
logger.debug("send succ");
retMsg.setRet(0);
retMsg.setMsg("发送验证码成功");
}
else {
// 2 调用【短信防火墙】失败结果
SmsVerifyCache.getInstance().remove(clientMobile);
fwClient.execFail(paramMap);
retMsg.setRet(-1);
retMsg.setMsg("发送验证码失败");
}
}
}
catch(Exception e) {
for(StackTraceElement elment: e.getStackTrace()) {
logger.error(elment.toString());
}
}
return retMsg;
}
2. 短信验证
public RetMsg smsVerify(HttpServletRequest request, HttpServletResponse response, String clientMobile, String smsVerifyCode) {
FwClient fwClient = new FwClientImpl();
RetMsg retMsg = new RetMsg(-1, "系统异常");
if(smsVerifyCode == null || smsVerifyCode.isEmpty()) {
retMsg.setRet(1);
retMsg.setMsg("输入验证码为空");
}
else {
// 1 调用【短信防火墙】验证请求
HashMap < String, Object > paramMap = fwClient.getVerifyReq(request, clientMobile); // 请求防火墙
String jsonReq = fwClient.execReq(paramMap);
// 报文处理
String smsSendRet = fwClient.getRetVaule(jsonReq, "riskResult");
if("REJECT".equals(smsSendRet)) {
retMsg.setRet(3);
retMsg.setMsg("请求过于频繁");
}
// 业务 TODO
if(cacheSmsVerify != null && cacheSmsVerify.getVerifyCode() != null && !cacheSmsVerify.getVerifyCode().isEmpty()) {
if(cacheSmsVerify.getVerifyCode().equals(smsVerifyCode)) {
retMsg.setRet(0);
retMsg.setMsg("验证成功");
}
else {
retMsg.setRet(1);
retMsg.setMsg("验证码错误");
}
}
else {
retMsg.setRet(-9);
retMsg.setMsg("验证码超时");
}
if(retMsg.getRet() == 0) {
// 2 调用【短信防火墙】成功结果
fwClient.execSucc(paramMap);
}
else {
// 2 调用【短信防火墙】失败结果
fwClient.execFail(paramMap);
}
}
return retMsg;
}
3. 用户登录
public RetMsg loginByPwd(HttpServletRequest request, HttpServletResponse response, String userId, String password) {
FwClient fwClient = new FwClientImpl();
RetMsg retMsg = new RetMsg(-1, "登录失败");
if(userId == null || userId.isEmpty()) {
retMsg.setRet(1);
retMsg.setMsg("请输入手机号码");
}
else if(password == null || password.isEmpty()) {
retMsg.setRet(1);
retMsg.setMsg("请输入密码");
}
else {
// 1 调用【短信防火墙】验证请求
HashMap < String, Object > paramMap = fwClient.getLoginByMobile(request, userId);
String jsonReq = fwClient.execReq(paramMap);
// 报文处理
String smsSendRet = fwClient.getRetVaule(jsonReq, "riskResult");
if("REJECT".equals(smsSendRet)) {
retMsg.setRet(3);
retMsg.setMsg("请求过于频繁");
}
else {
// 2 调用【短信防火墙】成功结果
fwClient.execSucc(paramMap);
// 业务 TODO
if(userInfo == null) {
retMsg.setRet(1);
retMsg.setMsg("用户名或密码错误");
// 2 调用【短信防火墙】失败结果
fwClient.loginFail(paramMap);
}
else {
retMsg.setRet(0);
retMsg.setMsg("登录成功");
// 2 调用【短信防火墙】成功结果
fwClient.loginSucc(paramMap);
}
}
}
return retMsg;
}