@Aspect @Order(10) public class RestControllerAspect { public static final Logger logger = LoggerFactory.getLogger(RestControllerAspect.class); @Resource private OfficeCommonService officeCommonService; @Pointcut("execution(public * com.sinosig.epassport.application.controller..*.*(..))") public void restful() { } 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"}; @Around("restful()") public Object execute(ProceedingJoinPoint pjp) throws Throwable { ResultModel rm = new ResultModel(); Object target = pjp.getTarget(); processingBefore(); if (target instanceof BeforeControllerHanding) { java.lang.reflect.Method method = Refs.getMethod(pjp.getTarget(), pjp.getSignature().getName()); if (method.getReturnType() == Void.TYPE) { logger.warn("return type is void, not wrap"); return pjp.proceed(); } StringBuilder sb = new StringBuilder(); //获取所有的参数 Object[] args = pjp.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)) { return sb.append(arg + "; "); } else { Map fieldsValue = (Map) getFieldsValue(arg); String sysNo = (String) fieldsValue.get("sysNo"); String sysPwd = (String) fieldsValue.get("sysPwd"); List<AccessControlEntity> sysNoAndPwd = officeCommonService.findSysNoAndPwd(sysNo, sysPwd); if (sysNo.isEmpty() || sysPwd.isEmpty()) { return new ResultModel(false, "", "子系统访问账号或密码不能为空"); } else if (sysNoAndPwd == null || sysNoAndPwd.size() <= 0) { return new ResultModel(false, "", "账号或密码错误"); } else { return pjp.proceed(); } } } } return null; } if (target instanceof RestControllerHandling) { try { java.lang.reflect.Method method = Refs.getMethod(pjp.getTarget(), pjp.getSignature().getName()); if (method.getReturnType() == Void.TYPE) { logger.warn("return type is void, not wrap"); return pjp.proceed(); } Object result = pjp.proceed(); rm.setResultData(result.toString()); rm.setSuccess(true); rm.setMessage(""); } catch (DaoException e) { rm.setMessage("数据访问错误"); logger.error(e.getMessage(), e); } catch (NoLoginException e) { rm.setMessage("用户未登录或登陆已失效"); logger.error(e.getMessage(), e); } catch (CaptchaErrorException e) { rm.setMessage("验证码错误"); logger.error(e.getMessage(), e); } catch (ValidationException e) { rm.setMessage(e.getMessage()); logger.error(e.getMessage(), e); } catch (Throwable e) { rm.setMessage("系统错误"); logger.error(e.getMessage(), e); } } else { return pjp.proceed(); } return rm; } private void processingBefore() { try { RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes(); if (requestAttributes instanceof ServletRequestAttributes) { HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest(); Page page = PageSelector.getPage(); page.reset(); String enabledPage = request.getParameter("enabledPage"); String pageSizePm = request.getParameter("pageSize"); String pageIndexPm = request.getParameter("pageIndex"); if ("0".equals(enabledPage) || "N".equalsIgnoreCase(enabledPage) || "false".equalsIgnoreCase(enabledPage)) { } else { if (!Strings.isEmpty(pageSizePm) && !Strings.isEmpty(pageIndexPm)) { int pageSize = Strings.toInteger(pageSizePm, 1); int pageIndex = Strings.toInteger(pageIndexPm, 10); page.setPaging(true); page.setPageSize(pageSize); page.setCurrentPage(pageIndex); } } } } catch (Exception e) { logger.error(e.getMessage(), e); } } //解析实体类,获取实体类中的属性 public static Object 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 ""; } } Map<Object, Object> map = new HashMap<>(); for (Field f : fields) { //在反射时能访问私有变量 f.setAccessible(true); try { for (String str : types) { //这边会有问题,如果实体类里面继续包含实体类,这边就没法获取。 //其实,我们可以通递归的方式去处理实体类包含实体类的问题。 if (f.getType().getName().equals(str)) { map.put(f.getName(), f.get(obj)); } } } catch (IllegalArgumentException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } } return map; } }