Administrator
2018-11-09 603203f018fa5d105070574bd3383c8fe4d72bb0
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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package com.yeshi.fanli.aspect;
 
import java.io.IOException;
import java.lang.reflect.Method;
 
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
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 com.yeshi.fanli.entity.common.AdminUser;
import com.yeshi.fanli.util.Constant;
import com.yeshi.fanli.util.StringUtil;
import com.yeshi.fanli.util.annotation.RequestNoLogin;
import org.yeshi.utils.JsonUtil;
 
@Component
@Aspect
public class LoginAspect {
    
//    @Around("execution(public * com.yeshi.fanli.controller.admin.*.*(..))")
    public Object verifyLoginState(ProceedingJoinPoint joinPoint) throws IOException {
        
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method targetMethod = methodSignature.getMethod();
        
        ServletRequestAttributes servletContainer = (ServletRequestAttributes)RequestContextHolder.getRequestAttributes();
        
        HttpServletRequest request = servletContainer.getRequest();
        
        
        AdminUser admin = (AdminUser) request.getSession().getAttribute(Constant.SESSION_ADMIN);
        
        if(admin==null){
            Method realMethod=null;
            try {
                realMethod = joinPoint.getTarget().getClass().getDeclaredMethod(joinPoint.getSignature().getName(),
                        targetMethod.getParameterTypes());
            } catch (NoSuchMethodException e) {
                e.printStackTrace();
            } catch (SecurityException e) {
                e.printStackTrace();
            }
 
            //判断是否有忽略验证的注解
            if (realMethod==null||!realMethod.isAnnotationPresent(RequestNoLogin.class)) 
            {
                HttpServletResponse response = servletContainer.getResponse();
                String callback = request.getParameter("callback");
                if (StringUtil.isNullOrEmpty(callback)) {
                    response.getWriter().print(JsonUtil.loadFalseResult(1, "请先登录"));
                } else {
                    response.getWriter().print(callback + "(" + JsonUtil.loadFalseResult(1, "请先登录") + ")");
                }
                
                return null;
            }
        }
        
        Object[] args = joinPoint.getArgs();
        Object obj = null;
        
        try {
            obj = joinPoint.proceed(args);
        } catch (Throwable e) {
            e.printStackTrace();
        }
        
        return obj;
    }
    
 
 
}