package com.ks.app.aop; import com.ks.app.service.inter.config.SystemConfigService; import com.ks.app.utils.ApiCodeConstant; import com.ks.app.utils.annotation.UserLogin; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.reflect.MethodSignature; import org.springframework.stereotype.Component; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; import org.yeshi.utils.JsonUtil; import org.yeshi.utils.SPELExpressionUtil; import org.yeshi.utils.StringUtil; import javax.annotation.Resource; import javax.servlet.http.HttpServletResponse; import java.lang.reflect.Method; @Component public class UserLoginValid { public static final String EDP = "execution(* com.yeshi.location.app.controller.client..*.*(..))"; @Resource private SystemConfigService systemConfigService; @Around(EDP) public Object around(ProceedingJoinPoint joinPoint) throws Throwable { Signature signature = joinPoint.getSignature(); MethodSignature methodSignature = (MethodSignature) signature; Method targetMethod = methodSignature.getMethod(); Method realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(joinPoint.getSignature().getName(), targetMethod.getParameterTypes()); if (realMethod.isAnnotationPresent(UserLogin.class)) { UserLogin userLogin = realMethod.getAnnotation(UserLogin.class); //EL表达式解析 String key = userLogin.uid(); String keyValue = SPELExpressionUtil.generateKeyBySpEL(key, joinPoint); if (StringUtil.isNullOrEmpty(keyValue)) { ServletRequestAttributes servletContainer = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); HttpServletResponse response = servletContainer.getResponse(); response.getWriter().print(JsonUtil.loadFalseResult(ApiCodeConstant.CODE_FAIL_NOT_LOGIN, "未登录")); return null; } else { return joinPoint.proceed(); } } return joinPoint.proceed(); } }