init
This commit is contained in:
@@ -45,6 +45,11 @@
|
|||||||
<artifactId>IJPay-AliPay</artifactId>
|
<artifactId>IJPay-AliPay</artifactId>
|
||||||
<version>${ijapy.version}</version>
|
<version>${ijapy.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.aliyun</groupId>
|
||||||
|
<artifactId>aliyun-java-sdk-core</artifactId>
|
||||||
|
<version>4.5.0</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
</project>
|
</project>
|
||||||
|
|||||||
@@ -77,6 +77,8 @@ public class CaiLoginManager {
|
|||||||
private UserForbidManager userForbidManager;
|
private UserForbidManager userForbidManager;
|
||||||
@Autowired
|
@Autowired
|
||||||
private AwardManager awardManager;
|
private AwardManager awardManager;
|
||||||
|
@Autowired
|
||||||
|
private SmsVerifyService smsVerifyService;
|
||||||
|
|
||||||
public String login(String username,String password){
|
public String login(String username,String password){
|
||||||
User user = userService.getByUsername(username);
|
User user = userService.getByUsername(username);
|
||||||
@@ -108,7 +110,7 @@ public class CaiLoginManager {
|
|||||||
if(!caiUser.getPassword().equals(caiUser.getPasswordCheck())){
|
if(!caiUser.getPassword().equals(caiUser.getPasswordCheck())){
|
||||||
throw new ServiceException("两次输入密码不一致,请检查");
|
throw new ServiceException("两次输入密码不一致,请检查");
|
||||||
}
|
}
|
||||||
boolean check = codeManager.check(CodeEnum.REGISTER, caiUser.getUsername(), caiUser.getCode());
|
boolean check = smsVerifyService.check(CodeEnum.REGISTER, caiUser.getUsername(), caiUser.getCode());
|
||||||
if(!check){
|
if(!check){
|
||||||
throw new ServiceException("验证码错误");
|
throw new ServiceException("验证码错误");
|
||||||
}
|
}
|
||||||
@@ -299,12 +301,15 @@ public class CaiLoginManager {
|
|||||||
return add;
|
return add;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public void resetPassword(ResetPasswordReq code) {
|
public void resetPassword(ResetPasswordReq code) {
|
||||||
User user = userService.getByUsername(code.getMobile());
|
User user = userService.getByUsername(code.getMobile());
|
||||||
if(user == null){
|
if(user == null){
|
||||||
throw new ServiceException("账户不存在");
|
throw new ServiceException("账户不存在");
|
||||||
}
|
}
|
||||||
|
boolean check = smsVerifyService.check(CodeEnum.RESET_PASSWORD, code.getMobile(), code.getCode());
|
||||||
|
if(!check){
|
||||||
|
throw new ServiceException("验证码错误");
|
||||||
|
}
|
||||||
userService.resetPassword(user.getId(),code.getPassword());
|
userService.resetPassword(user.getId(),code.getPassword());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,14 @@
|
|||||||
|
package com.ruoyi.cai.config;
|
||||||
|
|
||||||
|
import lombok.Data;
|
||||||
|
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
|
@Data
|
||||||
|
@Component
|
||||||
|
@ConfigurationProperties(prefix = "ali.sms")
|
||||||
|
public class AliSmsProperties {
|
||||||
|
private String accessKeyId = "LTAI5tRf2jUyoYmAC9kw4AMF";
|
||||||
|
private String accessKeySecret = "frvNLpySKxC38eihnWKi3NMFZSume3";
|
||||||
|
private String signName = "武汉康慧创欣科技";
|
||||||
|
}
|
||||||
@@ -76,10 +76,6 @@ public class AuthAppController {
|
|||||||
if(!mobile){
|
if(!mobile){
|
||||||
return R.fail(600,"请输入正确的手机格式");
|
return R.fail(600,"请输入正确的手机格式");
|
||||||
}
|
}
|
||||||
User user = userService.getByUsername(code.getMobile());
|
|
||||||
if(user == null){
|
|
||||||
throw new ServiceException("用户不存在");
|
|
||||||
}
|
|
||||||
smsVerifyService.put(CodeEnum.RESET_PASSWORD,code.getMobile());
|
smsVerifyService.put(CodeEnum.RESET_PASSWORD,code.getMobile());
|
||||||
return R.ok("发送成功");
|
return R.ok("发送成功");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -4,13 +4,14 @@ import lombok.Getter;
|
|||||||
|
|
||||||
@Getter
|
@Getter
|
||||||
public enum CodeEnum {
|
public enum CodeEnum {
|
||||||
REGISTER("注册短信"),
|
REGISTER("注册短信","SMS_294195165"),
|
||||||
RESET_PASSWORD("重置密码")
|
RESET_PASSWORD("重置密码","SMS_294195165")
|
||||||
;
|
;
|
||||||
|
|
||||||
private final String name;
|
private final String name;
|
||||||
|
private final String aliTemplate;
|
||||||
|
|
||||||
CodeEnum(String name) {
|
CodeEnum(String name, String aliTemplate) {
|
||||||
this.name = name;
|
this.name = name;
|
||||||
|
this.aliTemplate = aliTemplate;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
144
ruoyi-cai/src/main/java/com/ruoyi/cai/kit/AliSmsKit.java
Normal file
144
ruoyi-cai/src/main/java/com/ruoyi/cai/kit/AliSmsKit.java
Normal file
@@ -0,0 +1,144 @@
|
|||||||
|
package com.ruoyi.cai.kit;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSON;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.aliyuncs.CommonRequest;
|
||||||
|
import com.aliyuncs.CommonResponse;
|
||||||
|
import com.aliyuncs.DefaultAcsClient;
|
||||||
|
import com.aliyuncs.IAcsClient;
|
||||||
|
import com.aliyuncs.exceptions.ClientException;
|
||||||
|
import com.aliyuncs.http.MethodType;
|
||||||
|
import com.aliyuncs.profile.DefaultProfile;
|
||||||
|
import com.aliyuncs.profile.IClientProfile;
|
||||||
|
import com.google.common.collect.Lists;
|
||||||
|
import com.ruoyi.cai.config.AliSmsProperties;
|
||||||
|
import com.ruoyi.cai.enums.CodeEnum;
|
||||||
|
import lombok.Setter;
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.apache.http.util.Asserts;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.stereotype.Component;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import javax.annotation.PostConstruct;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Component
|
||||||
|
@Slf4j
|
||||||
|
public class AliSmsKit {
|
||||||
|
@Autowired
|
||||||
|
@Setter
|
||||||
|
private AliSmsProperties config;
|
||||||
|
private IClientProfile profile;
|
||||||
|
|
||||||
|
@PostConstruct
|
||||||
|
public void init() {
|
||||||
|
System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
|
||||||
|
System.setProperty("sun.net.client.defaultReadTimeout", "10000");
|
||||||
|
profile = DefaultProfile.getProfile("cn-hangzhou", config.getAccessKeyId(), config.getAccessKeySecret());
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean batchSendMessage(List<String> phone,String messageTemplate){
|
||||||
|
Asserts.notBlank(messageTemplate, "短信模板不能为空");
|
||||||
|
IAcsClient client = new DefaultAcsClient(profile);
|
||||||
|
CommonRequest request = new CommonRequest();
|
||||||
|
request.setSysMethod(MethodType.POST);
|
||||||
|
request.setSysDomain("dysmsapi.aliyuncs.com");
|
||||||
|
request.setSysVersion("2017-05-25");
|
||||||
|
request.setSysAction("SendBatchSms");
|
||||||
|
List<String> signName = phone.stream().map(i -> config.getSignName()).collect(Collectors.toList());
|
||||||
|
request.putQueryParameter("TemplateCode", messageTemplate);
|
||||||
|
request.putQueryParameter("SignNameJson", JSON.toJSONString(signName));
|
||||||
|
request.putQueryParameter("PhoneNumberJson", JSON.toJSONString(phone));
|
||||||
|
try {
|
||||||
|
CommonResponse response = client.getCommonResponse(request);
|
||||||
|
String data = response.getData();
|
||||||
|
JSONObject jsonObject = JSONObject.parseObject(data);
|
||||||
|
if(jsonObject.get("Code") == null || !"OK".equals(jsonObject.get("Code"))){
|
||||||
|
log.error("短信发送失败 response={}", jsonObject.toJSONString());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (ClientException e) {
|
||||||
|
log.error(e.getMessage(),e);
|
||||||
|
throw new RuntimeException("短信发送失败!");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean sendMessage(String phone, String messageTemplate, String messageCode) {
|
||||||
|
Asserts.notBlank(messageTemplate, "短信模板不能为空");
|
||||||
|
IAcsClient client = new DefaultAcsClient(profile);
|
||||||
|
CommonRequest request = new CommonRequest();
|
||||||
|
request.setSysMethod(MethodType.POST);
|
||||||
|
request.setSysDomain("dysmsapi.aliyuncs.com");
|
||||||
|
request.setSysVersion("2017-05-25");
|
||||||
|
request.setSysAction("SendSms");
|
||||||
|
request.putQueryParameter("SignName", config.getSignName());
|
||||||
|
request.putQueryParameter("RegionId", "cn-hangzhou");
|
||||||
|
request.putQueryParameter("PhoneNumbers", phone);
|
||||||
|
request.putQueryParameter("TemplateCode", messageTemplate);
|
||||||
|
if (StringUtils.isNoneBlank(messageCode)) {
|
||||||
|
request.putQueryParameter("TemplateParam", "{\"code\":\"" + messageCode + "\"}");
|
||||||
|
}
|
||||||
|
CommonResponse response = null;
|
||||||
|
try {
|
||||||
|
response = client.getCommonResponse(request);
|
||||||
|
String data = response.getData();
|
||||||
|
JSONObject jsonObject = JSONObject.parseObject(data);
|
||||||
|
if(jsonObject.get("Code") == null || !"OK".equals(jsonObject.get("Code"))){
|
||||||
|
log.error("短信发送失败 response={}", jsonObject.toJSONString());
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
} catch (ClientException e) {
|
||||||
|
log.error(e.getMessage(),e);
|
||||||
|
throw new RuntimeException("短信发送失败!");
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public boolean sendMessage(String phone, String messageTemplate, String messageCode, boolean isAsy) {
|
||||||
|
if (isAsy) {
|
||||||
|
new Thread(() -> {
|
||||||
|
sendMessage(phone, messageTemplate, messageCode);
|
||||||
|
}).start();
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return sendMessage(phone, messageTemplate, messageCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean batchSendMessagePublic(List<String> phone,String messageTemplate){
|
||||||
|
if(CollectionUtils.isEmpty(phone)){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
List<List<String>> list = Lists.partition(phone, 100);
|
||||||
|
for (List<String> phoneList : list) {
|
||||||
|
this.batchSendMessage(phoneList,messageTemplate);
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 异步发送消息
|
||||||
|
* @param phone
|
||||||
|
* @param messageTemplate
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public boolean sendMessageAsync(String phone, String messageTemplate) {
|
||||||
|
return sendMessage(phone,messageTemplate,null,true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
AliSmsKit messageSenderUtil = new AliSmsKit();
|
||||||
|
AliSmsProperties config = new AliSmsProperties();
|
||||||
|
messageSenderUtil.setConfig(config);
|
||||||
|
messageSenderUtil.init();
|
||||||
|
messageSenderUtil.batchSendMessagePublic(Arrays.asList("15302786929"), CodeEnum.REGISTER.getAliTemplate());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -23,6 +23,11 @@ public class CodeManager {
|
|||||||
return String.format(RedisConstant.CODE_REDIS,codeEnum.name(),phone);
|
return String.format(RedisConstant.CODE_REDIS,codeEnum.name(),phone);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void put(CodeEnum codeEnum,String phone,String code){
|
||||||
|
String key = getKey(codeEnum, phone);
|
||||||
|
redisTemplate.opsForValue().set(key,code,5, TimeUnit.MINUTES);
|
||||||
|
}
|
||||||
|
|
||||||
public String put(CodeEnum codeEnum,String phone){
|
public String put(CodeEnum codeEnum,String phone){
|
||||||
String code = RandomUtil.randomNumbers(6);
|
String code = RandomUtil.randomNumbers(6);
|
||||||
String key = getKey(codeEnum, phone);
|
String key = getKey(codeEnum, phone);
|
||||||
|
|||||||
@@ -12,5 +12,7 @@ import com.ruoyi.cai.enums.CodeEnum;
|
|||||||
*/
|
*/
|
||||||
public interface SmsVerifyService extends IService<SmsVerify> {
|
public interface SmsVerifyService extends IService<SmsVerify> {
|
||||||
|
|
||||||
|
boolean check(CodeEnum codeEnum, String mobile, String code);
|
||||||
|
|
||||||
void put(CodeEnum codeEnum, String mobile);
|
void put(CodeEnum codeEnum, String mobile);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,12 +1,23 @@
|
|||||||
package com.ruoyi.cai.service.impl;
|
package com.ruoyi.cai.service.impl;
|
||||||
|
|
||||||
|
import cn.hutool.core.util.RandomUtil;
|
||||||
|
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
|
||||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||||
import com.ruoyi.cai.domain.SmsVerify;
|
import com.ruoyi.cai.domain.SmsVerify;
|
||||||
|
import com.ruoyi.cai.domain.User;
|
||||||
|
import com.ruoyi.cai.domain.UserInfo;
|
||||||
import com.ruoyi.cai.enums.CodeEnum;
|
import com.ruoyi.cai.enums.CodeEnum;
|
||||||
|
import com.ruoyi.cai.enums.SystemConfigEnum;
|
||||||
|
import com.ruoyi.cai.kit.AliSmsKit;
|
||||||
import com.ruoyi.cai.manager.CodeManager;
|
import com.ruoyi.cai.manager.CodeManager;
|
||||||
|
import com.ruoyi.cai.manager.SystemConfigManager;
|
||||||
import com.ruoyi.cai.mapper.SmsVerifyMapper;
|
import com.ruoyi.cai.mapper.SmsVerifyMapper;
|
||||||
import com.ruoyi.cai.service.SmsVerifyService;
|
import com.ruoyi.cai.service.SmsVerifyService;
|
||||||
|
import com.ruoyi.cai.service.UserInfoService;
|
||||||
|
import com.ruoyi.cai.service.UserService;
|
||||||
|
import com.ruoyi.common.exception.ServiceException;
|
||||||
import com.ruoyi.common.utils.ServletUtils;
|
import com.ruoyi.common.utils.ServletUtils;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
@@ -24,19 +35,85 @@ public class SmsVerifyServiceImpl extends ServiceImpl<SmsVerifyMapper,SmsVerify>
|
|||||||
|
|
||||||
@Autowired
|
@Autowired
|
||||||
private CodeManager codeManager;
|
private CodeManager codeManager;
|
||||||
|
@Autowired
|
||||||
|
private UserInfoService userInfoService;
|
||||||
|
@Autowired
|
||||||
|
private UserService userService;
|
||||||
|
@Autowired
|
||||||
|
private AliSmsKit aliSmsKit;
|
||||||
|
@Autowired
|
||||||
|
private SystemConfigManager systemConfigManager;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean check(CodeEnum codeEnum, String mobile, String code){
|
||||||
|
String smsAdmin = systemConfigManager.getSystemConfig(SystemConfigEnum.SMS_CODE_ADMIN);
|
||||||
|
if(StringUtils.isNotEmpty(smsAdmin) && smsAdmin.equals(code)){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
SmsVerify one = this.getOne(Wrappers.lambdaQuery(SmsVerify.class)
|
||||||
|
.eq(SmsVerify::getType, codeEnum)
|
||||||
|
.eq(SmsVerify::getReceivePhone, mobile)
|
||||||
|
.eq(SmsVerify::getStatus, 1)
|
||||||
|
.gt(SmsVerify::getOverTime, LocalDateTime.now())
|
||||||
|
.orderByDesc(SmsVerify::getId)
|
||||||
|
.last("limit 1"));
|
||||||
|
if(one == null){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
if(one.getVerifyCode().equals(code)){
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
// codeManager.check(codeEnum,mobile,code);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void put(CodeEnum codeEnum,String mobile){
|
public void put(CodeEnum codeEnum,String mobile){
|
||||||
String code = codeManager.put(codeEnum, mobile);
|
String clientIP = ServletUtils.getClientIP();
|
||||||
|
if(codeEnum == CodeEnum.REGISTER){
|
||||||
|
long count = userService.count(Wrappers.lambdaQuery(User.class)
|
||||||
|
.eq(User::getMobile, mobile));
|
||||||
|
if(count > 0){
|
||||||
|
throw new ServiceException("手机号已经被注册使用!");
|
||||||
|
}
|
||||||
|
}else if(codeEnum == CodeEnum.RESET_PASSWORD){
|
||||||
|
long count = userService.count(Wrappers.lambdaQuery(User.class)
|
||||||
|
.eq(User::getMobile, mobile));
|
||||||
|
if(count == 0){
|
||||||
|
throw new ServiceException("手机号未注册!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LocalDateTime now = LocalDateTime.now();
|
||||||
|
SmsVerify one = this.getOne(Wrappers.lambdaQuery(SmsVerify.class)
|
||||||
|
.eq(SmsVerify::getType, codeEnum.name())
|
||||||
|
.eq(SmsVerify::getReceivePhone, mobile)
|
||||||
|
.eq(SmsVerify::getStatus, 1)
|
||||||
|
.gt(SmsVerify::getOverTime, now)
|
||||||
|
.last("limit 1"));
|
||||||
|
if(one != null){
|
||||||
|
long diff = now.until(one.getCreateTime(), ChronoUnit.SECONDS);
|
||||||
|
if(diff < 60){
|
||||||
|
throw new ServiceException("请"+diff+"秒之后再重试发送!");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
long count = userInfoService.count(Wrappers.lambdaQuery(UserInfo.class).eq(UserInfo::getRegIp, clientIP));
|
||||||
|
if(count > 8){
|
||||||
|
throw new ServiceException("验证码发送失败!2"+count);
|
||||||
|
}
|
||||||
|
String code = RandomUtil.randomNumbers(6);
|
||||||
SmsVerify smsVerify = new SmsVerify();
|
SmsVerify smsVerify = new SmsVerify();
|
||||||
smsVerify.setType(codeEnum.name());
|
smsVerify.setType(codeEnum.name());
|
||||||
smsVerify.setType(codeEnum.getName());
|
|
||||||
smsVerify.setReceivePhone(mobile);
|
smsVerify.setReceivePhone(mobile);
|
||||||
smsVerify.setVerifyCode(code);
|
smsVerify.setVerifyCode(code);
|
||||||
smsVerify.setSendInterface("阿里云");
|
smsVerify.setSendInterface("阿里云");
|
||||||
smsVerify.setOperateIp(ServletUtils.getClientIP());
|
smsVerify.setOperateIp(clientIP);
|
||||||
smsVerify.setOverTime(LocalDateTime.now().plus(3, ChronoUnit.MINUTES));
|
smsVerify.setOverTime(LocalDateTime.now().plus(1, ChronoUnit.MINUTES));
|
||||||
this.save(smsVerify);
|
this.save(smsVerify);
|
||||||
|
boolean boo = aliSmsKit.sendMessage(mobile, codeEnum.getAliTemplate(), code, false);
|
||||||
|
if(!boo){
|
||||||
|
this.removeById(smsVerify);
|
||||||
|
}
|
||||||
|
// String code = codeManager.put(codeEnum, mobile);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user