1.add image check code for user login, 2. add aspect to validate the user login parameters 3.add deviceId/deviceBrand in login part

main
Gary 1 year ago
parent 83bf6b065a
commit 4ec0092e2a

@ -29,7 +29,7 @@ public enum StatusCode implements IErrorCode {
// tag 模块错误码以50XXX不足5位补0;
TAG_COMMON_FAILED(50000, "标签模块错误"),
// recruit 模块错误码以50XXX不足5位补0;
// recruit 模块错误码以60XXX不足5位补0;
RECRUIT_COMMON_FAILED(60000, "招聘模块错误");
private int code;

@ -1,8 +1,10 @@
package exception;
import api.StatusCode;
public class BizException extends RuntimeException {
private static final long serialVersionUID = 2466145171145432619L;
private StatusCode codeEnum;
private Integer code = 500;
public BizException(String message) {
@ -14,7 +16,25 @@ public class BizException extends RuntimeException {
this.code = code;
}
public StatusCode getCodeEnum() {
return codeEnum;
}
public Integer getCode() {
return this.code;
}
public BizException(StatusCode codeEnum) {
super(codeEnum.getMessage());
this.codeEnum = codeEnum;
this.code = codeEnum.getCode();
}
/**
* fillInStackTrace .
*/
@Override
public Throwable fillInStackTrace() {
return this;
}
}

@ -0,0 +1,12 @@
package com.luoo.user.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface GlobalInterceptor {
boolean checkParam() default true;
}

@ -0,0 +1,17 @@
package com.luoo.user.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import com.luoo.user.enums.VerifyRegexEnum;
@Target({ElementType.PARAMETER,ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface VerifyParam {
VerifyRegexEnum regex() default VerifyRegexEnum.NO;
int min() default -1;
int max() default -1;
boolean required() default false;
}

@ -0,0 +1,93 @@
package com.luoo.user.aspect;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.lang.reflect.Parameter;
import org.apache.commons.lang3.ArrayUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import com.luoo.user.annotation.GlobalInterceptor;
import com.luoo.user.annotation.VerifyParam;
import com.luoo.user.util.StringTools;
import com.luoo.user.util.VerifyUtils;
import api.StatusCode;
import exception.BizException;
@Aspect
@Component("operationAspect")
public class OperationAspect {
static Logger logger= LoggerFactory.getLogger(OperationAspect.class);
private static final String[] BASE_TYPE_ARRAY=new String[] {"java.lang.String","java.lang.Integer","java.lang.Long"};
@Before("@annotation(com.luoo.user.annotation.GlobalInterceptor)")
public void interceptorDo(JoinPoint point) {
Object[] arguments=point.getArgs();
Method method=((MethodSignature)point.getSignature()).getMethod();
GlobalInterceptor interceptor=method.getAnnotation(GlobalInterceptor.class);
if(null==interceptor) {
return;
}
if(interceptor.checkParam()) {
validateParams(method,arguments);
}
}
private void validateParams(Method method, Object[] arguments) {
Parameter[] parameters=method.getParameters();
for(int i=0;i<parameters.length;i++) {
Parameter parameter=parameters[i];
Object value=arguments[i];
VerifyParam verifyParam=parameter.getAnnotation(VerifyParam.class);
if(null==verifyParam) {
continue;
}
String paramTypeName=parameter.getParameterizedType().getTypeName();
if(ArrayUtils.contains(BASE_TYPE_ARRAY, paramTypeName)) {
checkValue(value,verifyParam);
}else {
checkObjValue(parameter,value);
}
}
}
private void checkObjValue(Parameter parameter,Object value) {
try {
String typeName=parameter.getParameterizedType().getTypeName();
Class<?> clazz=Class.forName(typeName);
Field[] fields= clazz.getDeclaredFields();
for(Field field:fields) {
VerifyParam fieldVerifyParam=field.getAnnotation(VerifyParam.class);
if(null==fieldVerifyParam) {
continue;
}
field.setAccessible(true);
Object resultValue=field.get(value);
checkValue(resultValue,fieldVerifyParam);
}
}catch(Exception e) {
logger.error(StatusCode.VALIDATE_FAILED.getMessage(),e.getMessage());
throw new BizException(StatusCode.VALIDATE_FAILED);
}
}
private void checkValue(Object value, VerifyParam verifyParam) {
boolean isEmpty= null==value||StringTools.isEmpty(value.toString());
int length= null==value?0:value.toString().length();
if(isEmpty&&verifyParam.required()) {
throw new BizException(StatusCode.VALIDATE_FAILED);
}
if(!isEmpty&&(-1!=verifyParam.max()&&verifyParam.max()<length||-1!=verifyParam.min()&&verifyParam.min()>length)) {
throw new BizException(StatusCode.VALIDATE_FAILED);
}
if(!isEmpty&&!StringTools.isEmpty(verifyParam.regex().getRegex())&&!VerifyUtils.verify(verifyParam.regex(), String.valueOf(value))) {
throw new BizException(StatusCode.VALIDATE_FAILED);
}
}
}

@ -0,0 +1,8 @@
package com.luoo.user.constants;
public class Constants {
public static final String IMAGE_CHECK_CODE_KEY="image_check_code_key";
public static final String REDIS_KEY_IMAGE_CHECK_CODE="redis_key_image_check_code_";
public static final String REDIS_KEY_MOBILE_CHECK_CODE="redis_key_mobile_check_code_";
}

@ -1,19 +1,32 @@
package com.luoo.user.controller;
import exception.BizException;
import api.Result;
import api.StatusCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
*
*/
@Slf4j
@ControllerAdvice
public class BaseExceptionHandler {
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result<Void> error(Exception e){
e.printStackTrace();
return Result.failed(StatusCode.USER_COMMON_FAILED);
}
@ExceptionHandler(value = Exception.class)
@ResponseBody
public Result<Void> error(Exception e) {
log.error("执行出错", e);
return Result.failed(StatusCode.USER_COMMON_FAILED, e.getMessage());
}
@ExceptionHandler(value = BizException.class)
@ResponseBody
public Result<String> error(BizException e) {
log.info("业务错误:{}", e.getMessage());
StatusCode statusCode = null == e.getCodeEnum() ? StatusCode.USER_COMMON_FAILED : e.getCodeEnum();
return Result.failed(statusCode, e.getMessage());
}
}

@ -1,16 +1,28 @@
package com.luoo.user.controller;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import com.luoo.user.annotation.GlobalInterceptor;
import com.luoo.user.annotation.VerifyParam;
import com.luoo.user.constants.Constants;
import com.luoo.user.dto.response.CreateImageCode;
import com.luoo.user.dto.response.UserRespDTO;
import com.luoo.user.enums.VerifyRegexEnum;
import com.luoo.user.pojo.User;
import com.luoo.user.service.UserService;
import com.luoo.user.util.NickNameUtil;
import api.PageResult;
import api.Result;
@ -76,37 +88,91 @@ public class UserController {
}
@ApiOperation(value = "2.登录/注册后返回token")
@PostMapping("/appLogin/{mobile}/{checkcode}")
public Result appLogin(@PathVariable String mobile, @PathVariable String checkcode) {
@PostMapping("/appLogin/{deviceId}/{deviceBrand}/{mobile}/{mobileCheckCode}")
@GlobalInterceptor
public Result appLogin( @PathVariable @VerifyParam(required=true,max=32) String deviceId,
@PathVariable @VerifyParam(required=false,max=30) String deviceBrand,
@PathVariable @VerifyParam(required=true,regex=VerifyRegexEnum.MOBILE)String mobile,
@PathVariable @VerifyParam(required=true,regex=VerifyRegexEnum.MOBILE_CHECK_CODE) String mobileCheckCode
) {
// 得到缓存中的验证码
String checkcodeRedis = (String) redisTemplate.opsForValue().get("checkcode_" + mobile);
if (null == checkcodeRedis || checkcodeRedis.isEmpty()) {
return Result.failed(StatusCode.USER_VERIFICATION_CODE_EXPIRED);
String redisKey=Constants.REDIS_KEY_MOBILE_CHECK_CODE+deviceId;
try {
UserRespDTO userRespDTO=new UserRespDTO();
String checkcodeRedis = (String) redisTemplate.opsForValue().get(redisKey);
if (null == checkcodeRedis || checkcodeRedis.isEmpty()) {
return Result.failed(StatusCode.USER_VERIFICATION_CODE_EXPIRED);
}
if (!checkcodeRedis.equals(mobileCheckCode)) {
return Result.failed(StatusCode.USER_VERIFICATION_CODE_MISMATCH);
}
User user = userService.loginOrRegister(deviceId,deviceBrand,mobile);
BeanUtils.copyProperties(user, userRespDTO);
String token = jwtUtil.createJWT(user.getId(),user.getMobile(),"user");
userRespDTO.setToken(token);
return Result.success(userRespDTO);
}finally {
redisTemplate.delete(redisKey);
}
if (!checkcodeRedis.equals(checkcode)) {
return Result.failed(StatusCode.USER_VERIFICATION_CODE_MISMATCH);
}
UserRespDTO userRespDTO = userService.loginOrRegister(mobile);
return Result.success(userRespDTO);
}
@ApiOperation(value = "3.游客登录返回token", notes = "token中的subject和roles均为tourist")
@GetMapping("/touristLogin")
public Result<UserRespDTO> touristLogin() {
UserRespDTO userRespDTO = userService.touristLogin();
return Result.success(userRespDTO);
UserRespDTO userRespDTO=new UserRespDTO();
userRespDTO.setId(String.valueOf(idWorker.nextId()));
userRespDTO.setNickname("游客"+NickNameUtil.getRandomNickName());
String token = jwtUtil.createJWT(userRespDTO.getId(),"tourist","tourist");
userRespDTO.setToken(token);
return Result.success(userRespDTO);
}
/**
*
*/
@ApiOperation(value = "1.发送短信验证码")
@PostMapping("/sendsms/{mobile}")
public Result<Void> sendSms(@PathVariable String mobile) {
userService.sendSms(mobile);
@ApiOperation(value = "1.发送短信验证码有效期15分钟")
@PostMapping("/sendsms/{deviceId}/{mobile}")
@GlobalInterceptor
public Result<Void> sendSms(@PathVariable @VerifyParam(required=true,max=32) String deviceId,
@PathVariable @VerifyParam(required=true,regex=VerifyRegexEnum.MOBILE)String mobile) {
userService.sendSms(deviceId,mobile);
return Result.success();
}
/**
* token
*/
/*
* @ApiOperation(value = "5.token 续期默认token 有效期7天")
*
* @PostMapping("/autoLogin")
*
* @GlobalInterceptor public Result<String>
* autoLogin(@PathVariable @VerifyParam(required=true,max=32) String deviceId,
*
* @PathVariable @VerifyParam(required=true,regex=VerifyRegexEnum.MOBILE)String
* mobile) { userService.sendSms(deviceId,mobile); return Result.success(); }
*/
/**
*
*/
@ApiOperation(value = "4.三次短信验证码失败后获取图行验证码有效期10分钟")
@GetMapping("/imageCheckCode/{deviceId}")
@GlobalInterceptor
public void imageCheckCode(HttpServletResponse response,
@PathVariable @VerifyParam(required=true,max=32) String deviceId) throws IOException {
CreateImageCode vCode = new CreateImageCode(130,38,5,10);
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
response.setDateHeader("Expires", 0);
response.setContentType("image/jpeg");
String code=vCode.getCode();
String redisKey=Constants.REDIS_KEY_IMAGE_CHECK_CODE+deviceId;
redisTemplate.opsForValue().set(redisKey,code,10,TimeUnit.MINUTES);
vCode.write(response.getOutputStream());
}
@PostMapping("/register/{code}")
public Result regist(@PathVariable String code, @RequestBody User user) {

@ -26,5 +26,4 @@ public interface UserDao extends JpaRepository<User,String>,JpaSpecificationExec
public User findByLoginname(String loginname);
}

@ -1,20 +0,0 @@
package com.luoo.user.dto;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;
@Data
public class UserVO {
@ApiModelProperty(value = "ID")
private String id;//ID
@ApiModelProperty(value = "昵称首次注册登录登录用户随机为“雀乐XXX”游客为“游客XXX”",example="雀乐XXX")
private String nickname;//昵称
@ApiModelProperty(value = "性别0为男1 为女首次注册登录默认为0",example="0")
private String sex;//性别
@ApiModelProperty(value = "头像,首次注册登录,默认头像",example="default")
private String avatar;//头像
@ApiModelProperty(value = "个性签名,首次注册登录,默认为“无签名”",example="无签名")
private String personality;//个性
@ApiModelProperty(value = "TOKEN")
private String token;
}

@ -0,0 +1,139 @@
package com.luoo.user.dto.response;
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Random;
import javax.imageio.ImageIO;
public class CreateImageCode {
private int width = 160;
private int height = 40;
private int codeCount = 4;
// 干扰线数
private int lineCount = 20;
// 验证码图片Buffer
private BufferedImage buffImg = null;
// 验证码
private String code = null;
public String getCode() {
return code;
}
public void setCode(String code) {
this.code = code;
}
Random random = new Random();
public CreateImageCode() {
createImage();
}
public CreateImageCode(int width, int height) {
this.width = width;
this.height = height;
createImage();
}
public CreateImageCode(int width, int height, int codeCount) {
this.width = width;
this.height = height;
this.codeCount = codeCount;
createImage();
}
public CreateImageCode(int width, int height, int codeCount, int lineCount) {
this.width = width;
this.height = height;
this.codeCount = codeCount;
this.lineCount = lineCount;
createImage();
}
// 生成图片
private void createImage() {
int fontWidth = width / codeCount;// 字体宽度。
int fontHeight = height - 5;// 字体高度。
int codeY = height - 8;
// 得到图片
buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = buffImg.getGraphics();
// 设置背景色
g.setColor(getRandColor(200, 250));
g.fillRect(0, 0, width, height);
// 设置边框
//g.setColor(getRandColor(200, 250));
//g.drawRect(1, 1, width - 2, height - 2);
// 设置字体
Font font = new Font("Fixedsys", Font.BOLD, fontHeight);
g.setFont(font);
// 设置干扰线
for (int i = 0; i < lineCount; i++) {
int x1 = random.nextInt(width);
int y1 = random.nextInt(height);
int x2 = random.nextInt(width);
int y2 = random.nextInt(height);
g.setColor(getRandColor(1, 255));
g.drawLine(x1, y1, x2, y2);
}
// 添加噪点
float yawpRate = 0.01f;// 噪声率
int area = (int) (yawpRate * width * height);
for (int i = 0; i < area; i++) {
int x = random.nextInt(width);
int y = random.nextInt(height);
buffImg.setRGB(x, y, random.nextInt(255));
}
String str1 = randomStr(codeCount);// 得到随机字符
this.code = str1;
for (int i = 0; i < codeCount; i++) {
String strRand = str1.substring(i, i + 1);
g.setColor(getRandColor(1, 255));
// g.drawString(a,x,y);
// a为要画出来的东西x和y表示要画的东西最左侧字符的基线位于此图形上下文坐标系的 (x, y) 位置处
g.drawString(strRand, i * fontWidth + 3, codeY);
}
}
// 得到随机字符
private String randomStr(int n) {
String str1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
String str2 = "";
int len = str1.length() - 1;
double r;
for (int i = 0; i < n; i++) {
r = (Math.random()) * len;
str2 = str2 + str1.charAt((int) r);
}
return str2;
}
// 得到随机颜色
private Color getRandColor(int fc, int bc) {// 给定范围获得随机颜色
if (fc > 255)
fc = 255;
if (bc > 255)
bc = 255;
int r = fc + random.nextInt(bc - fc);
int g = fc + random.nextInt(bc - fc);
int b = fc + random.nextInt(bc - fc);
return new Color(r, g, b);
}
public void write(OutputStream sos) throws IOException {
ImageIO.write(buffImg, "png", sos);
sos.close();
}
}

@ -8,11 +8,11 @@ public class UserRespDTO {
private String id;//ID
@ApiModelProperty(value = "昵称首次注册登录登录用户随机为“雀乐XXX”游客为“游客XXX”",example="雀乐XXX")
private String nickname;//昵称
@ApiModelProperty(value = "性别0为男1 为女,首次注册登录,默认为0",example="0")
@ApiModelProperty(value = "性别0为男1 为女,首次注册登录,留空不显示",example="0")
private String sex;//性别
@ApiModelProperty(value = "头像,首次注册登录,默认头像",example="default")
@ApiModelProperty(value = "头像,首次注册登录,UI设计默认头像")
private String avatar;//头像
@ApiModelProperty(value = "个性签名,首次注册登录,默认为“无签名”",example="无签名")
@ApiModelProperty(value = "个性签名,首次注册登录,默认为“无签名”")
private String personality;//个性
@ApiModelProperty(value = "TOKEN")
private String token;

@ -0,0 +1,24 @@
package com.luoo.user.enums;
public enum VerifyRegexEnum {
NO("", "不校验"),
MOBILE("(1[0-9])\\d{9}$", "手机号码"),
MOBILE_CHECK_CODE("\\d{6}$", "手机短信验证号码"),
PASSWORD("^(?=.*\\d)(?=.*[a-zA-Z])[\\da-zA-Z~!@#$%^&*_]{8,}$", "只能是数字,字母,特殊字符 8-18位");
private String regex;
private String desc;
VerifyRegexEnum(String regex, String desc) {
this.regex = regex;
this.desc = desc;
}
public String getRegex() {
return regex;
}
public String getDesc() {
return desc;
}
}

@ -9,12 +9,17 @@ import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import lombok.Getter;
import lombok.Setter;
import java.io.Serializable;
/**
*
* @author Administrator
*
*/
@Getter
@Setter
@Entity
@Table(name="tb_user")
@EntityListeners(AuditingEntityListener.class)
@ -41,127 +46,6 @@ public class User implements Serializable{
private String personality;//个性
private Integer fanscount;//粉丝数
private Integer followcount;//关注数
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getLoginname() {
return loginname;
}
public void setLoginname(String loginname) {
this.loginname = loginname;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getNickname() {
return nickname;
}
public void setNickname(String nickname) {
this.nickname = nickname;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public java.util.Date getBirthday() {
return birthday;
}
public void setBirthday(java.util.Date birthday) {
this.birthday = birthday;
}
public String getAvatar() {
return avatar;
}
public void setAvatar(String avatar) {
this.avatar = avatar;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public java.util.Date getRegdate() {
return regdate;
}
public void setRegdate(java.util.Date regdate) {
this.regdate = regdate;
}
public java.util.Date getUpdatedate() {
return updatedate;
}
public void setUpdatedate(java.util.Date updatedate) {
this.updatedate = updatedate;
}
public java.util.Date getLastdate() {
return lastdate;
}
public void setLastdate(java.util.Date lastdate) {
this.lastdate = lastdate;
}
public Long getOnline() {
return online;
}
public void setOnline(Long online) {
this.online = online;
}
public String getInterest() {
return interest;
}
public void setInterest(String interest) {
this.interest = interest;
}
public String getPersonality() {
return personality;
}
public void setPersonality(String personality) {
this.personality = personality;
}
public Integer getFanscount() {
return fanscount;
}
public void setFanscount(Integer fanscount) {
this.fanscount = fanscount;
}
public Integer getFollowcount() {
return followcount;
}
public void setFollowcount(Integer followcount) {
this.followcount = followcount;
}
//private String deviceId;//设备id
//private String deviceBrand;//设备品牌
}

@ -24,10 +24,12 @@ import org.springframework.transaction.annotation.Transactional;
import util.IdWorker;
import com.apifan.common.random.RandomSource;
import com.luoo.user.constants.Constants;
import com.luoo.user.dao.UserDao;
import com.luoo.user.dto.response.UserRespDTO;
import com.luoo.user.enumerate.Gender;
import com.luoo.user.pojo.User;
import com.luoo.user.util.NickNameUtil;
import util.JwtUtil;
@ -216,11 +218,12 @@ public class UserService {
}
public void sendSms(String mobile) {
public void sendSms(String deviceId,String mobile) {
// 生成6位数字随机数
String checkcode = RandomStringUtils.randomNumeric(6);
// 向缓存中放一份
redisTemplate.opsForValue().set("checkcode_"+mobile,checkcode,6, TimeUnit.HOURS);
String redisKey=Constants.REDIS_KEY_MOBILE_CHECK_CODE+deviceId;
redisTemplate.opsForValue().set(redisKey,checkcode,15, TimeUnit.MINUTES);
// 向用户发一份
Map<String,String> map = new HashMap<>();
@ -247,48 +250,28 @@ public class UserService {
userDao.updatefanscount(x,friendid);
userDao.updatefollowcount(x,userid);
}
public UserRespDTO loginOrRegister(String mobile) {
public User loginOrRegister(String deviceId, String deviceBrand,String mobile) {
User user = userDao.findByMobile(mobile);
if(null==user) {
user=new User();
user.setId(String.valueOf(idWorker.nextId()));
user.setMobile(mobile);
user.setNickname("雀乐"+getRandomNickName());
user.setSex(Gender.Male.getCode());
user.setAvatar("default");
user.setPersonality("无签名");
user.setNickname("雀乐"+NickNameUtil.getRandomNickName());
//user.setDeviceId(deviceId);
//user.setDeviceBrand(deviceBrand);
userDao.save(user);
}
UserRespDTO userVO=new UserRespDTO();
BeanUtils.copyProperties(user, userVO);
String token = jwtUtil.createJWT(user.getId(),user.getMobile(),"user");
userVO.setToken(token);
return userVO;
}
// else {
// User updateUser = new User();
// updateUser.setLastdate(new Date());
// updateUser.setDeviceId(deviceId);
// updateUser.setDeviceBrand(deviceBrand);
// userDao.updateById(updateUser,user.getId());
// }
return user;
}
public User findByMobile(String mobile) {
User user = userDao.findByMobile(mobile);
return user;
}
public UserRespDTO touristLogin() {
UserRespDTO userVO=new UserRespDTO();
userVO.setId(String.valueOf(idWorker.nextId()));
userVO.setNickname("游客"+getRandomNickName());
userVO.setSex(Gender.Male.getCode());
userVO.setAvatar("default");
userVO.setPersonality("无签名");
String token = jwtUtil.createJWT(userVO.getId(),"tourist","tourist");
userVO.setToken(token);
return userVO;
}
private String getRandomNickName() {
String rawNickName=RandomSource.personInfoSource().randomChineseNickName(3);
if(rawNickName.length()>3) {
return rawNickName.substring(0, 3);
}
return rawNickName;
}
}

@ -0,0 +1,13 @@
package com.luoo.user.util;
import com.apifan.common.random.RandomSource;
public class NickNameUtil {
public static String getRandomNickName() {
String rawNickName=RandomSource.personInfoSource().randomChineseNickName(3);
if(rawNickName.length()>3) {
return rawNickName.substring(0, 3);
}
return rawNickName;
}
}

@ -0,0 +1,14 @@
package com.luoo.user.util;
public class StringTools {
public static boolean isEmpty(String str) {
if (null == str || "".equals(str) || "null".equals(str) || "\u0000".equals(str)) {
return true;
} else if ("".equals(str.trim())) {
return true;
}
return false;
}
}

@ -0,0 +1,29 @@
package com.luoo.user.util;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import com.luoo.user.enums.VerifyRegexEnum;
/**
* @ClassName VerifyUtils
* @Description TODO
* @Author Administrator
* @Date 2023/8/30 21:59
*/
public class VerifyUtils {
public static boolean verify(String regex,String value){
if(StringTools.isEmpty(value)){
return false;
}
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(value);
return matcher.matches();
}
public static boolean verify(VerifyRegexEnum regexEnum,String value){
return verify(regexEnum.getRegex(),value);
}
}
Loading…
Cancel
Save