业务场景

每个版本都是一点一点演化过去的。代码版本多,但是都很简单行数也不多

  1. 在论坛发布文章
  2. 后台要经过处理才可以发表或者存入数据库
  3. 每条链都需要有权限去判断还继不继续往下执行
  4. servlet filter实现

代码版本v1

代码版本v1

public class Main {
public static void main(String[] args) {
Msg msg = new Msg();
//在论坛发帖
msg.setMsg("大家好:}<script> ,欢迎访问www.zeze.info 台湾 ");

//第一步网页脚本
String r = msg.getMsg();
r = r.replace("<","&lt;");
r = r.replace(">","&gt;");
msg.setMsg(r);

//第二步处理敏感词
r = r.replace("台湾","中国");
msg.setMsg(r);
System.out.println(msg);
}
}

class Msg{
String name;
String msg;

public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public String toString() {
return "Msg{" +
"msg='" + msg + '\'' +
'}';
}
}

代码版本说明

代码直接写在一起,如果要加入其它的处理逻辑必须修改源码,比如现在要加一个URL过滤的处理,所以现在抽象出Filter,和FilterChain。对应版本V2和v3

代码V2-v3

代码版本v2
代码版本v3

这里直接贴出代码V3

package com.cor.v3;

import java.util.ArrayList;
import java.util.List;

class Msg{
String name;
String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public String toString() {
return "Msg{" +
"msg='" + msg + '\'' +
'}';
}
}

interface Filter{
void doFilter( Msg msg);
}
class FilterChain{
List< Filter> filters = new ArrayList<>();
public void addFilter(Filter filter){
filters.add(filter);
}
public void deleteFilter(Filter filter){
filters.remove(filter);
}
/**
* 链条处理完.这个方法名现在 故意这么取得后面就知道了
*/
public void doFilter(Msg msg){
for ( Filter f : filters){
f.doFilter(msg);
}
}
}

class HTMLfilter implements Filter {

@Override
public void doFilter( Msg msg) {
//第一步网页脚本
String r = msg.getMsg();
r = r.replace("<","&lt;");
r = r.replace(">","&gt;");
msg.setMsg(r);
}
}

class SensitiveFilter implements Filter {

@Override
public void doFilter( Msg msg) {
String r = msg.getMsg();
//第二步处理敏感词
r = r.replace("台湾","中国");
msg.setMsg(r);
}
}

public class Main {
public static void main(String[] args) {
Msg msg = new Msg();
//在论坛发帖
msg.setMsg("大家好:}<script> ,欢迎访问www.zeze.info 台湾 ");

FilterChain filterChain = new FilterChain();

filterChain.addFilter(new HTMLfilter());
filterChain.addFilter(new SensitiveFilter());
filterChain.doFilter(msg);

System.out.println(msg);
}
}

代码版本说明

此时处理链是可以配置,比如我要先处理HTML,或者先处理Sensitive,但是现在需求就是我这条链可以加入其它链,而且如果上一条链处理不通过 下面所以的链都不执行了

代码v4-v5

代码版本v4
代码版本v5

这里直接贴出 V5,我改进的有两个地方,第一让FilterChain也去实现Filter。这样我在addFilter的时候也可以加FilterChain.还有一个小技巧,在addFilter的方法返回一个FilterChain这样可以在调用的时链式调用。将doFilter方法返回值改为Boolean。即可实现新的需求

package com.cor.v5;

import java.util.ArrayList;
import java.util.List;

class Msg{
String name;
String msg;
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
@Override
public String toString() {
return "Msg{" +
"msg='" + msg + '\'' +
'}';
}
}

interface Filter{
boolean doFilter(Msg msg);
}

class FilterChain implements Filter {
List<Filter> filters = new ArrayList<>();
public FilterChain addFilter(Filter filter){
filters.add(filter);
return this;
}

public void deleteFilter(Filter filter){
filters.remove(filter);
}

/**
* 链条处理完
*/
@Override
public boolean doFilter(Msg msg){
for ( Filter f : filters){
boolean r = f.doFilter(msg);
if(!r){
return false;
}
}
return true;
}
}

class HTMLfilter implements Filter {

@Override
public boolean doFilter( Msg msg) {
//第一步网页脚本
String r = msg.getMsg();
r = r.replace("<","&lt;");
r = r.replace(">","&gt;");
msg.setMsg(r);
return true;
}
}

class SensitiveFilter implements Filter {

@Override
public boolean doFilter( Msg msg) {
String r = msg.getMsg();
//第二步处理敏感词
r = r.replace("台湾","中国");
msg.setMsg(r);
return true;
}
}

class URLFilter implements Filter {

@Override
public boolean doFilter(Msg msg) {
String r = msg.getMsg();
r = r.replace("zeze.info","^_^");
msg.setMsg(r);
return true;
}
}


class SQLFilter implements Filter {

@Override
public boolean doFilter(Msg msg) {
String r = msg.getMsg();
r = r.replace("OR","");
r = r.replace("select","");
msg.setMsg(r);
return true;
}
}

public class Main {
public static void main(String[] args) {
Msg msg = new Msg();
//在论坛发帖
msg.setMsg("大家好:}<script> ,欢迎访问www.zeze.info 台湾 OR 1=1 ");

FilterChain filterChain = new FilterChain();

filterChain.addFilter(new HTMLfilter())
.addFilter(new SensitiveFilter());
FilterChain filterChain2 = new FilterChain();
filterChain2.addFilter(new URLFilter());
filterChain2.addFilter(new SQLFilter());
filterChain.addFilter(filterChain2);
System.out.println(msg);
}
}

servlet filter实现

先统一处理Request,然后在处理Response

servlet filter

代码版本v6

interface Filter{
boolean doFilter(Request request,Response response,FilterChain filterChain);
}

class FilterChain implements Filter {
List<Filter> filters = new ArrayList<>();
public FilterChain addFilter(Filter filter){
filters.add(filter);
return this;
}
int index = 0;

public void deleteFilter(Filter filter){
filters.remove(filter);
}

/**
* 链条处理完
*/
@Override
public boolean doFilter(Request request,Response response,FilterChain filterChain){
if(index==filters.size()){return true;}
Filter chain = filters.get(index);
index++;
chain.doFilter(request,response,filterChain);
return true;
}
}

class Request{
String data="Request:";

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}
}

class Response{
String data="Response:";

public String getData() {
return data;
}

public void setData(String data) {
this.data = data;
}
}

class HTMLfilter implements Filter {

@Override
public boolean doFilter(Request request,Response response,FilterChain filterChain) {
request.setData(request.getData()+"HTMLfilter--");
filterChain.doFilter(request, response, filterChain);
response.setData(response.getData()+"HTMLfilter--");
return true;
}
}

class SensitiveFilter implements Filter {

@Override
public boolean doFilter(Request request,Response response,FilterChain filterChain) {
request.setData(request.getData()+"SensitiveFilter--");
filterChain.doFilter(request, response, filterChain);
response.setData(response.getData()+"SensitiveFilter--");
return true;
}
}

public class Main {
public static void main(String[] args) {
Response response = new Response();
Request request = new Request();

FilterChain filterChain = new FilterChain();
filterChain.addFilter(new HTMLfilter()).addFilter(new SensitiveFilter());
filterChain.doFilter(request,response,filterChain);
System.out.println(request.getData());
System.out.println(response.getData());
}
}

如果对你有帮助,请转发,谢谢

博主主页

可以加博主微信一起交流:twobixiaoxin