@Override
public void doFilter(ServletRequest request, ServletResponse response,
FilterChain chain) throws IOException, ServletException {
HttpServletRequest httpServletRequest = (HttpServletRequest) request;
String requestURI = httpServletRequest.getRequestURI();
if (requestURI.startsWith("/e/welink") ||
requestURI.startsWith("/e/test/welink") ||
requestURI.startsWith("/e/approve/waitApprove") ||
requestURI.startsWith("/e/page/applicationPage") ||
requestURI.startsWith("/e/ows/user/listRole") ||
requestURI.endsWith(".ttf") ||
requestURI.endsWith(".woff") ||
requestURI.endsWith(".eot") ||
requestURI.endsWith(".woff2") ||
requestURI.endsWith(".css") ||
requestURI.endsWith(".js") ||
requestURI.endsWith(".png") ||
requestURI.endsWith(".jpg") ||
requestURI.endsWith(".wav") ||
requestURI.endsWith(".xlsx") ||
requestURI.endsWith(".svg") ||
requestURI.startsWith("/e/api/csp/") ||
requestURI.endsWith("/e/api/important_contact/all") ||
requestURI.contains("getRepOffAndIndustry") ||
requestURI.contains("getAllQcLine")
) {
chain.doFilter(request, response);
return;
}
if (requestURI.indexOf("/e/page") != -1) {
if (isMSBrowser(httpServletRequest)) {
response.setCharacterEncoding("gbk");
PrintWriter out = response.getWriter();
out.println("<div>本系統不支援 IE EDGE 等瀏覽器,推薦使用 Google Chrome瀏覽器,謝謝。</div>");
return;
}
}
HttpSession session = httpServletRequest.getSession();
UserInfoBean uiBean = (UserInfoBean) session
.getAttribute(SsoConstants.SESSION_USER_INFO_KEY);
if (uiBean == null) {
if (CommonUtil.isAjax(httpServletRequest)) {
Map<String, String> map = new HashMap<>();
map.put("code", "401");
response.setCharacterEncoding("utf-8");
response.getWriter().print(JSONObject.toJSON(map));
return;
}
try {
SsoUtil.loginAndRedirect2AppCurrentURL((HttpServletRequest) request,
(HttpServletResponse) response);
return;
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
User token = null;
try (Jedis jedis = jedisPool.getResource()) {
String redisKey = "userInfo_" + uiBean.getUid();
String tokenStr = jedis.get(redisKey);
if (StringUtils.isNotBlank(tokenStr)) {
token = JSON.parseObject(tokenStr, User.class);
}
if (token == null) {
Result r = userService.login((HttpServletRequest) request,
(HttpServletResponse) response);
token = (User) r.getData();
if ("N".equals(token.getIsVaild())) {
if (CommonUtil.isAjax(httpServletRequest)) {
throw new BadRequestException("此賬號已被禁用,如需開通,請聯絡系統管理員 王超偉 w00509928 !");
} else {
response.setCharacterEncoding("gbk");
response.getWriter().println("此賬號已被禁用,如需開通,請聯絡系統管理員 王超偉 w00509928 !");
return;
}
}
if (token.getIsExists()) {
Boolean redirect2ApplicationPage = approveLoginFilter.redirectWithUserExist(token);
if (redirect2ApplicationPage) {
((HttpServletResponse) response).sendRedirect(((HttpServletRequest) request).getContextPath() + "/page/applicationPage?agentId=" + token.getUserAgentId());
return;
}
} else {
Map<String, Object> newUserMap = approveLoginFilter.getNewUserByOldAgentId(token);
if (ObjectUtils.isEmpty(newUserMap)) {
approveLoginFilter.redirectWithUserNotExist(token);
((HttpServletResponse) response).sendRedirect(((HttpServletRequest) request).getContextPath() + "/page/applicationPage?agentId=" + token.getUserAgentId());
return;
} else {
User newUser = new User();
newUser.setId((String) newUserMap.get("ID"));
newUser.setUserAgentId((String) newUserMap.get("USER_AGENTID"));
newUser.setUserEmail((String) newUserMap.get("USER_EMAIL"));
newUser.setUserPhono((String) newUserMap.get("USER_PHONE"));
newUser.setCreateTime((Long) newUserMap.get("CREATE_DATE"));
newUser.setRoleId((String) newUserMap.get("ROLE_ID"));
newUser.setQc_line((String) newUserMap.get("QC_LINE"));
newUser.setRep_off((String) newUserMap.get("REP_OFF"));
Boolean redirect2ApplicationPage = approveLoginFilter.redirectWithUserExist(newUser);
if (redirect2ApplicationPage) {
((HttpServletResponse) response).sendRedirect(((HttpServletRequest) request).getContextPath() + "/page/applicationPage?agentId=" + token.getUserAgentId());
return;
} else {
Result loginResult = userService.login((HttpServletRequest) request, (HttpServletResponse) response);
token = (User) loginResult.getData();
}
}
}
userService.setRectRole(token);
userService.loadUserResource(token);
logUtils.log("登入", null, token.getUserAgentId(), token.getId());
dao.commit();
jedis.set(redisKey, JSON.toJSONString(token));
log.info("從資料庫中載入使用者資訊");
}
jedis.expire(redisKey, EXP_TIME);
}
Method m = AppContext.class.getDeclaredMethod("setCurrentUser", User.class);
m.setAccessible(true);
m.invoke(null, token);
chain.doFilter(request, response);
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
} finally {
AppContext.clearAll();
try {
dao.close();
} catch (Exception e) {
e.printStackTrace();
log.error(e.getMessage(), e);
}
}
}
}