構(gòu)建一個(gè)spring boot項(xiàng)目。
添加攔截器需要添加一個(gè)configuration
1
2
3
|
@Configuration @ComponentScan (basePackageClasses = Application. class , useDefaultFilters = true ) public class ServletContextConfig extends WebMvcConfigurationSupport { |
為了方便掃描位置,我們可以寫一個(gè)接口或者入口類Application放置于最外一層的包內(nèi),這樣就會(huì)掃描該類以及子包的類。
1 resources配置
在沒有配置這個(gè)類的時(shí)候,我們可以在application.ym中修改靜態(tài)文件位置和匹配方式:
1
2
3
4
5
6
7
|
#指定環(huán)境配置文件 spring: profiles: active: dev # 修改默認(rèn)靜態(tài)路徑,默認(rèn)為/**,當(dāng)配置hello.config.ServletContextConfig后此處配置失效 mvc: static-path-pattern: /static/** |
但當(dāng)我們繼承了WebMvcConfigurationSupport 并配置掃描后,上述resources的配置失效,還原默認(rèn)配置。那么我們需要在這個(gè)類中再次指定靜態(tài)資源位置:
1
2
3
4
5
|
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler( "/" ).addResourceLocations( "/**" ); registry.addResourceHandler( "/static/**" ).addResourceLocations( "classpath:/static/" ); } |
這樣訪問classpath下的static包下的靜態(tài)資源的url匹配為/static/xxx.js。默認(rèn)匹配static下的靜態(tài)文件url為/xxx.js,雖然清潔,但我感覺idea不會(huì)識(shí)別這種路徑,還是改成完整的路徑比較好。
2.Interceptor配置
配置登錄攔截或者別的。需要?jiǎng)?chuàng)建一個(gè)攔截器類來繼承HandlerInterceptorAdapter,然后只需要覆蓋你想要攔截的位置就可以了。比如,我只是攔截訪問方法之前:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
package hello.interceptor; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Created by miaorf on 2016/8/3. */ public class LoginInterceptor extends HandlerInterceptorAdapter { private Logger logger = LoggerFactory.getLogger(LoginInterceptor. class ); @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String authorization = request.getHeader( "Authorization" ); logger.info( "The authorization is: {}" ,authorization); return super .preHandle(request, response, handler); } } |
寫好interceptor之后需要在開始創(chuàng)建的ServletContextConfig中添加這個(gè)攔截器:
1
2
3
4
5
6
7
|
@Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor( new LoginInterceptor()) .addPathPatterns( "/**" ) .excludePathPatterns(FAVICON_URL) ; } |
完整的ServletContextConfig為:
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
|
package hello.config; import hello.Application; import hello.interceptor.LoginInterceptor; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer; import org.springframework.web.servlet.config.annotation.InterceptorRegistry; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; /** * */ @Configuration @ComponentScan (basePackageClasses = Application. class , useDefaultFilters = true ) public class ServletContextConfig extends WebMvcConfigurationSupport { static final private String FAVICON_URL = "/favicon.ico" ; static final private String PROPERTY_APP_ENV = "application.environment" ; static final private String PROPERTY_DEFAULT_ENV = "dev" ; /** * 發(fā)現(xiàn)如果繼承了WebMvcConfigurationSupport,則在yml中配置的相關(guān)內(nèi)容會(huì)失效。 * @param registry */ @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler( "/" ).addResourceLocations( "/**" ); registry.addResourceHandler( "/static/**" ).addResourceLocations( "classpath:/static/" ); } /** * 配置servlet處理 */ @Override public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) { configurer.enable(); } @Override public void addInterceptors(InterceptorRegistry registry) { registry.addInterceptor( new LoginInterceptor()) .addPathPatterns( "/**" ) .excludePathPatterns(FAVICON_URL) ; } } |
github地址:https://github.com/chenxing12/spring-boot-demo
本demo源碼:spring-boot-demo.rar
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:http://www.cnblogs.com/woshimrf/p/5734956.html