一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務(wù)器之家 - 編程語言 - Java教程 - SpringBoot添加自定義攔截器的實現(xiàn)代碼

SpringBoot添加自定義攔截器的實現(xiàn)代碼

2021-06-01 11:53紫薇帝星的故事 Java教程

這篇文章主要介紹了SpringBoot添加自定義攔截器的實現(xiàn)代碼,非常不錯,具有一定的參考借鑒價值,需要的朋友可以參考下

在controller層時,往往會需要校驗或驗證某些操作,而在每個controller寫重復(fù)代碼,工作量比較大,這里在springboot項目中 ,通過繼承webmvcconfigureradapter,添加攔截器

1、webmvcconfigureradapter源碼

?
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/*
 * copyright 2002-2016 the original author or authors.
 *
 * licensed under the apache license, version 2.0 (the "license");
 * you may not use this file except in compliance with the license.
 * you may obtain a copy of the license at
 *
 *   http://www.apache.org/licenses/license-2.0
 *
 * unless required by applicable law or agreed to in writing, software
 * distributed under the license is distributed on an "as is" basis,
 * without warranties or conditions of any kind, either express or implied.
 * see the license for the specific language governing permissions and
 * limitations under the license.
 */
package org.springframework.web.servlet.config.annotation;
import java.util.list;
import org.springframework.format.formatterregistry;
import org.springframework.http.converter.httpmessageconverter;
import org.springframework.validation.messagecodesresolver;
import org.springframework.validation.validator;
import org.springframework.web.method.support.handlermethodargumentresolver;
import org.springframework.web.method.support.handlermethodreturnvaluehandler;
import org.springframework.web.servlet.handlerexceptionresolver;
/**
 * an implementation of {@link webmvcconfigurer} with empty methods allowing
 * subclasses to override only the methods they're interested in.
 *
 * @author rossen stoyanchev
 * @since 3.1
 */
public abstract class webmvcconfigureradapter implements webmvcconfigurer {
  /**
   * {@inheritdoc}
   * <p>this implementation is empty.
   */
  @override
  public void configurepathmatch(pathmatchconfigurer configurer) {
  }
  /**
   * {@inheritdoc}
   * <p>this implementation is empty.
   */
  @override
  public void configurecontentnegotiation(contentnegotiationconfigurer configurer) {
  }
  /**
   * {@inheritdoc}
   * <p>this implementation is empty.
   */
  @override
  public void configureasyncsupport(asyncsupportconfigurer configurer) {
  }
  /**
   * {@inheritdoc}
   * <p>this implementation is empty.
   */
  @override
  public void configuredefaultservlethandling(defaultservlethandlerconfigurer configurer) {
  }
  /**
   * {@inheritdoc}
   * <p>this implementation is empty.
   */
  @override
  public void addformatters(formatterregistry registry) {
  }
  /**
   * {@inheritdoc}
   * <p>this implementation is empty.
   */
  @override
  public void addinterceptors(interceptorregistry registry) {
  }
  /**
   * {@inheritdoc}
   * <p>this implementation is empty.
   */
  @override
  public void addresourcehandlers(resourcehandlerregistry registry) {
  }
  /**
   * {@inheritdoc}
   * <p>this implementation is empty.
   */
  @override
  public void addcorsmappings(corsregistry registry) {
  }
  /**
   * {@inheritdoc}
   * <p>this implementation is empty.
   */
  @override
  public void addviewcontrollers(viewcontrollerregistry registry) {
  }
  /**
   * {@inheritdoc}
   * <p>this implementation is empty.
   */
  @override
  public void configureviewresolvers(viewresolverregistry registry) {
  }
  /**
   * {@inheritdoc}
   * <p>this implementation is empty.
   */
  @override
  public void addargumentresolvers(list<handlermethodargumentresolver> argumentresolvers) {
  }
  /**
   * {@inheritdoc}
   * <p>this implementation is empty.
   */
  @override
  public void addreturnvaluehandlers(list<handlermethodreturnvaluehandler> returnvaluehandlers) {
  }
  /**
   * {@inheritdoc}
   * <p>this implementation is empty.
   */
  @override
  public void configuremessageconverters(list<httpmessageconverter<?>> converters) {
  }
  /**
   * {@inheritdoc}
   * <p>this implementation is empty.
   */
  @override
  public void extendmessageconverters(list<httpmessageconverter<?>> converters) {
  }
  /**
   * {@inheritdoc}
   * <p>this implementation is empty.
   */
  @override
  public void configurehandlerexceptionresolvers(list<handlerexceptionresolver> exceptionresolvers) {
  }
  /**
   * {@inheritdoc}
   * <p>this implementation is empty.
   */
  @override
  public void extendhandlerexceptionresolvers(list<handlerexceptionresolver> exceptionresolvers) {
  }
  /**
   * {@inheritdoc}
   * <p>this implementation returns {@code null}.
   */
  @override
  public validator getvalidator() {
    return null;
  }
  /**
   * {@inheritdoc}
   * <p>this implementation returns {@code null}.
   */
  @override
  public messagecodesresolver getmessagecodesresolver() {
    return null;
  }
}

可以看出,該類 還能配置其他很多操作,例如異常處理,跨域請求等配置。

2、自動義web配置類

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import org.springframework.context.annotation.bean;
import org.springframework.context.annotation.configuration;
import org.springframework.web.servlet.config.annotation.interceptorregistry;
import org.springframework.web.servlet.config.annotation.webmvcconfigureradapter;
@configuration
public class webmvcconfig extends webmvcconfigureradapter {
  @override
  public void addinterceptors(interceptorregistry registry) {
    registry.addinterceptor(getmyinterceptor()).addpathpatterns("/**");
  }
  @bean
  public myinterceptor getmyinterceptor(){
    return new myinterceptor();
  }
}

  如果需要添加多個攔截器,interceptorregistry registry.addinterceptor方法

?
1
2
3
4
5
public interceptorregistration addinterceptor(handlerinterceptor interceptor) {
    interceptorregistration registration = new interceptorregistration(interceptor);
    this.registrations.add(registration);
    return registration;
  }

registrations是個數(shù)組結(jié)構(gòu),可以添加多個

3、自動義攔截器

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;
import javax.servlet.http.httpsession;
import org.slf4j.logger;
import org.slf4j.loggerfactory;
import org.springframework.web.method.handlermethod;
import org.springframework.web.servlet.handler.handlerinterceptoradapter;
public class myinterceptor extends handlerinterceptoradapter {
  final logger logger = loggerfactory.getlogger(getclass());
  @override
  public boolean prehandle(httpservletrequest request, httpservletresponse response, object handler) throws exception {
    //攔截操作
    return true;
  }
}

總結(jié)

以上所述是小編給大家介紹的springboot添加自定義攔截器的實現(xiàn)代碼,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復(fù)大家的。在此也非常感謝大家對服務(wù)器之家網(wǎng)站的支持!

原文鏈接:https://www.cnblogs.com/zwdx/archive/2018/09/19/9674497.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 亚裔aⅴ艳星katsuni | 国产成人久视频免费 | 色吧五月婷婷 | 日韩aaa| 午夜影院免费体验 | 五月色综合婷婷综合俺来也 | 91综合在线视频 | 亚洲男人天堂a | 欧美最猛性xxxxx短视频 | 丁香婷婷在线视频 | 继攵催眠女乱h调教 | 国产免费一区二区三区免费视频 | 精品亚洲午夜久久久久 | 久久久久久免费高清电影 | 婷婷久久综合 | 国产精品久久国产精品99 gif | 欧美精品一区二区在线观看播放 | 免费看成年视频网页 | 波多野结衣52部合集在线观看 | 午夜欧美精品 | 天天色视频 | 饭冈加奈子在线播放观看 | 拔插拔插8x8x海外华人免费视频 | 国产青草视频在线观看免费影院 | 撕开老师的丝袜白丝扒开粉嫩的小 | 小小水蜜桃视频高清在线播放 | 精品国语国产在线对白 | 精品一区二区三区视频日产 | free哆拍拍免费永久视频 | 门卫老张和女警花小说 | 精品高潮呻吟99AV无码 | 日本在线视频网址 | 国产一级毛片潘金莲的奶头 | 国产日韩欧美精品在线 | 处女私拍 | 男男gaygays国内 | 911亚洲精品国内自产 | 国产黄频| 久久天堂成人影院 | 国产午夜亚洲精品理论片不卡 | 国产精品国产三级在线专区 |