阿里云短信接口调用
1、打开阿里云,登录
2、登录之后进入账号管理界面,搜索 短信服务
点击新手引导
申请好短信签名和模板,一般需要一小时
打开AccessKey管理
记下Accesskeyid 和accessSecret
3、需要的maven包 阿里云及Redis
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.6</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>1.1.0</version>
</dependency>
<!--redis服务-->
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>2.9.0</version>
</dependency>
4、短信工具类
public class MessageUtils { /** * 发送短信消息方法,返回验证码 * @param phone 用户手机号 * @return true 发送成功 ;false 发送失败 */ public void sendMsg(String phone) throws Exception { //设置超时时间-可自行调整 System.setProperty("sun.net.client.defaultConnectTimeout", "10000"); System.setProperty("sun.net.client.defaultReadTimeout", "10000"); //初始化ascClient需要的几个参数 final String product = "Dysmsapi";//短信API产品名称(短信产品名固定,无需修改) final String domain = "dysmsapi.aliyuncs.com";//短信API产品域名(接口地址固定,无需修改) //替换成你的AK final String accessKeyId = "";//你的accessKeyId,参考本文档步骤2 final String accessKeySecret = "";//你的accessKeySecret,参考本文档步骤2 //初始化ascClient,暂时不支持多region(请勿修改) IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId, accessKeySecret); DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", product, domain); IAcsClient acsClient = new DefaultAcsClient(profile); //组装请求对象 SendSmsRequest request = new SendSmsRequest(); //使用post提交 request.setMethod(MethodType.POST); //必填:待发送手机号。支持以逗号分隔的形式进行批量调用,批量上限为1000个手机号码,批量调用相对于单条调用及时性稍有延迟,验证码类型的短信推荐使用单条调用的方式 request.setPhoneNumbers(phone); //必填:短信签名-可在短信控制台中找到 request.setSignName(""); //必填:短信模板-可在短信控制台中找到 request.setTemplateCode(""); String checkCode = CoreUtils.randomString(6, true);//此处是生成6位数验证码工具类 //request.setTemplateParam("{\"code\":\"123\"}");//测试用,此处json一定要严格按照json格式书写 request.setTemplateParam("{\"code\":\"" + checkCode + "\"}"); //可选-上行短信扩展码(扩展码字段控制在7位或以下,无特殊需求用户请忽略此字段) //request.setSmsUpExtendCode("90997"); //可选:outId为提供给业务方扩展字段,最终在短信回执消息中将此值带回给调用者 //request.setOutId("yourOutId"); //请求失败这里会抛ClientException异常 SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request); if (sendSmsResponse.getCode() != null && sendSmsResponse.getCode().equals("OK")) { //请求成功,短信已发送 //将手机号和生成的随机数存入redis 并设置redis的过期时间 RedisClientServer.set(phone, phone+checkCode, 180); } } }
5、生成随机字符串工具类
public class CoreUtils { /** * 生成随机字符串 * * @param length * @return */ public static String randomString(int length, boolean isNumeric) { String base = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"; if (isNumeric) { base = "0123456789"; } Random random = new Random(); StringBuffer buffer = new StringBuffer(length); for (int i = 0; i < length; i++) { buffer.append(base.charAt(random.nextInt(base.length()))); } return buffer.toString(); } }
6、Redis工具类
1 public class RedisClientServer { 2 3 4 private static JedisPool jedisPool = null; 5 6 private static Jedis jedis = null; 7 8 public static Jedis jedis_object = null; 9 10 private static String host = "127.0.0.1"; 11 private static String password = "root"; 12 private static Integer port = 6379; 13 14 15 static{ 16 if(jedisPool == null){ 17 JedisPoolConfig config = new JedisPoolConfig(); 18 //设置最大连接数 19 config.setMaxTotal(500); 20 //设置最大空闲数 21 config.setMaxIdle(20); 22 //设置最小空闲数 23 config.setMinIdle(8); 24 //设置超时时间 25 config.setMaxWaitMillis(3000); 26 //Idle时进行连接扫描 27 config.setTestWhileIdle(true); 28 //表示idle object evitor两次扫描之间要sleep的毫秒数 29 config.setTimeBetweenEvictionRunsMillis(30000); 30 //表示idle object evitor每次扫描的最多的对象数 31 config.setNumTestsPerEvictionRun(10); 32 //表示一个对象至少停留在idle状态的最短时间,然后才能被idle object evitor 33 //扫描并驱逐;这一项只有在timeBetweenEvictionRunsMillis大于0时才有意义 34 config.setMinEvictableIdleTimeMillis(60000); 35 //初始化连接池 36 jedisPool = new JedisPool(config, host, port); 37 jedis_object = new Jedis( host, port); 38 } 39 } 40 41 private RedisClientServer() { 42 43 } 44 45 private static Jedis getJedisInstance(){ 46 47 try { 48 if(null == jedis){ 49 jedis = jedisPool.getResource(); 50 jedis.auth(password); 51 } 52 } catch (Exception e) { 53 } 54 return jedis; 55 } 56 57 /** 58 * 向缓存中设置字符串内容 59 *@author liudianpeng.com 60 *@date 61 */ 62 public static boolean set(String key, String value) throws Exception { 63 Jedis jedis = null; 64 try { 65 jedis = getJedisInstance(); 66 jedis.set(key, value); 67 return true; 68 } catch (Exception e) { 69 } finally { 70 jedisPool.close(); 71 } 72 return false; 73 } 74 75 /** 76 * 向缓存中设置字符串内容 ,设置过期时间 77 *@author liudianpeng.com 78 *@date 79 */ 80 public static boolean set(String key, String value,Integer seconds) throws Exception { 81 Jedis jedis = null; 82 try { 83 jedis = getJedisInstance(); 84 jedis.set(key, value); 85 jedis.expire(key, seconds); 86 return true; 87 } catch (Exception e) { 88 } finally { 89 jedisPool.close(); 90 } 91 return false; 92 } 93 94 /** 95 * 根据key 获取内容 96 *@author liudianpeng.com 97 *@date 98 */ 99 public static Object get(String key) { 100 Jedis jedis = null; 101 try { 102 jedis = getJedisInstance(); 103 Object value = jedis.get(key); 104 return value; 105 } catch (Exception e) { 106 } finally { 107 jedisPool.close(); 108 } 109 return null; 110 } 111 112 /** 113 * 删除缓存中得对象,根据key 114 *@author liudianpeng.com 115 *@date 116 */ 117 public static boolean del(String key) { 118 Jedis jedis = null; 119 try { 120 jedis = getJedisInstance(); 121 jedis.del(key); 122 return true; 123 } catch (Exception e) { 124 e.printStackTrace(); 125 } finally { 126 jedisPool.close(); 127 } 128 return false; 129 } 130 131 132 public static <T> T parseObject(String text, Class<T> clazz) { 133 return parseObject(text, clazz); 134 } 135 136 /** 137 * 根据key 获取对象 138 *@author liudianpeng.com 139 *@date 140 */ 141 public static <T> T get(String key, Class<T> clazz) { 142 Jedis jedis = null; 143 try { 144 jedis = getJedisInstance(); 145 String value = jedis.get(key); 146 return parseObject(value, clazz); 147 } catch (Exception e) { 148 e.printStackTrace(); 149 } finally { 150 jedisPool.close(); 151 } 152 return null; 153 } 154 155 /** 156 * 设置key过期 157 *@author 158 *@date 159 */ 160 public static boolean expire(String key,int seconds){ 161 Jedis jedis = null; 162 try { 163 jedis = getJedisInstance(); 164 jedis.expire(key, seconds); 165 return true; 166 } catch (Exception e) { 167 e.printStackTrace(); 168 } finally { 169 jedisPool.close(); 170 } 171 return false; 172 } 173 174 /** 175 * 判断是否存在key 176 *@author 177 *@date 178 */ 179 public static Boolean exists(String key){ 180 Jedis jedis = null; 181 try { 182 jedis = getJedisInstance(); 183 return jedis.exists(key); 184 } catch (Exception e) { 185 e.printStackTrace(); 186 return false; 187 }finally { 188 jedisPool.close(); 189 } 190 } 191 192 }
7、