spring boot如何添加拦截器
程序员文章站
2024-03-02 19:01:46
构建一个spring boot项目。
添加拦截器需要添加一个configuration
@configuration
@componentscan(basep...
构建一个spring boot项目。
添加拦截器需要添加一个configuration
@configuration @componentscan(basepackageclasses = application.class, usedefaultfilters = true) public class servletcontextconfig extends webmvcconfigurationsupport {
为了方便扫描位置,我们可以写一个接口或者入口类application放置于最外一层的包内,这样就会扫描该类以及子包的类。
1 resources配置
在没有配置这个类的时候,我们可以在application.ym中修改静态文件位置和匹配方式:
#指定环境配置文件 spring: profiles: active: dev # 修改默认静态路径,默认为/**,当配置hello.config.servletcontextconfig后此处配置失效 mvc: static-path-pattern: /static/**
但当我们继承了webmvcconfigurationsupport 并配置扫描后,上述resources的配置失效,还原默认配置。那么我们需要在这个类中再次指定静态资源位置:
@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,然后只需要覆盖你想要拦截的位置就可以了。比如,我只是拦截访问方法之前:
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中添加这个拦截器:
@override public void addinterceptors(interceptorregistry registry) { registry.addinterceptor(new logininterceptor()) .addpathpatterns("/**") .excludepathpatterns(favicon_url) ; }
完整的servletcontextconfig为:
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地址:
本demo源码:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。