欢迎来到 IT实训基地-南通科迅教育
咨询电话:0513-81107100
java责任链模式
2017/4/3
南通科迅教育
485
南通Web前端培训中心怎样选择
java责任链模式:Struts2的拦截器功能强大,每个Action调用之前都会进行过滤器权限的校验、文件格式的校验等,拦截器用到的主要设计模式。

就是责任链模式思想了,包括Netty中的ChannelPipeline,也可以看成是一个链条容器,链条就是ChannelHandler了,
下面通过一个列子来演示责任链模式来展示其设计巧妙之处。
先定义一个接口Filter,即过滤器或者拦截器,里面有一个doFilter方法,用于过滤或拦截一些信息。

?
1
2
3
4
5
6
7
8
9
packagecom.sf.simba.filter;
/**
 *
 * @author -simba-
 *
 */
publicinterfaceFilter {
      voiddoFilter(Request request,Response response,FilterChain filterChain);
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
packagecom.sf.simba.filter;
/**
 * @author -simba-
 *
 */
publicclassHTMLFilterimplementsFilter {
 
    @Override
    publicvoiddoFilter(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返回
 
 
    }
 
}

权限校验类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
packagecom.sf.simba.filter;
/**
 * 权限过滤器
 * @author -simba-
 *
 */
publicclassPowerFilterimplementsFilter {
 
    @Override
    publicvoiddoFilter(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------");
 
    }
 
}

敏感信息过滤类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
packagecom.sf.simba.filter;
/**
 * 敏感过滤器
 * @author -Simba-
 *
 */
publicclassSensitiveFilterimplementsFilter {
 
    @Override
    publicvoiddoFilter(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------");
    }
 
}

请求类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
packagecom.sf.simba.filter;
/**
 * 请求类
 * @author -simba-
 *
 */
publicclassRequest  {
    privateString requestStr;
 
 
 
    publicString getRequestStr() {
        returnrequestStr;
    }
 
    publicvoidsetRequestStr(String requestStr) {
        this.requestStr = requestStr;
    }
 
}

返回类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
packagecom.sf.simba.filter;
 
publicclassResponse  {
   privateString responseStr;
 
    publicString getResponseStr() {
        returnresponseStr;
    }
    publicvoidsetResponseStr(String responseStr) {
        this.responseStr = responseStr;
    }
 
}

过滤器处理类,处理每个链,其中它自己也是一个链,只是这个链中包含了几个小链条。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
packagecom.sf.simba.filter;
 
importjava.util.ArrayList;
importjava.util.List;
/**
 * 拦截器链本身也是一种拦截器
 * @author -simba-
 *
 */
publicclassFilterChainimplementsFilter {
 
    privateList<filter> filters =newArrayList<filter>();
 
    private intindex =0;
 
    publicFilterChain addFilter(Filter filter){
        this.filters.add(filter);
        returnthis;
    }
    @Override
    publicvoiddoFilter(Request request, Response response,
            FilterChain filterChain) {
 
        if(index >= filters.size())return;
        Filter filter = filters.get(index);
        if(filterinstanceofFilterChain){//如果拦截器是一种拦截器链,那么需要把自己传入,即一个新的拦截器链
            filter.doFilter(request, response, (FilterChain)filter);
        }
        index ++;
        filter.doFilter(request, response, filterChain);//把拦截器链传个拦截器,好让拦截器知道下一个拦截器是谁
 
    }
 
}</filter></filter>

测试类

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
packagecom.sf.simba.filter;
 
publicclassFilterMain {
 
    publicstaticvoidmain(String[] args) {
        String message ="你知道吗?饭桶在政府,<script>alert('权限')</script>,哈哈";
 
        Request request =newRequest();
        request.setRequestStr(message);
        Response response =newResponse();
        response.setResponseStr("response:");
 
        FilterChain filterChain =newFilterChain();
        filterChain.addFilter(newHTMLFilter())
                   .addFilter(newPowerFilter());
 
        FilterChain filterChain1 =newFilterChain();
        filterChain1.addFilter(newSensitiveFilter())
                    .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——

拦截器原理图如下:

77
关闭
先学习,后交费申请表
每期5位名额
在线咨询
免费电话
QQ联系
先学习,后交费
TOP
您好,您想咨询哪门课程呢?
关于我们
机构简介
官方资讯
地理位置
联系我们
0513-91107100
周一至周六     8:30-21:00
微信扫我送教程
手机端访问
南通科迅教育信息咨询有限公司     苏ICP备15009282号     联系地址:江苏省南通市人民中路23-6号新亚大厦三楼             法律顾问:江苏瑞慈律师事务所     Copyright 2008-