Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use dynamic proxy object to filter sensitive words in JAVA

Shulou Source: shulou.com Published: 2022-06-03 09:20:50 10月04日 Update

This article will explain in detail how JAVA uses dynamic proxy objects to filter sensitive words. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.

Package com.hopetesting.web.filter;import javax.servlet.*;import javax.servlet.annotation.WebFilter;import java.io.BufferedReader;import java.io.FileNotFoundException;import java.io.FileReader;import java.io.IOException;import java.lang.reflect.InvocationHandler;import java.lang.reflect.Method;import java.lang.reflect.Proxy;import java.util.ArrayList;import java.util.List / * * @ author newcityman * @ date 2019-9-13-19:41 * / @ WebFilter ("/ *") public class SensitiveWordsFilter implements Filter {public void destroy () {} public void doFilter (ServletRequest req, ServletResponse resp, FilterChain chain) throws ServletException, IOException {/ / 1, create proxy object Enhanced getParameter method ServletRequest proxy_req = (ServletRequest) Proxy.newProxyInstance (req.getClass (). GetClassLoader (), req.getClass (). GetInterfaces (), new InvocationHandler () {@ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {/ / determine whether it is a getParameter method / / if it is Enhanced getParameter method if (method.getName (). Equals ("getParameter")) {/ / enhanced return value / / get the return value String value = (String) method.invoke (req, args) If (value! = null) {for (String str: list) {if (value.contains (str)) {value = value.replace (str, "*");} return value;} return method.invoke (req, args);}}) Chain.doFilter (proxy_req, resp);} private List list = new ArrayList (); / / sensitive vocabulary collection public void init (FilterConfig config) throws ServletException {try {/ / 1, the real path to get the file ServletContext servletContext = config.getServletContext (); String realPath = servletContext.getRealPath ("/ WEB-INF/classes/ sensitive vocabulary .txt"); / / 2. Read the file BufferedReader br = new BufferedReader (new FileReader (realPath)) / / 3. Read each line of the file into list String line = null; while ((line = br.readLine ())! = null) {list.add (line);} br.close (); System.out.println (list);} catch (FileNotFoundException e) {e.printStackTrace ();} catch (IOException e) {e.printStackTrace () } this is the end of the article on "how JAVA uses dynamic proxy objects to filter sensitive words". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Objects agents files methods articles dynamic more vocabulary good practical one line content articles knowledge paths references help related Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Huawei Shulou Technology Linux vpn