In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
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.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.