spring boot如何添加攔截器
構(gòu)建一個(gè)spring boot項(xiàng)目。
添加攔截器需要添加一個(gè)configuration
@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)文件位置和匹配方式:
#指定環(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)資源位置:
@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,然后只需要覆蓋你想要攔截的位置就可以了。比如,我只是攔截訪問方法之前:
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è)攔截器:
@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"; /** * 發(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_jb51.rar
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Java線程啟動(dòng)為什么要用start()而不是run()?
這篇文章主要介紹了線程啟動(dòng)為什么要用start()而不是run()?下面文章圍繞start()與run()的相關(guān)資料展開詳細(xì)內(nèi)容,具有一定的參考價(jià)值,西藥的小火熬版可以參考一下,希望對(duì)你有所幫助2021-12-12如何在SpringBoot項(xiàng)目里進(jìn)行統(tǒng)一異常處理
這篇文章主要介紹了如何在SpringBoot項(xiàng)目里進(jìn)行統(tǒng)一異常處理,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值。需要的小伙伴可以參考一下2022-07-07SpringBoot整合SQLite數(shù)據(jù)庫(kù)全過程
sqlite是一個(gè)很輕量級(jí)的數(shù)據(jù)庫(kù),可以滿足日常sql的需求,下面這篇文章主要給大家介紹了關(guān)于SpringBoot整合SQLite數(shù)據(jù)庫(kù)的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下2023-03-03java使用監(jiān)聽器實(shí)現(xiàn)一個(gè)統(tǒng)計(jì)網(wǎng)站在線人數(shù)的示例
本文主要介紹了java使用監(jiān)聽器實(shí)現(xiàn)一個(gè)統(tǒng)計(jì)網(wǎng)站在線人數(shù)的示例,具有一定的參考價(jià)值,有需要的朋友可以了解一下。2016-10-10學(xué)習(xí)Spring-Session+Redis實(shí)現(xiàn)session共享的方法
本篇文章主要介紹了學(xué)習(xí)Spring-Session+Redis實(shí)現(xiàn)session共享的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-05-05SpringBoot利用ThreadPoolTaskExecutor批量插入百萬級(jí)數(shù)據(jù)
在處理大量數(shù)據(jù)時(shí),為了提高效率和性能,通常需要采用批量插入的方式,本文主要介紹了SpringBoot利用ThreadPoolTaskExecutor批量插入百萬級(jí)數(shù)據(jù),具有一定的參考價(jià)值,感興趣的可以了解一下2024-03-03