🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
[TOC] # 简介 ![](https://img.kancloud.cn/fa/b3/fab396024db3737439f3ba8114f4a361_419x285.png) 首先任务会先到消息处理器. 消息处理器调用doFilter方法,也就是处理链中的doFilter. 处理链的doFilter是循环调用它内部的一个个处理类. 处理类的doFilter是详细处理 ![](https://img.kancloud.cn/c5/1c/c51cecc9c50866739fdeeac1dd73d9c0_380x194.png) ![](https://img.kancloud.cn/39/74/39749c5527517ffe83a857d0d2ff6c00_723x359.png) # 接口 规范实现类 ~~~ public interface Filter { String doFilter(String str); } ~~~ # 具体处理类 ## HtmlFilter ~~~ public class HtmlFilter implements Filter { @Override public String doFilter(String str) { //处理特殊字符 String replace = str.replace('<', '[') .replace('>', ']'); return replace; } } ~~~ ## SesitiveFilte ~~~ public class SesitiveFilte implements Filter { @Override public String doFilter(String str) { //处理敏感字 String r = str.replace("敏感", "**") .replace("不好", "**"); return r; } } ~~~ ## FaceFilter ~~~ public class FaceFilter implements Filter { @Override public String doFilter(String str) { //处理特殊字符 String replace = str.replace(':', '*'); return replace; } } ~~~ # 责任链 ~~~ public class FilterChain implements Filter { List<Filter> filters = new ArrayList<Filter>(); //把责任添加进去 public FilterChain addFilter(Filter f) { this.filters.add(f); return this; } @Override public String doFilter(String str) { String r = str; //挨个调用每个filter的处理方法,一直在里面循环直到循环结束 for (Filter filter : filters) { r = filter.doFilter(r); } return r; } } ~~~ # 消息处理程序 这个是启动责任链的程序 ~~~ @Data public class MsgProcessor { //保存消息 private String msg; //保存责任链 FilterChain fc; public String process() { return fc.doFilter(msg); } } ~~~ # 主程序 ~~~ String msg = "大家好:), <script>: 敏感词, 不好"; //初始化消息处理器 MsgProcessor mp = new MsgProcessor(); //把要处理的消息添加进去 mp.setMsg(msg); //初始化,责任链 FilterChain fc = new FilterChain(); //添加过滤器,链式调用 fc.addFilter(new HtmlFilter()).addFilter(new SesitiveFilte()); //添加子链,这个子责任链是挂到主责任链上面的 FilterChain fc2 = new FilterChain(); fc2.addFilter(new FaceFilter()); //把子链添加到责任链中 fc.addFilter(fc2); //把责任链添加到消息处理器中 mp.setFc(fc); //进行消息处理 String result = mp.process(); System.out.println(result); ~~~ # web中的责任链 ![](https://img.kancloud.cn/66/d4/66d4132429eb9ee3d0dde6bd95543b9f_455x263.png) ![](https://img.kancloud.cn/61/ba/61baba9e5af7be4578d41bf65aa159e3_367x490.png) ## Filter接口 ~~~ public interface Filter { //给他请求类和响应类,他来调用责任链处理里面的 void doFilter(Request request, Response response, FilterChain filterChain); } ~~~ ## 具体处理 ### FaceFilter ~~~ public class FaceFilter implements Filter { @Override public void doFilter(Request request, Response response, FilterChain filterChain) { //请求处理特殊字符 String str = request.getRequestStr() .replace(':', '*'); request.setRequestStr(str); filterChain.doFilter(request, response, filterChain); //响应处理的 String responseMsg = response.getResponseStr() + "---FaceFilter"; response.setResponseStr(responseMsg); } } ~~~ ### HtmlFilter ~~~ public class HtmlFilter implements Filter { @Override public void doFilter(Request request, Response response, FilterChain filterChain) { //请求处理特殊字符 String str = request.getRequestStr() .replace('<', '[') .replace('>', ']'); request.setRequestStr(str); filterChain.doFilter(request, response, filterChain); //响应处理的 String responseMsg = response.getResponseStr() + "---HtmlFilter"; response.setResponseStr(responseMsg); } } ~~~ ### SesitiveFilte ~~~ public class SesitiveFilte implements Filter { @Override public void doFilter(Request request, Response response, FilterChain filterChain) { //请求处理特殊字符 String str = request.getRequestStr() .replace("敏感", "**") .replace("不好", "**"); request.setRequestStr(str); filterChain.doFilter(request, response, filterChain); //响应处理的 String responseMsg = response.getResponseStr() + "---SesitiveFilte"; response.setResponseStr(responseMsg); } } ~~~ ## 责任链 ~~~ public class FilterChain implements Filter { List<Filter> filters = new ArrayList<Filter>(); //记录运行到那个filter上 int index = 0; public FilterChain addFilter(Filter f) { this.filters.add(f); return this; } @Override public void doFilter(Request request, Response response, FilterChain filterChain) { //没有filter了 if (index == filters.size()) { return; } //取出filter Filter f = filters.get(index); index++; f.doFilter(request, response, filterChain); } } ~~~ ## 消息处理程序 ~~~ @Data public class MsgProcessor { //保存请求 private Request request; //保存响应 private Response response; //保存调用链 private FilterChain filterChain; public void process() { //调用责任链处理 filterChain.doFilter(this.getRequest(), this.getResponse(), this.getFilterChain()); } } ~~~ ## 主程序 ~~~ //提前准备好请求和响应 Request request = new Request(); Response response = new Response(); String msg = "大家好:), <script>: 敏感词, 不好"; //请求需要处理的 request.setRequestStr(msg); //响应需要处理的 response.setResponseStr("abc---123"); //初始化消息处理器 MsgProcessor mp = new MsgProcessor(); //把要处理的请求或者响应添加进去 mp.setRequest(request); mp.setResponse(response); //初始化,责任链 FilterChain fc = new FilterChain(); //添加过滤器 fc.addFilter(new HtmlFilter()).addFilter(new SesitiveFilte()); //添加子链,这个子责任链是挂到主责任链上面的 FilterChain fc2 = new FilterChain(); fc2.addFilter(new FaceFilter()); //把子链添加到责任链中 fc.addFilter(fc2); //把责任链添加到消息处理器中 mp.setFilterChain(fc); //进行消息处理 mp.process(); System.out.println(request.getRequestStr()); System.out.println(response.getResponseStr()); ~~~