一、自定义拦截器实现 handlerinterceptor 接口
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
|
/** * * created by zhh on 2018/04/20. */ public class myinterceptor implements handlerinterceptor { @autowired private networkproxyinfoservice networkproxyinfoservice; @override public void aftercompletion(httpservletrequest arg0, httpservletresponse arg1, object arg2, exception arg3) throws exception { // todo auto-generated method stub } @override public void posthandle(httpservletrequest arg0, httpservletresponse arg1, object arg2, modelandview arg3) throws exception { networkproxyinfoservice.getallnetworkproxyinfobyisvalid(gobalconstant.proxyvalid.valid); } @override public boolean prehandle(httpservletrequest arg0, httpservletresponse arg1, object arg2) throws exception { // todo auto-generated method stub return true ; } } |
二、自定义拦截器配置
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
|
/** * * created by zhh on 2018/04/20. */ @configuration public class mywebmvcconfig extends webmvcconfigureradapter { /** * 将自定义拦截器作为bean写入配置 * @return */ @bean public myinterceptor myinterceptor() { return new myinterceptor(); } @override public void addinterceptors(interceptorregistry registry) { /** * 多个拦截器组成一个拦截器链 * addpathpatterns 用于添加拦截规则 * excludepathpatterns 用户排除拦截 */ registry.addinterceptor(myinterceptor()).addpathpatterns( "/**" ); super .addinterceptors(registry); } } |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://segmentfault.com/a/1190000015413584