-
Notifications
You must be signed in to change notification settings - Fork 2
/
TokenInterceptor.java
83 lines (76 loc) · 3.14 KB
/
TokenInterceptor.java
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
83
package com.duodian.youhui.admin.config.anno.aop;
import com.duodian.youhui.admin.bean.ResponseBean;
import com.duodian.youhui.admin.config.ContextHolder;
import com.duodian.youhui.admin.config.anno.annotation.Token;
import com.duodian.youhui.admin.moudle.user.service.UserInfoService;
import com.duodian.youhui.admin.utils.AesUtils;
import com.duodian.youhui.enums.exception.ErrorCodeEnum;
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.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.Method;
/**
* @author HealerJean
*/
@Aspect
@Component
@Order(0)
public class TokenInterceptor {
@Resource
private UserInfoService userInfoService;
/**
* 先取method的Token,如果没有再去class的Token,如果有使用method的Token
* 从request获取token进行解密,获取userId和oid,如果解密失败,返回错误信息
* 把userId和oid放入当前上下文
*
* @param point
* @return
* @throws Throwable
*/
@Around("@annotation(com.duodian.youhui.admin.config.anno.annotation.Token) || @within(com.duodian.youhui.admin.config.anno.annotation.Token)")
protected Object invoke(ProceedingJoinPoint point) throws Throwable {
Token token = null;
Signature signature = point.getSignature();
if (signature instanceof MethodSignature) {
Method method = ((MethodSignature) signature).getMethod();
token = method.getAnnotation(Token.class);
}
if (token == null) {
token = point.getTarget().getClass().getAnnotation(Token.class);
}
if (token == null || !token.check()) {
return point.proceed();
}
HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
String tokenParam = request.getParameter("token");
// String tokenParam = request.getHeader("anno");
if (tokenParam == null) {
return ResponseBean.buildFailure(ErrorCodeEnum.token认证失败) ;
}
try {
String decrip = AesUtils.LoginDecrypt(tokenParam) ;
if (decrip == null) {
return ResponseBean.buildFailure(ErrorCodeEnum.token认证失败) ;
}
Long userId = Long.valueOf( decrip);
//检验redis中是否还存在,并比较相等
if (!userInfoService.checkAppToken(userId, tokenParam)) {
return ResponseBean.buildFailure(ErrorCodeEnum.token认证失败) ;
}
ContextHolder.setUserId(userId);
return point.proceed();
} catch (Exception e) {
throw e;
} finally {
ContextHolder.clearUserId();
}
}
}