短信验证码--+验证码存在redis中--直接可用的后台代码

1.小程序存值在redis里面,不建议用cook存sessionid,因为同一个会话每次请求接口值会变,存不住,

 1.1工具包(短信工具包---记住:发给谁(手机号),发什么(发送内容),发送路径(url))---工具包直接粘了用

public static void sendSms(final String url, final String smsContent) throws Exception {
if (null == url || 0 == url.length())
throw new IllegalArgumentException("短信接口連接地址不能為空");
if (null == smsContent || 0 == smsContent.length())
throw new IllegalArgumentException("短信發送內容不能為空");
try {
URL connection = new URL(url);
HttpURLConnection conn = (HttpURLConnection) connection.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty(
"User-Agent",
"Mozilla/5.0 (Windows; U; Windows NT 6.1; zh-CN; rv:1.9.2.10) Gecko/20100914 Firefox/3.6.10 ");
conn.setRequestProperty("Content-type", "text/xml;charset=UTF-8");
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(
conn.getOutputStream()));
bw.write(smsContent);
bw.close();
BufferedReader br = new BufferedReader(new InputStreamReader(
conn.getInputStream()));
String out = null;
while ((out = br.readLine()) != null) {
System.out.println(out);
}
br.close();
} catch (Exception e) {
e.printStackTrace();
throw e;
}
}

 

 

 

1.2 ctroller接口代码

/*
* 短信验证码(给短信平台手机号和自主生成的随机数,即验证码)
* */
@RequestMapping(value = "/identifyingCode")
public CIPResponseMsg identifyingCode(HttpServletRequest request,HttpServletResponse response)throws Exception {
CIPResponseMsg msg = new CIPResponseMsg();

String per_phone = request.getParameter("per_phone");   //用get请求
boolean isJedisConnectExeptionOccured = false;      //redis申明
try {
String rd_code = WCM_utils_SequenceUtils.rd_code();
String dx_id = WCM_utils_SequenceUtils.dx_id();
//String url = "http://10.**.***.***:****/smsinterface/sendInterface/sendSms_xml.do";   //本地短信接口的ip
String url = "http://10.**.***.***:****/smsinterface/sendInterface/sendSms_xml.do";    //uat测试短信接口ip  --正式上线的时候
String code = "" +
"" +
"1.0" +
"dkh_app" +
"lV4g6pzrrOvZ3MvOqPbF" +
"
" +
"" +
"" +
""+dx_id+"" +
""+随机数+"" +
""+发送给谁的手机号+"" +
"
" +
"
" +
"
";
SmsUtil.sendSms(url, code);
System.out.println(code);
//短信平台获取短信验证码
String dx_cd = rd_code;
if(dx_cd==null&dx_cd.equals("")){
msg.data=dx_cd;
msg.errorCode = CIPErrorCode.CALL_SUCCESS.code;
msg.msg = "验证码获取失败";
}
msg.data=dx_cd;
jedisSet = CIPRedisUtils.getJedisResource();    //new jedisSet,用这个存,(死的)
jedisSet.setex("dx_cd", 900,dx_cd);    //存---用(key,900存储时间,value)
System.out.println(jedisSet.get("dx_cd"));   //控制台打印一下
log.info("dx_cd="+dx_cd);  //linux打印日志
msg.data = jedisSet.get("dx_cd");
msg.errorCode = CIPErrorCode.CALL_SUCCESS.code;
msg.msg = "操作成功";
/*} catch (Exception e) {
e.printStackTrace();
}*/
} catch (Exception e) {
isJedisConnectExeptionOccured = true;    
}finally {
if(null != jedisSet) {
CIPRedisUtils.returnJedisResource(jedisSet, isJedisConnectExeptionOccured);                 //重点:重点:重点:用完redis一定要释放资源
}
}
return msg;
}

 

 

 

//在另一个类里面去存在redis

转载于:https://www.cnblogs.com/Darkqueen/p/9848471.html

你可能感兴趣的:(短信验证码--+验证码存在redis中--直接可用的后台代码)