zm
2020-05-18 a18bfacbf56b401f6e0fdae8710fbca4df8cff77
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
package com.changhong.epc.rely.api.tool;
 
import com.changhong.epc.constter.base.BaseConst;
import com.changhong.epc.constter.system.prop.ChangHongProperties;
import com.changhong.epc.rely.api.service.UserService;
import com.iemsoft.framework.cloud.core.thread.Keys;
import com.iemsoft.framework.cloud.core.thread.ThreadData;
import com.iemsoft.framework.cloud.core.tools.JSONTool;
import com.iemsoft.framework.cloud.core.tools.ObjectUtil;
import com.iemsoft.framework.cloud.core.tools.SpringUtil;
import com.iemsoft.framework.cloud.redis.service.CacheUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.math.NumberUtils;
 
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
 
import static com.iemsoft.framework.cloud.core.thread.Keys.USER_LANGUAGE;
 
@Slf4j
public class FilterTool implements BaseConst{
 
    private static final Map<String,Keys> COOKIE_KEY;
 
    public static final Map<FilterType, FilterInterFace> FILTER_TYPE;
 
 
    static{
        Map<String,Keys> cookieKey = new HashMap<>(6);
        // tenantId
        cookieKey.put("tenantId"    , TENANT_ID);
        // openId
        cookieKey.put("openId"        , OPEN_ID);
        // 公司id
        cookieKey.put("companyId"    , COMPANY_ID);
        // 公司主数据id
        cookieKey.put("companyMasterId", COMPANY_MASTER_ID);
        // token
        cookieKey.put("token"        , TOKEN);
        //userName
        cookieKey.put("userName"    , USER_NAME);
        //dataSourceId
        cookieKey.put("dataSourceId", DATA_SOURCE);
 
        cookieKey.put("Language"    ,  USER_LANGUAGE);
 
        cookieKey.put("permType"    ,  PERM_TYPE);
 
        COOKIE_KEY = Collections.unmodifiableMap(cookieKey);
 
        Map<FilterType, FilterInterFace> filter = new HashMap<>(3);
        /**
         * 对外
         */
        filter.put(FilterType.PUBLIC, request -> {
            log.debug("进入对外接口拦截");
            String data = request.getParameter("data");
            log.debug("对外接口传入参数为:{}", JSONTool.toJson(request.getParameterMap()));
            Map<String, Object> dataObj = JSONTool.toObj(data, Map.class);
 
            if(ObjectUtil.notEmpty(dataObj.get("tenantId"))) {
                String tenantId;
                ThreadData.set(TENANT_ID, tenantId = Objects.toString(dataObj.get("tenantId"), ""));
 
                if(ObjectUtil.notEmpty(dataObj.get("compId"))) {
                    ThreadData.set(COMPANY_ID, NumberUtils.createInteger(Objects.toString(dataObj.get("compId"), "0")));
//                    Tenant tenant =
//                            SpringUtil.getBean(CscApiService.class)
//                                    .getTenantAndCompany(
//                                            NumberUtils.createInteger(tenantId)
//                                            , company -> Objects.equals(company.getMasterCompanyId(), dataObj.get("compId").toString()));
//                    if(ObjectUtil.notEmpty(tenant.getCompanys()))
//                        ThreadData.set(COMPANY_ID, tenant.getCompanys().get(0).getId());
//                    ThreadData.set(COMPANY_MASTER_ID, NumberUtils.createInteger(Objects.toString(dataObj.get("compId"), "0")));
                }
            }
            ThreadData.set(Keys.TENANT_ID, ThreadData.get(TENANT_ID));
            log.debug("添加cookie数据:{}={}", Keys.TENANT_ID, ThreadData.get(Keys.TENANT_ID));
            ThreadData.set(Keys.USER_ID, ThreadData.get(OPEN_ID));
            log.debug("添加cookie数据:{}={}", Keys.USER_ID, ThreadData.get(Keys.USER_ID));
            ThreadData.set(Keys.COMPANY_ID, Objects.toString(ThreadData.get(COMPANY_ID)));
            log.debug("添加cookie数据:{}={}", Keys.COMPANY_ID, ThreadData.get(Keys.COMPANY_ID));
 
        });
 
        /**
         * 对内
         */
        filter.put(FilterType.PRIVATE, request -> {
            log.debug("进入对内接口拦截");
 
            Cookie[] cookie = request.getCookies();
            if (cookie != null) {
                for (Cookie cook : cookie) {
                    if (!ObjectUtil.empty(COOKIE_KEY.get(cook.getName()))) {
                        log.debug("Cookie解密前key为:"+cook.getName()+"--value:"+cook.getValue());
                        ThreadData.set(COOKIE_KEY.get(cook.getName()), Des1.desDecrypt(cook.getValue(),Des1.SECRETKEY));
                        log.debug("{}:添加cookie数据:{}={}", HttpContextTool.getRequestUIR(request), COOKIE_KEY.get(cook.getName()), cook.getValue());
                    }
                }
            }
            try {
                SpringUtil.getBean(UserService.class).getUserInfo();
            } catch (Exception e) {
                log.error(e.getMessage());
            }
            log.debug("解密后---------->");
            ThreadData.set(Keys.TENANT_ID, ThreadData.get(TENANT_ID));
            log.debug("添加cookie数据:{}={}", Keys.TENANT_ID, ThreadData.get(Keys.TENANT_ID));
            ThreadData.set(Keys.USER_ID, ThreadData.get(OPEN_ID));
            log.debug("添加cookie数据:{}={}", Keys.USER_ID, ThreadData.get(Keys.USER_ID));
            ThreadData.set(Keys.COMPANY_ID, Objects.toString(ThreadData.get(COMPANY_ID)));
            log.debug("添加cookie数据:{}={}", Keys.COMPANY_ID, ThreadData.get(Keys.COMPANY_ID));
            //setCookie超时时间
            if(ThreadData.get(TOKEN)!=null && !Objects.equals("",ThreadData.get(TOKEN))) {
                log.debug("添加token过期时间");
                String key = String.format("token_%s"
                        , ThreadData.get(OPEN_ID));
                log.debug("当前缓存中登陆token:" + key);
                String time = SpringUtil.getBean(ChangHongProperties.class).getTokenTimeOut();
                log.debug("当前超时时间:" + time);
                log.debug("当前token:" + ThreadData.get(TOKEN));
                CacheUtils.set(key, ThreadData.get(TOKEN), Long.parseLong(time));
            }
        });
        
        /**
         * 主数据ftp
         */
        filter.put(FilterType.MASTER, request -> {
           log.debug("进入主数据ftp接口拦截");
           //暂时没有拦截目标
           ThreadData.set(Keys.TENANT_ID, ThreadData.get(TENANT_ID));
           log.debug("添加cookie数据:{}={}", Keys.TENANT_ID, ThreadData.get(Keys.TENANT_ID));
           ThreadData.set(Keys.USER_ID, ThreadData.get(OPEN_ID));
           log.debug("添加cookie数据:{}={}", Keys.USER_ID, ThreadData.get(Keys.USER_ID));
           ThreadData.set(Keys.COMPANY_ID, Objects.toString(ThreadData.get(COMPANY_ID)));
           log.debug("添加cookie数据:{}={}", Keys.COMPANY_ID, ThreadData.get(Keys.COMPANY_ID));
        });
        FILTER_TYPE = Collections.unmodifiableMap(filter);
    }
 
    /**
     * 拦截类型
     */
    public enum FilterType{
        /**
         * 对外
         */
        PUBLIC,
        /**
         * 对内
         */
        PRIVATE,
        /**
         * 主数据ftp
         */
        MASTER,
    }
 
    /**
     * 拦截接口
     */
    @FunctionalInterface
    public interface FilterInterFace{
 
        void filter(HttpServletRequest request);
 
    }
 
}