提交 7209ef6d authored 作者: Whispa's avatar Whispa

commit commit

上级 d35bd969
......@@ -6,10 +6,7 @@ import com.example.afrishop_v3.config.DomainProperties;
import com.example.afrishop_v3.enums.ResultCodeEnum;
import com.example.afrishop_v3.enums.SexEnum;
import com.example.afrishop_v3.enums.UserTypeEnum;
import com.example.afrishop_v3.models.Network;
import com.example.afrishop_v3.models.TbCfCoupon;
import com.example.afrishop_v3.models.TbCfToicoupon;
import com.example.afrishop_v3.models.TbCfUserInfo;
import com.example.afrishop_v3.models.*;
import com.example.afrishop_v3.payload.request.LoginRequest;
import com.example.afrishop_v3.repository.NetworkRepository;
import com.example.afrishop_v3.repository.TbCfCouponRepository;
......@@ -17,27 +14,27 @@ import com.example.afrishop_v3.repository.TbCfToicouponRepository;
import com.example.afrishop_v3.repository.UserRepository;
import com.example.afrishop_v3.security.jwt.JwtUtils;
import com.example.afrishop_v3.security.services.UserDetailsImpl;
import com.example.afrishop_v3.util.IdUtil;
import com.example.afrishop_v3.util.*;
import org.apache.http.Consts;
import org.hashids.Hashids;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.*;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import java.text.ParseException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
@CrossOrigin(origins = "*", maxAge = 3600)
@RestController
......@@ -46,6 +43,7 @@ public class AuthController extends Controller {
private final AuthenticationManager authenticationManager;
private final UserRepository userRepository;
private final EmailHelper emailHelper;
private final TbCfCouponRepository couponRepository;
private final TbCfToicouponRepository toicouponRepository;
private final NetworkRepository networkRepository;
......@@ -56,10 +54,12 @@ public class AuthController extends Controller {
private final DomainProperties domainProperties;
private final JwtUtils jwtUtils;
private static Logger logger = LoggerFactory.getLogger(AuthController.class);
public AuthController(AuthenticationManager authenticationManager, UserRepository userRepository, TbCfCouponRepository couponRepository, TbCfToicouponRepository toicouponRepository, NetworkRepository networkRepository, EntityManager entityManager, PasswordEncoder encoder, DomainProperties domainProperties, JwtUtils jwtUtils) {
public AuthController(AuthenticationManager authenticationManager, UserRepository userRepository, EmailHelper emailHelper, TbCfCouponRepository couponRepository, TbCfToicouponRepository toicouponRepository, NetworkRepository networkRepository, EntityManager entityManager, PasswordEncoder encoder, DomainProperties domainProperties, JwtUtils jwtUtils) {
this.authenticationManager = authenticationManager;
this.userRepository = userRepository;
this.emailHelper = emailHelper;
this.couponRepository = couponRepository;
this.toicouponRepository = toicouponRepository;
this.networkRepository = networkRepository;
......@@ -156,8 +156,8 @@ public class AuthController extends Controller {
String validatePassword = validatePassword(password);
if( validatePassword != null ){
return new Result<>(ResultCodeEnum.VALIDATE_ERROR.getCode(),validatePassword);
if (validatePassword != null) {
return new Result<>(ResultCodeEnum.VALIDATE_ERROR.getCode(), validatePassword);
}
boolean byEmail = userRepository.existsByFirebaseUid(email);
......@@ -200,7 +200,7 @@ public class AuthController extends Controller {
}
private String validatePassword(String password){
private String validatePassword(String password) {
if (password == null || password.isEmpty()) {
return "Password is Empty";
......@@ -253,8 +253,8 @@ public class AuthController extends Controller {
String validatePassword = validatePassword(password);
if( validatePassword != null ){
return new Result<>(ResultCodeEnum.VALIDATE_ERROR.getCode(),validatePassword);
if (validatePassword != null) {
return new Result<>(ResultCodeEnum.VALIDATE_ERROR.getCode(), validatePassword);
}
......@@ -388,15 +388,13 @@ public class AuthController extends Controller {
}
fixCode(user);
//addToNetwork(user);
user.setLastLoginTime(new Date());
if( user.getAccount() == null){
if (user.getAccount() == null) {
user.setAccount(user.getEmail() == null ? user.getEmail() : user.getPhone());
}
......@@ -424,7 +422,6 @@ public class AuthController extends Controller {
}
}
......@@ -519,4 +516,129 @@ public class AuthController extends Controller {
tbCfUserInfoVo.setUserType(UserTypeEnum.UN_KNOW.getCode());
tbCfUserInfoVo.setEmailFlag(StateConstant.INVALID);
}
@PostMapping("/resetPassword/{account}")
public Result resetPassword(@PathVariable("account") String account, @RequestBody PasswordModel model) {
Optional<TbCfUserInfo> firstByAccount = userRepository.findFirstByAccount(account);
if (!firstByAccount.isPresent())
return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(), "User not found !!!");
if (model == null) return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(), "Empty body");
String code = model.getCode();
if (code == null || code.isEmpty())
return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(), "Verification Code is required !!");
String password = model.getPassword();
if (password == null || password.isEmpty())
return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(), "New password is required !!");
String validatePassword = validatePassword(password);
if( validatePassword != null ){
return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(),validatePassword);
}
TbCfUserInfo user = firstByAccount.get();
if (user.getVerificationCode() != null && user.getVerificationCode().equals(code)) {
user.setPassword(encoder.encode(password));
userRepository.save(user);
return new Result("Password reset successfully !");
}else {
return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(),"Verification code don't match !");
}
}
@GetMapping("/forgotPassword/{account}")
public Result getUserPhoneCode(@PathVariable("account") String account) {
Optional<TbCfUserInfo> byAccount = userRepository.findFirstByAccount(account);
if (!byAccount.isPresent())
return new Result<>(ResultCodeEnum.VALIDATE_ERROR.getCode(), "Account not found !!!");
TbCfUserInfo user = byAccount.get();
if (isPhoneValid(account)) return sendPhoneCode(user, account);
if (isEmailValid(account)) return getEmailCode(user, account);
return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(), "Invalid phone number or email");
}
private Result getEmailCode(TbCfUserInfo user, String email) {
try {
EmailTemplateBo emailTemplateBo = new EmailTemplateBo();
String name = URLDecoder.decode(email, Consts.UTF_8.name());
Integer randomCode = RandomCodeHelper.producedRandomCode(6);
emailTemplateBo.setNick(name);
emailTemplateBo.setIdentifyCode(randomCode);
Integer identifyCode = emailHelper.sendIdentifyEmail(email, EmailTemplateConstant.REGISTER, emailTemplateBo);
user.setVerificationCode(String.valueOf(identifyCode));
userRepository.save(user);
return new Result("Verification code has been sent");
} catch (Exception e) {
return new Result(ResultCodeEnum.VALIDATE_ERROR.getCode(), e.getMessage());
}
}
private Result sendPhoneCode(TbCfUserInfo user, String phone) {
Result result = new Result();
try {
phone = "+" + phone.trim();
Integer sentCount = user.getSentCount();
if (sentCount == null) {
user.setSentCount(3);
sentCount = 3;
}
Date date = new Date();
Date sentTime = user.getCodeSentTime();
long diff = date.getTime() - (sentTime == null ? 0 : sentTime.getTime());
long diffMinutes = diff / (60 * 1000) % 60;
if (diffMinutes > 15 && sentCount >= 3) {
sentCount = -1;
user.setSentCount(sentCount);
}
boolean canSend = sentCount < 3;
if (canSend) {
result.setMessage("Verification code has been sent");
//发送验证码
String code = SMSUtil.yzCode(phone);
user.setCodeSentTime(date);
user.setVerificationCode(code);
user.setSentCount(user.getSentCount() + 1);
userRepository.save(user);
logger.info("用户[" + phone + "]获取验证码成功," + "验证码:" + code);
} else {
//用户频繁操作
result.setCode(ResultCodeEnum.SERVICE_ERROR.getCode());
result.setMessage("Frequent operation, please try again after 15 minutes");
logger.info("用户[" + phone + "]获取验证码失败,操作频繁,15分钟后重试");
}
} catch (Exception e) {
result.setCode(ResultCodeEnum.SERVICE_ERROR.getCode());
result.setMessage(e.toString());
logger.info("用户[" + phone + "]获取验证码发生异常--->>>" + e.toString());
}
return result;
}
}
package com.example.afrishop_v3.models;
public class PasswordModel {
private String code;
private String password;
public String getPassword() {
return password;
}
public String getCode() {
return code;
}
}
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论