add SmsModule

pangu
PeterAlbus 3 years ago
parent b5c12d5e13
commit 7e001bfbc3

2
.gitignore vendored

@ -35,3 +35,5 @@ build/
/target/ /target/
application.yml application.yml
5963105_www.peteralbus.com.pfx 5963105_www.peteralbus.com.pfx
SmsUtil.java
SmsUtil

@ -25,6 +25,11 @@
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId> <artifactId>spring-boot-starter-jdbc</artifactId>
</dependency> </dependency>
<!--mail-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<!--redis--> <!--redis-->
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
@ -72,6 +77,18 @@
<version>0.4.15</version> <version>0.4.15</version>
</dependency> </dependency>
<!--ali sdk-->
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.6.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-dysmsapi</artifactId>
<version>2.2.1</version>
</dependency>
<dependency> <dependency>
<groupId>org.springframework.boot</groupId> <groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>

@ -2,13 +2,23 @@ package com.peteralbus.controller;
import cn.dev33.satoken.stp.SaTokenInfo; import cn.dev33.satoken.stp.SaTokenInfo;
import cn.dev33.satoken.stp.StpUtil; import cn.dev33.satoken.stp.StpUtil;
import com.aliyuncs.exceptions.ClientException;
import com.peteralbus.domain.User; import com.peteralbus.domain.User;
import com.peteralbus.service.UserService; import com.peteralbus.service.UserService;
import com.peteralbus.util.RandomUtil;
import com.peteralbus.util.RedisUtils;
import com.peteralbus.util.SmsUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.web.bind.annotation.CrossOrigin; import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.mail.internet.MimeMessage;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
/** /**
* The type User controller. * The type User controller.
* @author PeterAlbus * @author PeterAlbus
@ -18,7 +28,15 @@ import org.springframework.web.bind.annotation.RestController;
@CrossOrigin @CrossOrigin
public class UserController public class UserController
{ {
UserService userService; private UserService userService;
private RedisUtils redisUtils;
private JavaMailSender javaMailSender;
@Autowired
public void setRedisUtils(RedisUtils redisUtils)
{
this.redisUtils = redisUtils;
}
@Autowired @Autowired
public void setUserService(UserService userService) public void setUserService(UserService userService)
@ -26,6 +44,12 @@ public class UserController
this.userService = userService; this.userService = userService;
} }
@Autowired
public void setJavaMailSender(JavaMailSender javaMailSender)
{
this.javaMailSender = javaMailSender;
}
@RequestMapping("/mailLogin") @RequestMapping("/mailLogin")
public SaTokenInfo doLogin(String userMail, String userPassword) public SaTokenInfo doLogin(String userMail, String userPassword)
{ {
@ -44,7 +68,10 @@ public class UserController
if(StpUtil.isLogin()) if(StpUtil.isLogin())
{ {
Long userId=Long.valueOf((String) StpUtil.getLoginId()); Long userId=Long.valueOf((String) StpUtil.getLoginId());
return userService.getUserById(userId); User user=userService.getUserById(userId);
user.setUserPassword(null);
user.setUserSalt(null);
return user;
} }
return null; return null;
} }
@ -56,11 +83,75 @@ public class UserController
return "logout"; return "logout";
} }
@RequestMapping("/applyMailVerifyCode")
public String applyMailVerifyCode(String account)
{
String verifyCode=RandomUtil.generateVerifyCode(6);
try {
MimeMessage message=javaMailSender.createMimeMessage();
MimeMessageHelper helper=new MimeMessageHelper(message,true);
helper.setFrom("wuhongdb@163.com","PeterAlbus");
helper.setTo(account);
helper.setSubject("[验证码]PeterAlbus的博客注册");
helper.setText("您正在注册PeterAlbus的个人博客账号验证码为"+verifyCode+"有效期10分钟。若非本人操作请忽略此邮件",false);
javaMailSender.send(message);
} catch (Exception e) {
e.printStackTrace();
return "sendMailFail";
}
redisUtils.set("verifyCode:"+account,verifyCode,10L, TimeUnit.MINUTES);
return "sendMailSuccess";
}
@RequestMapping("/applyPhoneVerifyCode")
public String applyPhoneVerifyCode(String account)
{
final String success ="smsSendSuccess";
String result="";
String verifyCode=RandomUtil.generateVerifyCode(4);
try
{
result=SmsUtil.sendSms(account,verifyCode);
if(Objects.equals(result, success))
{
redisUtils.set("verifyCode:"+account,verifyCode,10L, TimeUnit.MINUTES);
return "sendMailSuccess";
}
}
catch (ClientException e)
{
e.printStackTrace();
}
return result;
}
@RequestMapping("/register") @RequestMapping("/register")
public String register(User user) public String register(User user,String verifyCode)
{
String verifyCodeKey;
if(user.getUserMail()!=null)
{
verifyCodeKey="verifyCode:"+user.getUserMail();
}
else if(user.getUserPhone()!=null)
{
verifyCodeKey="verifyCode:"+user.getUserPhone();
}
else
{
return "needRequestVerifyCode";
}
if(redisUtils.exists(verifyCodeKey))
{
if(redisUtils.get(verifyCodeKey).equals(verifyCode))
{ {
redisUtils.remove(verifyCodeKey);
return userService.register(user); return userService.register(user);
} }
return "wrongVerifyCode";
}
return "needRequestVerifyCode";
}
@RequestMapping("/changePassword") @RequestMapping("/changePassword")
public String changePassword(Long userId,String oldPassword,String newPassword) public String changePassword(Long userId,String oldPassword,String newPassword)

@ -5,7 +5,7 @@ import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.peteralbus.domain.User; import com.peteralbus.domain.User;
import com.peteralbus.mapper.UserMapper; import com.peteralbus.mapper.UserMapper;
import com.peteralbus.service.UserService; import com.peteralbus.service.UserService;
import com.peteralbus.util.Md5Util; import com.peteralbus.util.RandomUtil;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -34,7 +34,8 @@ public class UserServiceImpl implements UserService
public String register(User user) public String register(User user)
{ {
user.setGmtCreate(LocalDateTime.now()); user.setGmtCreate(LocalDateTime.now());
user.setUserSalt(Md5Util.getSalt(8)); user.setUserIdentity(5);
user.setUserSalt(RandomUtil.getSalt(8));
user.setUserPassword(SaSecureUtil.md5BySalt(user.getUserPassword(), user.getUserSalt())); user.setUserPassword(SaSecureUtil.md5BySalt(user.getUserPassword(), user.getUserSalt()));
try try
{ {
@ -47,7 +48,6 @@ public class UserServiceImpl implements UserService
{ {
Throwable cause = e.getCause(); Throwable cause = e.getCause();
if (cause instanceof SQLIntegrityConstraintViolationException) { if (cause instanceof SQLIntegrityConstraintViolationException) {
String sqlState = ((SQLIntegrityConstraintViolationException) cause).getSQLState();
return "repeatAccount"; return "repeatAccount";
} else { } else {
e.printStackTrace(); e.printStackTrace();

@ -7,7 +7,7 @@ import java.util.Random;
* @author PeterAlbus * @author PeterAlbus
* Created on 2022/3/26. * Created on 2022/3/26.
*/ */
public class Md5Util public class RandomUtil
{ {
/** /**
* Get salt string. * Get salt string.
@ -25,4 +25,15 @@ public class Md5Util
} }
return sb.toString(); return sb.toString();
} }
public static String generateVerifyCode(int n){
Random r = new Random();
StringBuilder stringBuffer =new StringBuilder();
for(int i = 0;i < n;i ++){
int ran1 = r.nextInt(10);
stringBuffer.append(ran1);
}
return stringBuffer.toString();
}
} }

@ -0,0 +1,54 @@
package com.peteralbus.util;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
/**
* The type Sms util.
* @author PeterAlbus
* Created on 2022/3/27.
*/
public class SmsUtil
{
private static final String PRODUCT = "Dysmsapi";
private static final String DOMAIN = "dysmsapi.aliyuncs.com";
private static final String ACCESS_KEY_ID = "your access key id";
private static final String ACCESS_KEY_SECRET = "your access key secret";
private static final String OK = "OK";
/**
* Send sms int.
*
* @return the int
*/
public static String sendSms(String phone,String code) throws ClientException
{
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
IClientProfile profile = DefaultProfile.getProfile("cn-shanghai", ACCESS_KEY_ID, ACCESS_KEY_SECRET);
DefaultProfile.addEndpoint("cn-shanghai", PRODUCT, DOMAIN);
IAcsClient acsClient = new DefaultAcsClient(profile);
SendSmsRequest request = new SendSmsRequest();
request.setPhoneNumbers(phone);
/* 必填:短信签名-可在短信控制台中找到 */
request.setSignName("阿里云短信测试");
/* 必填:短信模板code-可在短信控制台中找到 */
request.setTemplateCode("SMS_154950909");
request.setTemplateParam("{\"code\":\"" + code + "\"}");
SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);
if(sendSmsResponse.getCode()!= null && sendSmsResponse.getCode().equals(OK)){
return "smsSendSuccess";
}else {
return sendSmsResponse.getMessage();
}
}
}
Loading…
Cancel
Save