admin
2021-12-09 f609ca35ee2946acd0ff04b7ac1aa61f75a2e4a1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
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();
    }
}