短信验证码发送:账号注册 @Service @Transactional public class VerifyCodeServiceImpl implements IVerifyCodeService { @Value("${msg.url}") private String msgUrl; @Value("${msg.sign}") private String msgSign; @Value("${msg.appkey}") private String msgAppkey;
/*在service中创建一个方法将网见的url按格式写进去就好:返回值是0表示成功,其他的表示失败*/ private boolean realSendVerifyCode(String username,StringBuilder sbs){ try { //此处应该是url但是我用的spring boot 将数据配置在properties文件里面了; URL url = new URL(msgUrl); HttpURLConnection conn=(HttpURLConnection) url.openConnection(); //请求方式 conn.setRequestMethod("GET"); StringBuilder sbd = new StringBuilder(100); //拼接内容,原来用的某东云测试发现不行了,只能改电话号码改其他东西改了都发不出去 sbd.append("sign=")//网建给的 .append(msgSign)//短信头,有些格式规定了 .append("&mobile=")//网建给的 .append(username)//电话号码 .append("&content=")//这些值要看网建具体给你的是什么 .append(sbs)//短信内容 .append("&appkey=").append(msgAppkey);//密钥 conn.setDoInput(true); conn.getOutputStream().write(sb.toString().getBytes("UTF-8")); conn.connect(); String count = StreamUtils.copyToString(conn.getInputStream(), Charset.forName("UTF-8")); int cou = Integer.parseInt(count); //判断发送短信是否成功 if(cou>0){ return true; } }catch (Exception e){ e.printStackTrace(); } //短信发送失败 return false; } }
另外添加spring boot的配置文件
msg.properties:内容

引入配置文件,在你的配置类里面添加
短信验证码发送实现
3656

被折叠的 条评论
为什么被折叠?



