@Override public void init(FilterConfig fc) throws ServletException { LOG.info("初始化性能过滤器(Initialize the filter performance)"); enabled = PropertyHolder.getBooleanProperty("monitor.performance"); if (enabled) { LOG.info("启用性能分析日志(Enable performance analyzing log)"); } else { LOG.info("禁用性能分析日志(Disable performance analyzing log)"); } }
private boolean filter(HttpServletRequest req) { String path = req.getRequestURI(); if (path.contains("/log/")) { LOG.info("路径包含/log/,不执行性能分析(/log/ in path, not execute performance analysis) " + path); return false; } if (path.contains("/monitor/")) { LOG.info("路径包含/monitor/,不执行性能分析(/log/ in path, not execute performance analysis) " + path); return false; } return true; }
@Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; long start = 0; if (enabled && filter(req)) { start = System.currentTimeMillis(); } chain.doFilter(request, response); if (enabled && filter(req)) { long end = System.currentTimeMillis(); User user = OnlineUserService.getUser(req.getSession().getId()); ProcessTime logger = new ProcessTime(); logger.setUsername(user.getUsername()); logger.setUserIP(req.getRemoteAddr()); try { logger.setServerIP(InetAddress.getLocalHost().getHostAddress()); } catch (UnknownHostException ex) { LOG.error("保存日志出错(Error in saving log)", ex); } logger.setAppName(SystemListener.getContextPath()); String resource = req.getRequestURI().replace(logger.getAppName(), ""); logger.setResource(resource); logger.setStartTime(new Date(start)); logger.setEndTime(new Date(end)); logger.setProcessTime(end - start); LogQueue.addLog(logger); } }
protected void initParameters(final FilterConfig fConfig) { failureUrl = PropertyHolder.getProperty("login.page") + "?state=checkCodeError"; if ("true".equals(PropertyHolder.getProperty("login.code"))) { log.info("启用登录验证码机制"); filter = true; } else { filter = false; log.info("禁用登录验证码机制"); } if (StringUtils.isNotBlank(fConfig.getInitParameter(PARAM_FILTER_PROCESSES_URL))) { filterProcessesUrl = fConfig.getInitParameter(PARAM_FILTER_PROCESSES_URL); } if (StringUtils.isNotBlank(fConfig.getInitParameter(PARAM_CAPTCHA_PARAMTER_NAME))) { captchaParamterName = fConfig.getInitParameter(PARAM_CAPTCHA_PARAMTER_NAME); } }
@Override public Object convertFromString(Map context, String[] values, Class toClass) { if (values[0] == null || values[0].trim().equals("")) { return 0; } try { return Integer.parseInt(values[0].trim()); } catch (Exception e) { LOG.info("字符串:" + values[0].trim() + "转换为数字失败"); } return 0; }
protected void genernateCaptchaImage( final HttpServletRequest request, final HttpServletResponse response) { ServletUtils.setDisableCacheHeader(response); response.setContentType("image/png"); ServletOutputStream out = null; try { out = response.getOutputStream(); String captchaId = request.getSession(true).getId(); BufferedImage challenge = (BufferedImage) captchaService.getChallengeForID(captchaId, request.getLocale()); // String writerNames[] = ImageIO.getWriterFormatNames(); ImageIO.write(challenge, "png", out); out.flush(); } catch (IOException | CaptchaServiceException e) { log.error("生成验证码出错", e); } finally { try { out.close(); } catch (IOException e) { log.error("生成验证码出错", e); } } }
@Override public void destroy() { LOG.info("销毁性能过滤器(Destroy the filter performance)"); }
/** 初始化系统安全拦截信息 */ @PostConstruct public void initSecurityConfigInfo() { String security = PropertyHolder.getProperty("security"); if (security == null || !"true".equals(security.trim())) { log.info("当前系统禁用安全机制"); return; } log.info("开始初始化权限子系统..."); LinkedHashMap<RequestKey, Collection<ConfigAttribute>> requestMap = new LinkedHashMap<>(); SecurityConfig manager = new SecurityConfig("ROLE_MANAGER"); SecurityConfig superManager = new SecurityConfig("ROLE_SUPERMANAGER"); Collection<ConfigAttribute> value = new ArrayList<>(); value.add(manager); value.add(superManager); Collection<String> urls = new LinkedHashSet<>(); String[] urlFiles = PropertyHolder.getProperty("manager.default.url").split(","); for (String urlFile : urlFiles) { Collection<String> url = FileUtils.getClassPathTextFileContent(urlFile); urls.addAll(url); } for (String url : urls) { if (url.contains("=")) { String[] attr = url.split("="); url = attr[0]; String[] roles = attr[1].split(","); Collection<ConfigAttribute> v = new ArrayList<>(); for (String role : roles) { v.add(new SecurityConfig(role)); } // POST RequestKey key = new RequestKey(url, "POST"); requestMap.put(key, v); // GET key = new RequestKey(url, "GET"); requestMap.put(key, v); } else { // POST RequestKey key = new RequestKey(url, "POST"); requestMap.put(key, value); // GET key = new RequestKey(url, "GET"); requestMap.put(key, value); } } for (Command command : serviceFacade.query(Command.class).getModels()) { List<String> paths = ModuleService.getCommandPath(command); Map<String, String> map = ModuleService.getCommandPathToRole(command); for (String path : paths) { RequestKey key = new RequestKey(path.toString().toLowerCase() + ".action*", "POST"); value = new ArrayList<>(); value.add(new SecurityConfig("ROLE_MANAGER" + map.get(path))); value.add(superManager); requestMap.put(key, value); // GET key = new RequestKey(path.toString().toLowerCase() + ".action*", "GET"); requestMap.put(key, value); } } RequestKey key = new RequestKey("/**", "POST"); value = new ArrayList<>(); value.add(superManager); requestMap.put(key, value); // GET key = new RequestKey("/**", "GET"); requestMap.put(key, value); DefaultFilterInvocationSecurityMetadataSource source = new DefaultFilterInvocationSecurityMetadataSource(new AntUrlPathMatcher(), requestMap); filterSecurityInterceptor.setSecurityMetadataSource(source); log.debug("system privilege info:\n"); for (Map.Entry<RequestKey, Collection<ConfigAttribute>> entry : requestMap.entrySet()) { log.debug(entry.getKey().toString()); for (ConfigAttribute att : entry.getValue()) { log.debug("\t" + att.toString()); } } log.info("完成初始化权限子系统..."); }