UserCenterHanlerInterceptor.java
3.7 KB
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package com.tianbo.analysis.intercept;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.tianbo.analysis.feign.UserCenterAPI;
import com.tianbo.analysis.model.ResultJson;
import com.tianbo.analysis.thread.SessionUserContext;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;
import org.springframework.web.servlet.HandlerInterceptor;
import javax.annotation.PostConstruct;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@Slf4j
@Component
public class UserCenterHanlerInterceptor implements HandlerInterceptor {
private static UserCenterHanlerInterceptor _THIS;
@Autowired
private Environment environment;
@Autowired
private UserCenterAPI userCenterAPI;
@PostConstruct
public void init(){
_THIS = this;
}
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
String serviceName = _THIS.environment.getProperty("spring.application.name");
String permPath = "/" + serviceName + request.getRequestURI();
log.info("path = {}, serviceName= {}",request.getRequestURI(),serviceName);
AntPathMatcher pathMatcher = new AntPathMatcher();
boolean match = pathMatcher.match(permPath, request.getRequestURI());
String authHeader = request.getHeader("Authorization");
if (authHeader != null && authHeader.startsWith("Bearer ")) {
ResultJson resultJson = _THIS.userCenterAPI.getDataPerm(permPath, authHeader);
if (resultJson.getData()!=null){
}
JSONObject user = (JSONObject) JSON.toJSON(resultJson.getData());
log.info(resultJson.toString());
}
/**
* 根据访问接口地址和token信息获取用户权限信息
* 参数为request 中的接口地址和的 auth 的token信息
* 样例实体类如下:
*/
String userDataPermJsonDemoStr = "{\n" +
"\t\"username\":\"nmms\",\n" +
"\t\"userId\": \"80\",\n" +
"\t\"userDataPerm\":{\n" +
"\t\t\"rowCondition\": \"usr\",\n" +
"\t\t\"rowDataPermConditions\":[\n" +
"\t\t\t{\n" +
"\t\t\t\t\"colName\": \"USERNAME\"\n" +
"\t\t\t},\n" +
"\t\t\t{\n" +
"\t\t\t\t\"colName\": \"BILLNO\"\n" +
"\t\t\t}\n" +
"\t\t],\n" +
"\t}\t\n" +
"}";
JSONObject userInfo = JSONObject.parseObject(userDataPermJsonDemoStr);
JSONObject userDataPerm = userInfo.getJSONObject("userDataPerm");
String rowCondition = userDataPerm.getString("rowCondition");
JSONArray rowDataPermConditions = userDataPerm.getJSONArray("rowDataPermConditions");
JSONObject userDataPermModel = new JSONObject();
userDataPermModel.put("colName","USERNAME");
//获取token中的user的值
userDataPermModel.put("colValue","nmms");
// JSONObject userDataPermModel1 = new JSONObject();
// userDataPermModel1.put("colName","BILLNO");
// userDataPermModel1.put("colValue","78464771722");
JSONArray userDataPermArray = new JSONArray();
userDataPermArray.add(userDataPermModel);
// userDataPermArray.add(userDataPermModel1);
SessionUserContext.setSessionUser(userDataPermArray);
log.info("进入HTTPServletRequest 拦截器");
return true;
}
}