1.add image check code for user login, 2. add aspect to validate the user login parameters 3.add deviceId/deviceBrand in login part
parent
83bf6b065a
commit
4ec0092e2a
@ -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());
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
}
|
||||
}
|
@ -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…
Reference in new issue