1.第一步,自定义注解
package com.drg.rule.comm.annotation;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
/** @Author:zhj @Description: @Date: 2019/4/25 11:26 @Version:1.0 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface CheckCurrentUser {}
2、定义切面
package com.drg.rule.comm.aspect;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.drg.rule.business.entity.DataMaintainConfirm;
import com.drg.rule.business.service.impl.DataMaintainConfirmServiceImpl;
import com.drg.rule.comm.BizException;
import com.drg.rule.comm.Constants;
import com.drg.rule.comm.ResponseCodeEnum;
import com.drg.rule.security.core.ThreadLocalContextHolder;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
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 javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Field;
/** @Author:zhj @Description: @Date: 2019/4/25 11:28 @Version:1.0 */
@Aspect
@Slf4j
@Component
public class CheckCurrentUserAspect {
@Autowired private DataMaintainConfirmServiceImpl dataMaintainConfirmService;
@Before(value = "@annotation(com.drg.rule.comm.annotation.CheckCurrentUser)")
public void deBefore(JoinPoint joinPoint) throws ClassNotFoundException {
ServletRequestAttributes requestAttributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
HttpServletRequest request = requestAttributes.getRequest();
//url
log.info("url=" + request.getRequestURI());
//获取拦截的方法名和类名
log.info(
"class="
+ joinPoint.getSignature().getDeclaringTypeName()
+ "and method name="
+ joinPoint.getSignature().getName());
//获取参数
Object[] arguments = joinPoint.getArgs();
//此方法在网上百度
String dataMaintainId = getFieldsValue(arguments[0], "dataMaintainId");
log.info("拦截到的dataMaintainId:" + dataMaintainId);
//根据id获取数据
QueryWrapper queryWrapper = new QueryWrapper();
queryWrapper.eq("is_valid", Constants.HAS_VALID);
queryWrapper.eq("id", dataMaintainId);
DataMaintainConfirm one = dataMaintainConfirmService.getOne(queryWrapper);
if (null == one) {
throw new BizException(ResponseCodeEnum.OPERATE_ERROR);
}
//获取操作人
String createdBy = one.getCreatedBy();
//获取当前登录用户
String userCode = ThreadLocalContextHolder.getContext().getUserInfo().getUserCode();
//如果创建人和当前登录用户为同一人,则抛出异常
if (userCode.equals(createdBy)) {
throw new BizException(ResponseCodeEnum.USER_CANNOT_SAME);
}
}
private static String[] types = {
"java.lang.Integer",
"java.lang.Double",
"java.lang.Float",
"java.lang.Long",
"java.lang.Short",
"java.lang.Byte",
"java.lang.Boolean",
"java.lang.Char",
"java.lang.String",
"int",
"double",
"long",
"short",
"byte",
"boolean",
"char",
"float"
};
public static String getParamValue(JoinPoint joinPoint) {
StringBuilder sb = new StringBuilder();
//获取所有的参数
Object[] args = joinPoint.getArgs();
for (int k = 0; k < args.length; k++) {
Object arg = args[k];
// 获取对象类型
String typeName = arg.getClass().getTypeName();
for (String t : types) {
//1 判断是否是基础类型
if (t.equals(typeName)) {
sb.append(arg + "; ");
} else {
//2 通过反射获取实体类属性
sb.append(getFieldsValue(arg));
}
}
}
return sb.toString();
}
//解析实体类,获取实体类中的属性
public static String getFieldsValue(Object obj) {
//通过反射获取所有的字段,getFileds()获取public的修饰的字段
//getDeclaredFields获取private protected public修饰的字段
Field[] fields = obj.getClass().getDeclaredFields();
String typeName = obj.getClass().getTypeName();
for (String t : types) {
if (t.equals(typeName)) {
return "";
}
}
StringBuilder sb = new StringBuilder();
sb.append("{");
for (Field f : fields) {
//在反射时能访问私有变量
f.setAccessible(true);
try {
for (String str : types) {
//这边会有问题,如果实体类里面继续包含实体类,这边就没法获取。
//其实,我们可以通递归的方式去处理实体类包含实体类的问题。
if (f.getType().getName().equals(str)) {
sb.append(f.getName() + " : " + f.get(obj) + ", ");
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
sb.append("}");
return sb.toString();
}
//解析实体类,获取实体类中的属性
public static String getFieldsValue(Object obj, String paramName) {
//通过反射获取所有的字段,getFileds()获取public的修饰的字段
//getDeclaredFields获取private protected public修饰的字段
Field[] fields = obj.getClass().getDeclaredFields();
String typeName = obj.getClass().getTypeName();
for (String t : types) {
if (t.equals(typeName)) {
return "";
}
}
String sb = "";
a:
for (Field f : fields) {
//在反射时能访问私有变量
f.setAccessible(true);
try {
for (String str : types) {
//这边会有问题,如果实体类里面继续包含实体类,这边就没法获取。
//其实,我们可以通递归的方式去处理实体类包含实体类的问题。
if (f.getType().getName().equals(str)) {
if (f.getName().equals(paramName)) {
sb = f.get(obj) + "";
break a;
}
break;
}
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
return sb;
}
}
3、在需要进行拦截的方法上加上自定义注解
@ApiOperation(value = "驳回数据修改接口", notes = "驳回数据修改接口")
@PostMapping("/rejectChange")
@CheckCurrentUser
public RMPResponse rejectChangeDataMaintainConfirm(
@RequestBody @Validated DataMaintainOperateDto dataMaintainOperateDto) {
iDataMaintainConfirmService.rejectChangeDataMaintainConfirm(dataMaintainOperateDto);
return RMPResponse.success();
}