|
|
|
@ -1,10 +1,11 @@
|
|
|
|
|
package com.luoo.user.aspect;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.lang.reflect.Field;
|
|
|
|
|
import java.lang.reflect.Method;
|
|
|
|
|
import java.lang.reflect.Parameter;
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
|
|
|
|
import org.apache.commons.lang3.ArrayUtils;
|
|
|
|
|
import org.aspectj.lang.JoinPoint;
|
|
|
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
|
|
@ -12,81 +13,137 @@ import org.aspectj.lang.annotation.Before;
|
|
|
|
|
import org.aspectj.lang.reflect.MethodSignature;
|
|
|
|
|
import org.slf4j.Logger;
|
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
import org.springframework.stereotype.Component;
|
|
|
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
|
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
|
|
|
|
|
|
import com.luoo.user.annotation.GlobalInterceptor;
|
|
|
|
|
import com.luoo.user.annotation.VerifyParam;
|
|
|
|
|
import com.luoo.user.constants.Constants;
|
|
|
|
|
import com.luoo.user.util.VerifyUtils;
|
|
|
|
|
|
|
|
|
|
import api.StatusCode;
|
|
|
|
|
import dto.UserLoginDto;
|
|
|
|
|
import exception.BizException;
|
|
|
|
|
import util.JwtUtil;
|
|
|
|
|
import util.StringTools;
|
|
|
|
|
|
|
|
|
|
@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"};
|
|
|
|
|
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" };
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private JwtUtil jwtUtil;
|
|
|
|
|
|
|
|
|
|
@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) {
|
|
|
|
|
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);
|
|
|
|
|
/**
|
|
|
|
|
* 登录校验
|
|
|
|
|
*/
|
|
|
|
|
if (interceptor.checkLogin()) {
|
|
|
|
|
checkLogin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 管理用户登录校验
|
|
|
|
|
*/
|
|
|
|
|
if (interceptor.checkAdminLogin()) {
|
|
|
|
|
checkAdminLogin();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 校验参数
|
|
|
|
|
*/
|
|
|
|
|
if (interceptor.checkParam()) {
|
|
|
|
|
validateParams(method, arguments);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void checkAdminLogin() {
|
|
|
|
|
UserLoginDto userLoginDto = getUserLoginDtoFromToken();
|
|
|
|
|
if (userLoginDto == null) {
|
|
|
|
|
throw new BizException(StatusCode.UNAUTHORIZED);
|
|
|
|
|
}
|
|
|
|
|
if (!Constants.TOKEN_ROLE_ADMIN_USER.equals(userLoginDto.getRoles())) {
|
|
|
|
|
throw new BizException(StatusCode.FORBIDDEN);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void checkLogin() {
|
|
|
|
|
UserLoginDto userLoginDto = getUserLoginDtoFromToken();
|
|
|
|
|
if (userLoginDto == null) {
|
|
|
|
|
throw new BizException(StatusCode.UNAUTHORIZED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private UserLoginDto getUserLoginDtoFromToken() {
|
|
|
|
|
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
|
|
|
|
|
.getRequest();
|
|
|
|
|
String token = request.getHeader("token");
|
|
|
|
|
return jwtUtil.getUserLoginDto(token);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
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);
|
|
|
|
|
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) {
|
|
|
|
|
|
|
|
|
|
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) {
|
|
|
|
|
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);
|
|
|
|
|
Object resultValue = field.get(value);
|
|
|
|
|
checkValue(resultValue, fieldVerifyParam);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}catch(Exception e) {
|
|
|
|
|
logger.error(StatusCode.VALIDATE_FAILED.getMessage(),e.getMessage());
|
|
|
|
|
} 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()) {
|
|
|
|
|
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)) {
|
|
|
|
|
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))) {
|
|
|
|
|
if (!isEmpty && !StringTools.isEmpty(verifyParam.regex().getRegex())
|
|
|
|
|
&& !VerifyUtils.verify(verifyParam.regex(), String.valueOf(value))) {
|
|
|
|
|
throw new BizException(StatusCode.VALIDATE_FAILED);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|