Struts2的拦截器功能强大,每个Action调用之前都会进行过滤器权限的校验、文件格式的校验等,拦截器用到的主要设计模式
就是责任链模式思想了,包括Netty中的ChannelPipeline,也可以看成是一个链条容器,链条就是ChannelHandler了,
下面通过一个列子来演示责任链模式来展示其设计巧妙之处。
先定义一个接口Filter,即过滤器或者拦截器,里面有一个doFilter方法,用于过滤或拦截一些信息。
package com.sf.simba.filter;
/**
*
* @author -simba-
*
*/
public interface Filter {
void doFilter(Request request,Response response,FilterChain filterChain);
}
定义一个HTMLFilter专门过滤含有html语言的类
package com.sf.simba.filter;
/**
* html语言过滤器
* @author -simba-
*
*/
public class HTMLFilter implements Filter {
@Override
public void doFilter(Request request, Response response,
FilterChain filterChain) {
request.setRequestStr(request.getRequestStr().replaceAll("<", "[").replaceAll(">", "]"));
System.out.println("------request:HTMLFilter-------");
filterChain.doFilter(request, response, filterChain);//执行下一个拦截器
response.setResponseStr(response.getResponseStr()+"------response:HTMLFilter------");//response返回
}
}
权限校验类
package com.sf.simba.filter;
/**
* 权限过滤器
* @author -simba-
*
*/
public class PowerFilter implements Filter {
@Override
public void doFilter(Request request, Response response,
FilterChain filterChain) {
request.setRequestStr(request.getRequestStr().replaceAll("权限", "你有权限"));
System.out.println("------request:PowerFilter-------");
filterChain.doFilter(request, response, filterChain);
response.setResponseStr(response.getResponseStr()+"------response:PowerFilter------");
}
}
敏感信息过滤类
package com.sf.simba.filter;
/**
* 敏感过滤器
* @author -Simba-
*
*/
public class SensitiveFilter implements Filter {
@Override
public void doFilter(Request request, Response response,
FilterChain filterChain) {
request.setRequestStr(request.getRequestStr().replaceAll("饭桶在政府", "高手在民间"));
System.out.println("------request:SensitiveFilter-------");
filterChain.doFilter(request, response, filterChain);
response.setResponseStr(response.getResponseStr()+"------response:SensitiveFilter------");
}
}
请求类
package com.sf.simba.filter;
/**
* 请求类
* @author -simba-
*
*/
public class Request {
private String requestStr;
public String getRequestStr() {
return requestStr;
}
public void setRequestStr(String requestStr) {
this.requestStr = requestStr;
}
}
返回类
package com.sf.simba.filter;
public class Response {
private String responseStr;
public String getResponseStr() {
return responseStr;
}
public void setResponseStr(String responseStr) {
this.responseStr = responseStr;
}
}
过滤器处理类,处理每个链,其中它自己也是一个链,只是这个链中包含了几个小链条。
package com.sf.simba.filter;
import java.util.ArrayList;
import java.util.List;
/**
* 拦截器链本身也是一种拦截器
* @author -simba-
*
*/
public class FilterChain implements Filter {
private List<Filter> filters = new ArrayList<Filter>();
private int index = 0;
public FilterChain addFilter(Filter filter){
this.filters.add(filter);
return this;
}
@Override
public void doFilter(Request request, Response response,
FilterChain filterChain) {
if(index >= filters.size()) return;
Filter filter = filters.get(index);
if(filter instanceof FilterChain){//如果拦截器是一种拦截器链,那么需要把自己传入,即一个新的拦截器链
filter.doFilter(request, response, (FilterChain)filter);
}
index ++;
filter.doFilter(request, response, filterChain);//把拦截器链传个拦截器,好让拦截器知道下一个拦截器是谁
}
}
测试类
package com.sf.simba.filter;
public class FilterMain {
public static void main(String[] args) {
String message = "你知道吗?饭桶在政府,<script>alert('权限')</script>,哈哈";
Request request = new Request();
request.setRequestStr(message);
Response response = new Response();
response.setResponseStr("response:");
FilterChain filterChain = new FilterChain();
filterChain.addFilter(new HTMLFilter())
.addFilter(new PowerFilter());
FilterChain filterChain1 = new FilterChain();
filterChain1.addFilter(new SensitiveFilter())
.addFilter(filterChain);
filterChain1.doFilter(request, response, filterChain1);
System.out.println(request.getRequestStr());
System.out.println(response.getResponseStr());
}
}
测试结果
——request:SensitiveFilter——-
——request:HTMLFilter——-
——request:PowerFilter——-
你知道吗?高手在民间,[script]alert(‘你有权限’)[/script],哈哈
response:——response:PowerFilter————response:HTMLFilter————response:SensitiveFilter——
拦截器原理图如下: