构建一个spring boot项目。
添加拦截器需要添加一个configuration
1
2
3
|
@Configuration @ComponentScan (basePackageClasses = Application. class , useDefaultFilters = true ) public class ServletContextConfig extends WebMvcConfigurationSupport { |
为了方便扫描位置,我们可以写一个接口或者入口类Application放置于最外一层的包内,这样就会扫描该类以及子包的类。
1 resources配置
在没有配置这个类的时候,我们可以在application.ym中修改静态文件位置和匹配方式:
1
2
3
4
5
6
7
|
#指定环境配置文件 spring: profiles: active: dev # 修改默认静态路径,默认为/**,当配置hello.config.ServletContextConfig后此处配置失效 mvc: static-path-pattern: /static/** |
但当我们继承了WebMvcConfigurationSupport 并配置扫描后,上述resources的配置失效,还原默认配置。那么我们需要在这个类中再次指定静态资源位置:
1
2
3
4
5
|
@Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler( "/" ).addResourceLocations( "/**" ); registry.addResourceHandler( "/static/**" ).addResourceLocations( "classpath:/static/" ); } |
这样访问classpath下的static包下的静态资源的url匹配为/static/xxx.js。默认匹配static下的静态文件url为/xxx.js,虽然清洁,但我感觉idea不会识别这种路径,还是改成完整的路径比较好。
2.Interceptor配置
配置登录拦截或者别的。需要创建一个拦截器类来继承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之后需要在开始创建的ServletContextConfig中添加这个拦截器:
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" ; /** * 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 * @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
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/woshimrf/p/5734956.html