|
| 1 | +package SpringChat.configurations; |
| 2 | + |
| 3 | +import SpringChat.models.SessionModel; |
| 4 | +import SpringChat.models.UserModel; |
| 5 | +import org.springframework.beans.factory.annotation.Autowired; |
| 6 | +import org.springframework.http.MediaType; |
| 7 | +import org.springframework.scheduling.annotation.Scheduled; |
| 8 | +import org.springframework.stereotype.Component; |
| 9 | +import org.springframework.web.servlet.HandlerInterceptor; |
| 10 | +import org.springframework.web.servlet.ModelAndView; |
| 11 | +import SpringChat.SpringChat; |
| 12 | +import SpringChat.models.FileInfoModel; |
| 13 | +import SpringChat.storages.FileInfoStorage; |
| 14 | +import SpringChat.storages.SessionStorage; |
| 15 | +import SpringChat.storages.UserStorage; |
| 16 | +import SpringChat.utils.Utils1; |
| 17 | + |
| 18 | +import javax.servlet.http.Cookie; |
| 19 | +import javax.servlet.http.HttpServletRequest; |
| 20 | +import javax.servlet.http.HttpServletResponse; |
| 21 | +import javax.servlet.http.HttpSession; |
| 22 | +import java.io.File; |
| 23 | +import java.io.UnsupportedEncodingException; |
| 24 | +import java.util.*; |
| 25 | + |
| 26 | +@Component |
| 27 | +public class HttpInterceptor implements HandlerInterceptor { |
| 28 | + |
| 29 | + @Autowired |
| 30 | + private SessionStorage sessionStorage; |
| 31 | + |
| 32 | + @Autowired |
| 33 | + private UserStorage userStorage; |
| 34 | + |
| 35 | + @Autowired |
| 36 | + private Utils1 byteUtils; |
| 37 | + |
| 38 | + @Autowired |
| 39 | + private FileInfoStorage fileInfoStorage; |
| 40 | + |
| 41 | + private Set<String> allowedUrls = new HashSet<>(); |
| 42 | + private Set<String> allowedFiles = new HashSet<>(); |
| 43 | + private Set<String> htmlFiles = new HashSet<>(); |
| 44 | + |
| 45 | + public HttpInterceptor() { |
| 46 | + allowedUrls.add("/"); |
| 47 | + allowedUrls.add("/home"); |
| 48 | + allowedUrls.add("/index"); |
| 49 | + allowedUrls.add("/login-helper"); |
| 50 | + allowedUrls.add("/signup"); |
| 51 | + allowedUrls.add("/signup-helper"); |
| 52 | + |
| 53 | + for (String filename : Objects.requireNonNull(new File(SpringChat.IMAGE_RESOURCE_PATH).list())) { |
| 54 | + allowedFiles.add("/" + filename); |
| 55 | + } |
| 56 | + for (String filename : Objects.requireNonNull(new File(SpringChat.STYLE_RESOURCE_PATH).list())) { |
| 57 | + allowedFiles.add("/" + filename); |
| 58 | + } |
| 59 | + for (String filename : Objects.requireNonNull(new File(SpringChat.SCRIPT_RESOURCE_PATH).list())) { |
| 60 | + allowedFiles.add("/" + filename); |
| 61 | + } |
| 62 | + for (String filename : Objects.requireNonNull(new File(SpringChat.MISC_RESOURCE_PATH).list())) { |
| 63 | + allowedFiles.add("/" + filename); |
| 64 | + } |
| 65 | + for (String filename : Objects.requireNonNull(new File(SpringChat.PAGE_RESOURCE_PATH).list())) { |
| 66 | + htmlFiles.add("/" + filename); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private void putHeaderParams(Map<String, String> params, UserModel userModel) { |
| 71 | + if (userModel != null) { |
| 72 | + params.put("headerAccountText", userModel.getFirstname() + " " + userModel.getLastname()); |
| 73 | + params.put("headerAccountAction", ""); |
| 74 | + params.put("headerAccountClasses", "Dropdown"); |
| 75 | + params.put("HeaderUserCaretSvgDisplay", "inline-block"); |
| 76 | + } else { |
| 77 | + params.put("headerAccountText", "Sign In / Sign Up"); |
| 78 | + params.put("headerAccountAction", "onclick='location.href=\"/userModel\"'"); |
| 79 | + params.put("headerAccountClasses", ""); |
| 80 | + params.put("HeaderUserCaretSvgDisplay", "none"); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + private Properties getPropertiesFromCookies(Cookie cookie[]) { |
| 85 | + if (cookie == null) { |
| 86 | + return null; |
| 87 | + } |
| 88 | + Properties cookies = new Properties(); |
| 89 | + for (Cookie c : cookie) { |
| 90 | + cookies.put(c.getName(), c.getValue()); |
| 91 | + } |
| 92 | + return cookies; |
| 93 | + } |
| 94 | + |
| 95 | + public void loginWithCookies(Properties cookies, SessionModel sessionModel) { |
| 96 | + if (cookies != null) { |
| 97 | + if (cookies.containsKey("username") && cookies.containsKey("password")) { |
| 98 | + UserModel dbUserModel = userStorage.findByUsername("" + cookies.get("username")); |
| 99 | + if ((dbUserModel != null) && dbUserModel.getPassword().equals(cookies.get("password"))) { |
| 100 | + sessionModel.setUserModel(dbUserModel); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + private void loginWithBasicAuth(HttpServletRequest request, HttpSession httpSession, SessionModel sessionModel) throws UnsupportedEncodingException { |
| 107 | + String auth = request.getHeader("Authorization"); |
| 108 | + if (auth != null) { |
| 109 | + String[] split = auth.split(" "); |
| 110 | + if ("basic".equals(split[0].toLowerCase())) { |
| 111 | + String base64 = split[1]; |
| 112 | + String cred = new String(Base64.getDecoder().decode(base64), "UTF-8"); |
| 113 | + String username = cred.substring(0, cred.indexOf(":")); |
| 114 | + String password = cred.substring(cred.indexOf(":") + 1, cred.length()); |
| 115 | + UserModel dbUserModel = userStorage.findByUsername(username); |
| 116 | + if ((dbUserModel != null) && dbUserModel.getPassword().equals(byteUtils.hash(password))) { |
| 117 | + sessionModel.setUserModel(dbUserModel); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + @Scheduled(fixedRate = 3600000, initialDelay = 3600000) |
| 124 | + public void clearExpiredSessions() { |
| 125 | + List<SessionModel> expired = sessionStorage.findExpired(System.currentTimeMillis() - 3600000); |
| 126 | + sessionStorage.deleteAll(expired); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { |
| 131 | + String uri = request.getRequestURI(); |
| 132 | + HttpSession httpSession = request.getSession(); |
| 133 | + |
| 134 | + SessionModel sessionModel = sessionStorage.findById(httpSession.getId()).orElse(null); |
| 135 | + if (sessionModel == null) { |
| 136 | + sessionModel = new SessionModel(httpSession.getId(), null, System.currentTimeMillis()); |
| 137 | + } |
| 138 | + sessionModel.setLastModified(System.currentTimeMillis()); |
| 139 | + sessionStorage.save(sessionModel); |
| 140 | + |
| 141 | + loginWithBasicAuth(request, httpSession, sessionModel); |
| 142 | + loginWithCookies(getPropertiesFromCookies(request.getCookies()), sessionModel); |
| 143 | + |
| 144 | + if (allowedFiles.contains(uri)) { |
| 145 | + String uriLC = uri.toLowerCase(); |
| 146 | + if (uriLC.endsWith(".svg")) { |
| 147 | + response.setContentType("image/svg+xml"); |
| 148 | + response.getOutputStream().write(byteUtils.readBytes(new File((SpringChat.IMAGE_RESOURCE_PATH + "/" + uri)), false)); |
| 149 | + } else if (uriLC.endsWith(".css")) { |
| 150 | + response.setContentType("text/css"); |
| 151 | + response.getOutputStream().write(byteUtils.readBytes(new File((SpringChat.STYLE_RESOURCE_PATH + "/" + uri)), false)); |
| 152 | + } else if (uriLC.endsWith(".json")) { |
| 153 | + response.setContentType("application/json"); |
| 154 | + response.getOutputStream().write(byteUtils.readBytes(new File((SpringChat.MISC_RESOURCE_PATH + "/" + uri)), false)); |
| 155 | + } else if (uriLC.endsWith(".js")) { |
| 156 | + response.setContentType("text/javascript"); |
| 157 | + response.getOutputStream().write(byteUtils.readBytes(new File((SpringChat.SCRIPT_RESOURCE_PATH + "/" + uri)), false)); |
| 158 | + } else if (uriLC.endsWith(".png")) { |
| 159 | + response.setContentType("image/png"); |
| 160 | + response.getOutputStream().write(byteUtils.readBytes(new File((SpringChat.IMAGE_RESOURCE_PATH + "/" + uri)), false)); |
| 161 | + } else if (uriLC.endsWith(".ico")) { |
| 162 | + response.setContentType("image/x-icon"); |
| 163 | + response.getOutputStream().write(byteUtils.readBytes(new File((SpringChat.IMAGE_RESOURCE_PATH + "/" + uri)), false)); |
| 164 | + } |
| 165 | + response.setStatus(HttpServletResponse.SC_OK); |
| 166 | + return false; |
| 167 | + } |
| 168 | + |
| 169 | + if (uri.startsWith("/image-file-preview/")) { |
| 170 | + String fileInfoId = uri.substring(uri.indexOf('/', 1) + 1); |
| 171 | + FileInfoModel info = fileInfoStorage.findById(UUID.fromString(fileInfoId)).get(); |
| 172 | + MediaType mediaType; |
| 173 | + byte fileData[]; |
| 174 | + if (info.getImgPrevFileDataId() == null) { |
| 175 | + mediaType = byteUtils.getMediaType(info.getName()); |
| 176 | + fileData = byteUtils.readBytes(new File(SpringChat.UPLOAD_PATH + "/" + info.getFileDataId()), true); |
| 177 | + } else { |
| 178 | + mediaType = byteUtils.getMediaType("a." + SpringChat.IMG_THUMBNAIL_FORMAT); |
| 179 | + fileData = byteUtils.readBytes(new File(SpringChat.UPLOAD_PATH + "/" + info.getImgPrevFileDataId()), true); |
| 180 | + } |
| 181 | + response.setContentType("" + mediaType); |
| 182 | + response.getOutputStream().write(fileData); |
| 183 | + return false; |
| 184 | + } |
| 185 | + |
| 186 | + if (!allowedUrls.contains(uri)) { |
| 187 | + if ("/login".equals(uri)) { |
| 188 | + if (sessionModel.getUserModel() != null) { |
| 189 | + response.sendRedirect("/user"); |
| 190 | + return false; |
| 191 | + } |
| 192 | + } else { |
| 193 | + if (sessionModel.getUserModel() == null) { |
| 194 | + sessionModel.setRedirectedUri(uri); |
| 195 | + response.sendRedirect("/login"); |
| 196 | + return false; |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + |
| 201 | + if (uri.equals("/")) { |
| 202 | + uri = "/home"; |
| 203 | + } |
| 204 | + if (htmlFiles.contains(uri + ".html")) { |
| 205 | + Map<String, String> params = new HashMap<>(); |
| 206 | + putHeaderParams(params, sessionModel.getUserModel()); |
| 207 | + params.put("pageTitle", uri.substring(1)); |
| 208 | + if (uri.equals("/user")) { |
| 209 | + params.put("optionsSvg", |
| 210 | + "<img src=\"options.svg\" " |
| 211 | + + "id=\"OptionsSvg\" " |
| 212 | + + "class=\"Button ButtonTransPrimary\"" |
| 213 | + + "onclick='toggleSidebarDisplay()'/>"); |
| 214 | + } else { |
| 215 | + params.put("optionsSvg", ""); |
| 216 | + } |
| 217 | + String html = byteUtils.readPage(uri + ".html", params); |
| 218 | + response.setContentType("text/html"); |
| 219 | + response.getOutputStream().write(html.getBytes("UTF-8")); |
| 220 | + return false; |
| 221 | + } |
| 222 | + return true; |
| 223 | + } |
| 224 | + |
| 225 | + @Override |
| 226 | + public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception { |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception exception) throws Exception { |
| 231 | + } |
| 232 | +} |
0 commit comments