/** * The <code>doFilter</code> method of the Filter is called by the container * each time a request/response pair is passed through the chain due to a * client request for a resource at the end of the chain. The FilterChain * passed in to this method allows the Filter to pass on the request and * response to the next entity in the chain. * <p> * A typical implementation of this method would follow the following * pattern:- <br> * 1. Examine the request<br> * 2. Optionally wrap the request object with a custom implementation to * filter content or headers for input filtering <br> * 3. Optionally wrap the response object with a custom implementation to * filter content or headers for output filtering <br> * 4. a) <strong>Either</strong> invoke the next entity in the chain using * the FilterChain object (<code>chain.doFilter()</code>), <br> * 4. b) <strong>or</strong> not pass on the request/response pair to the * next entity in the filter chain to block the request processing<br> * 5. Directly set headers on the response after invocation of the next * entity in the filter chain. * * @param request The request to process * @param response The response associated with the request * @param chain Provides access to the next filter in the chain for this * filter to pass the request and response to for further * processing * @throws IOException if an I/O error occurs during this filter's * processing of the request * @throws ServletException if the processing fails for any other reason */ @Override publicvoiddoFilter(ServletRequest request, ServletResponse response, FilterChain chain)throws IOException, ServletException { ResponseWrapperresponseWrapper=newResponseWrapper((HttpServletResponse) response); HttpServletRequesthttpServletRequest= (HttpServletRequest) request; longstartTime= System.nanoTime();
for (inti=0; i < txt.length(); i++) { intlength= CheckSensitiveWord(txt, i, matchType); //判断是否包含敏感字符 if (length > 0) { //存在,加入list中 sensitiveWordList.add(txt.substring(i, i + length)); i = i + length - 1; //减1的原因,是因为for会自增 } }
/** * Constructs a response adaptor wrapping the given response. * * @param response The response to be wrapped * @throws IllegalArgumentException if the response is null */ @SneakyThrows publicResponseWrapper(HttpServletResponse response) { super(response); buffer = newByteArrayOutputStream(); outputStream = newWrapperOutputStream(buffer); writer = newPrintWriter(newOutputStreamWriter(buffer, StandardCharsets.UTF_8)); }
/** * The default behavior of this method is to return getOutputStream() on the * wrapped response object. */ @Override public ServletOutputStream getOutputStream()throws IOException { return outputStream; }
/** * The default behavior of this method is to return getWriter() on the * wrapped response object. */ @Override public PrintWriter getWriter()throws IOException { return writer; }
/** * The default behavior of this method is to call flushBuffer() on the * wrapped response object. */ @Override @SneakyThrows publicvoidflushBuffer() { if (outputStream != null) { outputStream.flush(); } if (writer != null) { writer.flush(); } }
/** * The default behavior of this method is to call reset() on the wrapped * response object. */ @Override publicvoidreset() { buffer.reset(); }
/** * Writes the specified byte to this output stream. The general * contract for <code>write</code> is that one byte is written * to the output stream. The byte to be written is the eight * low-order bits of the argument <code>b</code>. The 24 * high-order bits of <code>b</code> are ignored. * <p> * Subclasses of <code>OutputStream</code> must provide an * implementation for this method. * * @param b the <code>byte</code>. * @throws IOException if an I/O error occurs. In particular, * an <code>IOException</code> may be thrown if the * output stream has been closed. */ @Override publicvoidwrite(int b)throws IOException { bos.write(b); }
/** * Checks if a non-blocking write will succeed. If this returns * <code>false</code>, it will cause a callback to * {@link WriteListener#onWritePossible()} when the buffer has emptied. If * this method returns <code>false</code> no further data must be written * until the contain calls {@link WriteListener#onWritePossible()}. * * @return <code>true</code> if data can be written, else <code>false</code> * @since Servlet 3.1 */ @Override publicbooleanisReady() { returnfalse; }
/** * Sets the {@link WriteListener} for this {@link ServletOutputStream} and * thereby switches to non-blocking IO. It is only valid to switch to * non-blocking IO within async processing or HTTP upgrade processing. * * @param listener The non-blocking IO write listener * @throws IllegalStateException If this method is called if neither * async nor HTTP upgrade is in progress or * if the {@link WriteListener} has already * been set * @throws NullPointerException If listener is null * @since Servlet 3.1 */ @Override publicvoidsetWriteListener(WriteListener listener) {