kongdeqiang
2023-07-10 96f927cb94eec4a91df60973d4052cb812856e13
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package com.wgcloud.filter;
 
 
import com.wgcloud.config.CommonConfig;
import com.wgcloud.entity.AccountInfo;
import com.wgcloud.util.staticvar.StaticKeys;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
 
import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
 
/**
 * http请求过滤器,拦截不是从路由过来的请求
 *
 * @author wgcloud
 */
@WebFilter(filterName = "authRestFilter", urlPatterns = {"/*"})
public class AuthRestFilter implements Filter {
 
    static Logger log = LoggerFactory.getLogger(AuthRestFilter.class);
 
    @Autowired
    CommonConfig commonConfig;
 
    //静态资源和不需要登录的URL
    String[] static_resource = {"/fileSafe/agentList", "/shellInfo/agentList", "/shellInfo/shellCallback", "/fileWarnInfo/agentStateListList", "/fileWarnInfo/agentList", "/heathMonitor/agentList", "/dbTable/agentList",
            "/systemInfoOpen/", "/systemInfo/agentList", "/agentLogGo/minTask", "/agentGo/minTask", "/agentDiskGo/minTask", "/dceInfo/agentList",
            "/login/toLogin", "/login/login", "/appInfo/agentList", "/dockerInfo/agentList", "/portInfo/agentList", "/license/",
            "/static/", "/resources/", "/log/agentList", "/customInfo/agentList", "/agentCustomGo/minTask", "/dbInfo/agentList", "/agentDbTableGo/minTask",
            "/agentHeathMonitorGo/minTask", "/agentDceInfoGo/minTask", "/agentSnmpInfoGo/minTask", "/snmpInfo/agentList","/api/"};
 
 
    //公众看板URL
    String[] dash_views = {"/dash/main", "/systemInfo/systemInfoList", "/systemInfo/systemInfoListAjax", "/systemInfo/detail", "/systemInfo/chart", "/warnInfo/warnCountAjax"};
 
    //大屏URL
    String[] daping_views = {"/daping/index", "/dapingNew/index"};
 
    //只读账号不能进行的操作
    String[] guest_no_views = {"/del", "/save", "/edit", "/editBatch"};
 
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        final HttpServletResponse response = (HttpServletResponse) servletResponse;
        final HttpServletRequest request = (HttpServletRequest) servletRequest;
        final HttpSession session = request.getSession();
        AccountInfo accountInfo = (AccountInfo) session.getAttribute(StaticKeys.LOGIN_KEY);
        String uri = request.getRequestURI();
        log.debug("uri----" + uri);
        String servletPath = request.getServletPath();
        log.debug("servletPath----" + servletPath);
        menuActive(session, uri);
 
        //已登陆,跳转到index begin
        if (accountInfo != null && (servletPath.startsWith("/login/toLogin") || uri.endsWith("tssw/") || uri.endsWith("tssw"))) {
            response.sendRedirect("/tssw/dash/main");
            return;
        }
        //已登陆,跳转到index end
 
        //只读账号已登陆,校验是否是非法操作 begin
        //是非法操作直接跳转到错误页面
        if (accountInfo != null && StaticKeys.ROLE_GUEST.equals(accountInfo.getRole())) {
            for (String ss : guest_no_views) {
                if (servletPath.endsWith(ss)) {
                    response.sendRedirect("/tssw/common/error/guestError");
                    return;
                }
            }
        }
        //只读账号已登陆,校验是否是非法操作 end
 
        //静态资源过滤 begin
        for (String ss : static_resource) {
            if (servletPath.startsWith(ss)) {
                filterChain.doFilter(servletRequest, servletResponse);
                return;
            }
        }
        //静态资源过滤 end
 
        //公众看板处理 begin
        if (accountInfo == null) {
            for (String ss : dash_views) {
                if (servletPath.startsWith(ss) && StaticKeys.TRUE_VAL.equals(commonConfig.getDashView()) && request.getParameter(StaticKeys.DASH_VIEW_ACCOUNT) != null) {
                    filterChain.doFilter(servletRequest, servletResponse);
                    return;
                }
            }
        }
        //公众看板处理 end
 
        //大屏看板处理 begin
        if (accountInfo == null) {
            for (String ss : daping_views) {
                if (servletPath.startsWith(ss) && StaticKeys.TRUE_VAL.equals(commonConfig.getDapingView())) {
                    filterChain.doFilter(servletRequest, servletResponse);
                    return;
                }
            }
        }
        //大屏看板处理 end
 
        //未登录跳转到登陆页面 begin
        if (accountInfo == null) {
            response.sendRedirect("/tssw/login/toLogin");
            return;
        }
        //未登录跳转到登陆页面 end
 
        filterChain.doFilter(servletRequest, servletResponse);
    }
 
 
    /**
     * 添加菜单标识
     *
     * @param session
     * @param uri
     */
    public void menuActive(HttpSession session, String uri) {
        if (uri.indexOf("/log/") > -1) {
            session.setAttribute("menuActive", "21");
            return;
        }
        if (uri.indexOf("/dash/main") > -1) {
            session.setAttribute("menuActive", "01");
            return;
        }
        if (uri.indexOf("/systemInfo/systemInfoList") > -1 || uri.indexOf("/systemInfo/detail") > -1 || uri.indexOf("/systemInfo/chart") > -1 || uri.indexOf("/dash/hostDraw") > -1) {
            session.setAttribute("menuActive", "12");
            return;
        }
        if (uri.indexOf("/appInfo") > -1) {
            session.setAttribute("menuActive", "13");
            return;
        }
        if (uri.indexOf("/dockerInfo") > -1) {
            session.setAttribute("menuActive", "14");
            return;
        }
        if (uri.indexOf("/portInfo") > -1) {
            session.setAttribute("menuActive", "15");
            return;
        }
        if (uri.indexOf("/fileWarnInfo") > -1) {
            session.setAttribute("menuActive", "16");
            return;
        }
        if (uri.indexOf("/fileSafe") > -1) {
            session.setAttribute("menuActive", "17");
            return;
        }
        if (uri.indexOf("/customInfo") > -1) {
            session.setAttribute("menuActive", "18");
            return;
        }
        if (uri.indexOf("/mailset") > -1) {
            session.setAttribute("menuActive", "22");
            return;
        }
        if (uri.indexOf("/shellInfo") > -1) {
            session.setAttribute("menuActive", "23");
            return;
        }
        if (uri.indexOf("/hostGroup") > -1) {
            session.setAttribute("menuActive", "24");
            return;
        }
        if (uri.indexOf("/accountInfo") > -1) {
            session.setAttribute("menuActive", "25");
            return;
        }
        if (uri.indexOf("/dbInfo") > -1) {
            session.setAttribute("menuActive", "41");
            return;
        }
        if (uri.indexOf("/dbTable") > -1) {
            session.setAttribute("menuActive", "42");
            return;
        }
        if (uri.indexOf("/heathMonitor") > -1) {
            session.setAttribute("menuActive", "51");
            return;
        }
        if (uri.indexOf("/dceInfo") > -1) {
            session.setAttribute("menuActive", "61");
            return;
        }
        if (uri.indexOf("/snmpInfo") > -1) {
            session.setAttribute("menuActive", "62");
            return;
        }
        if (uri.indexOf("/tuopu/tuopuListHost") > -1) {
            session.setAttribute("menuActive", "71");
            return;
        }
        if (uri.indexOf("/tuopu/tuopuListSt") > -1) {
            session.setAttribute("menuActive", "72");
            return;
        }
        if (uri.indexOf("/equipment") > -1) {
            session.setAttribute("menuActive", "81");
            return;
        }
        if (uri.indexOf("/equipment") > -1) {
            session.setAttribute("menuActive", "81");
            return;
        }
        if (uri.indexOf("/inspectionTask") > -1) {
            session.setAttribute("menuActive", "c1");
            return;
        }
        if (uri.indexOf("/failurelogging") > -1) {
            session.setAttribute("menuActive", "c2");
            return;
        }
        session.setAttribute("menuActive", "11");
        return;
 
    }
 
}