Skip to content

Commit d0e1942

Browse files
committed
sms init
1 parent d44695c commit d0e1942

File tree

13 files changed

+469
-4
lines changed

13 files changed

+469
-4
lines changed
Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
#actuator端口
21
management:
32
server:
43
port: 9001
54
endpoints:
65
web:
7-
#修改访问路径,2.0默认是 /actuator
86
base-path: /actuator
97
exposure:
10-
#开放所有页面节点 默认只开启了health、info两个节点
118
include: "*"
129
endpoint:
1310
health:
14-
#显示健康具体信息 默认不会显示详细信息
1511
show-details: always

springboot-sms/.gitignore

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
HELP.md
2+
/target/
3+
!.mvn/wrapper/maven-wrapper.jar
4+
5+
### STS ###
6+
.apt_generated
7+
.classpath
8+
.factorypath
9+
.project
10+
.settings
11+
.springBeans
12+
.sts4-cache
13+
14+
### IntelliJ IDEA ###
15+
.idea
16+
*.iws
17+
*.iml
18+
*.ipr
19+
20+
### NetBeans ###
21+
/nbproject/private/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/build/
27+
28+
### VS Code ###
29+
.vscode/

springboot-sms/pom.xml

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>org.springframework.boot</groupId>
7+
<artifactId>spring-boot-starter-parent</artifactId>
8+
<version>2.1.3.RELEASE</version>
9+
<relativePath/> <!-- lookup parent from repository -->
10+
</parent>
11+
<groupId>cn.tellsea</groupId>
12+
<artifactId>springboot-sms</artifactId>
13+
<version>0.0.1-SNAPSHOT</version>
14+
<name>springboot-sms</name>
15+
<description>Demo project for Spring Boot</description>
16+
17+
<properties>
18+
<java.version>1.8</java.version>
19+
</properties>
20+
21+
<dependencies>
22+
<dependency>
23+
<groupId>org.springframework.boot</groupId>
24+
<artifactId>spring-boot-starter-web</artifactId>
25+
</dependency>
26+
27+
<dependency>
28+
<groupId>org.projectlombok</groupId>
29+
<artifactId>lombok</artifactId>
30+
<optional>true</optional>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-test</artifactId>
35+
<scope>test</scope>
36+
</dependency>
37+
38+
<!-- sms -->
39+
<dependency>
40+
<groupId>com.aliyun</groupId>
41+
<artifactId>aliyun-java-sdk-core</artifactId>
42+
<version>4.0.6</version>
43+
</dependency>
44+
<dependency>
45+
<groupId>com.aliyun</groupId>
46+
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
47+
<version>1.1.0</version>
48+
</dependency>
49+
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-data-redis</artifactId>
53+
</dependency>
54+
55+
<dependency>
56+
<groupId>org.apache.commons</groupId>
57+
<artifactId>commons-lang3</artifactId>
58+
</dependency>
59+
</dependencies>
60+
61+
<build>
62+
<plugins>
63+
<plugin>
64+
<groupId>org.springframework.boot</groupId>
65+
<artifactId>spring-boot-maven-plugin</artifactId>
66+
</plugin>
67+
</plugins>
68+
</build>
69+
70+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package cn.tellsea;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringbootSmsApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringbootSmsApplication.class, args);
11+
}
12+
13+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.tellsea.config;
2+
3+
import lombok.Data;
4+
import org.springframework.boot.context.properties.ConfigurationProperties;
5+
6+
@Data
7+
@ConfigurationProperties(prefix = "tellsea.sms")
8+
public class SmsProperties {
9+
10+
String accessKeyId;
11+
12+
String accessKeySecret;
13+
14+
String signName;
15+
16+
String verifyCodeTemplete;
17+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package cn.tellsea.service;
2+
3+
public interface SmsService {
4+
5+
void sendCode(String phone);
6+
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package cn.tellsea.service.impl;
2+
3+
import cn.tellsea.config.SmsProperties;
4+
import cn.tellsea.service.SmsService;
5+
import cn.tellsea.utils.JsonUtils;
6+
import cn.tellsea.utils.NumberUtils;
7+
import cn.tellsea.utils.SmsUtils;
8+
import org.springframework.beans.factory.annotation.Autowired;
9+
import org.springframework.boot.context.properties.EnableConfigurationProperties;
10+
import org.springframework.data.redis.core.StringRedisTemplate;
11+
import org.springframework.stereotype.Service;
12+
13+
import java.util.HashMap;
14+
import java.util.Map;
15+
import java.util.concurrent.TimeUnit;
16+
17+
@Service
18+
@EnableConfigurationProperties(SmsProperties.class)
19+
public class SmsServiceImpl implements SmsService {
20+
21+
@Autowired
22+
private SmsProperties smsProperties;
23+
24+
@Autowired
25+
private SmsUtils smsUtils;
26+
27+
@Autowired
28+
private StringRedisTemplate stringRedisTemplate;
29+
30+
private static final String KEY_PREFIX = "user:verify:phone:";
31+
32+
public void sendCode(String phone) {
33+
// 生成redis的key
34+
String key = KEY_PREFIX + phone;
35+
String code = NumberUtils.generateCode(6);
36+
Map<Object, Object> msg = new HashMap<>();
37+
msg.put("phone", phone);
38+
msg.put("code", code);
39+
// 异步发送验证码
40+
smsUtils.sendSms(phone, smsProperties.getSignName(), smsProperties.getVerifyCodeTemplete(), JsonUtils.toString(msg));
41+
// 保存验证码
42+
stringRedisTemplate.opsForValue().set(key, code, 5, TimeUnit.MINUTES);
43+
}
44+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
package cn.tellsea.utils;
2+
3+
import com.fasterxml.jackson.core.JsonProcessingException;
4+
import com.fasterxml.jackson.core.type.TypeReference;
5+
import com.fasterxml.jackson.databind.ObjectMapper;
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
import java.io.IOException;
10+
import java.util.List;
11+
import java.util.Map;
12+
13+
public class JsonUtils {
14+
15+
public static final ObjectMapper mapper = new ObjectMapper();
16+
17+
private static final Logger logger = LoggerFactory.getLogger(JsonUtils.class);
18+
19+
public static String toString(Object obj) {
20+
if (obj == null) {
21+
return null;
22+
}
23+
if (obj.getClass() == String.class) {
24+
return (String) obj;
25+
}
26+
try {
27+
return mapper.writeValueAsString(obj);
28+
} catch (JsonProcessingException e) {
29+
logger.error("json序列化出错toString:" + obj, e);
30+
return null;
31+
}
32+
}
33+
34+
public static <T> T toBean(String json, Class<T> tClass) {
35+
try {
36+
return mapper.readValue(json, tClass);
37+
} catch (IOException e) {
38+
logger.error("json解析出错toBean:" + json, e);
39+
return null;
40+
}
41+
}
42+
43+
public static <E> List<E> toList(String json, Class<E> eClass) {
44+
try {
45+
return mapper.readValue(json, mapper.getTypeFactory().constructCollectionType(List.class, eClass));
46+
} catch (IOException e) {
47+
logger.error("json解析出错toList:" + json, e);
48+
return null;
49+
}
50+
}
51+
52+
public static <K, V> Map<K, V> toMap(String json, Class<K> kClass, Class<V> vClass) {
53+
try {
54+
return mapper.readValue(json, mapper.getTypeFactory().constructMapType(Map.class, kClass, vClass));
55+
} catch (IOException e) {
56+
logger.error("json解析出错toMap:" + json, e);
57+
return null;
58+
}
59+
}
60+
61+
public static <T> T nativeRead(String json, TypeReference<T> type) {
62+
try {
63+
return mapper.readValue(json, type);
64+
} catch (IOException e) {
65+
logger.error("json解析出错nativeRead:" + json, e);
66+
return null;
67+
}
68+
}
69+
}
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
package cn.tellsea.utils;
2+
3+
import java.math.BigDecimal;
4+
import java.math.RoundingMode;
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
import java.util.Random;
8+
import java.util.regex.MatchResult;
9+
import java.util.regex.Matcher;
10+
import java.util.regex.Pattern;
11+
12+
public class NumberUtils {
13+
14+
public static boolean isInt(Double num) {
15+
return num.intValue() == num;
16+
}
17+
18+
/**
19+
* 判断字符串是否是数值格式
20+
* @param str
21+
* @return
22+
*/
23+
public static boolean isDigit(String str){
24+
if(str == null || str.trim().equals("")){
25+
return false;
26+
}
27+
return str.matches("^\\d+\\.?\\d+$");
28+
}
29+
30+
public static double toDouble(String s){
31+
if(s == null){
32+
return 0;
33+
}
34+
if(!isDigit(s)){
35+
return 0;
36+
}
37+
return Double.valueOf(s);
38+
}
39+
40+
/**
41+
* 将一个小数精确到指定位数
42+
* @param num
43+
* @param scale
44+
* @return
45+
*/
46+
public static double scale(double num, int scale) {
47+
BigDecimal bd = new BigDecimal(num);
48+
return bd.setScale(scale, RoundingMode.HALF_UP).doubleValue();
49+
}
50+
51+
// 从字符串中根据正则表达式寻找,返回找到的数字数组
52+
public static Double[] searchNumber(String value, String regex){
53+
List<Double> doubles = new ArrayList<>();
54+
Pattern pattern = Pattern.compile(regex);
55+
Matcher matcher = pattern.matcher(value);
56+
if(matcher.find()) {
57+
MatchResult result = matcher.toMatchResult();
58+
for (int i = 1; i <= result.groupCount(); i++) {
59+
doubles.add(Double.valueOf(result.group(i)));
60+
}
61+
}
62+
return doubles.toArray(new Double[doubles.size()]);
63+
}
64+
65+
/**
66+
* 生成指定位数的随机数字
67+
* @param len
68+
* @return
69+
*/
70+
public static String generateCode(int len){
71+
len = Math.min(len, 8);
72+
int min = Double.valueOf(Math.pow(10, len - 1)).intValue();
73+
int num = new Random().nextInt(Double.valueOf(Math.pow(10, len + 1)).intValue() - 1) + min;
74+
return String.valueOf(num).substring(0,len);
75+
}
76+
}

0 commit comments

Comments
 (0)