最近重構公司項目發現以前項目中有不少的攔截配置,用的大部分都是shiro進行攔截,將項目升級成boot項目以後試着shiro的方式半天,後來發現boot項目自帶攔截,有點撿來芝麻丟了西瓜的感受,好了廢話很少說之間上代碼:
一、繼承 WebMvcConfigurerAdapter 實現方法
WebMvcConfigurerAdapter配置類實際上是Spring內部的一種配置方式,採用JavaBean的形式來代替傳統的xml配置文件形式進行鍼對框架個性化定製,Spring 5.0 之後WebMvcConfigurerAdapter會取消掉
如下WebMvcConfigurerAdapter 比較經常使用的重寫接口
/** 解決跨域問題 **/ public void addCorsMappings(CorsRegistry registry) ;
/** 添加攔截器 **/ void addInterceptors(InterceptorRegistry registry);
/** 這裏配置視圖解析器 **/ void configureViewResolvers(ViewResolverRegistry registry);
/** 配置內容裁決的一些選項 **/ void configureContentNegotiation(ContentNegotiationConfigurer configurer);
/** 視圖跳轉控制器 **/ void addViewControllers(ViewControllerRegistry registry);
/** 靜態資源處理 **/ void addResourceHandlers(ResourceHandlerRegistry registry);
/** 默認靜態資源處理器 **/ void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer);html
新的版本解決方案目前有兩種:
方案1 直接實現WebMvcConfigurer
@Configuration
public class WebMvcConfg implements WebMvcConfigurer {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("index");
}
}
方案2 直接繼承WebMvcConfigurationSupport
@Configuration
public class WebMvcConfg extends WebMvcConfigurationSupport {
@Override
public void addViewControllers(ViewControllerRegistry registry) {
registry.addViewController("/index").setViewName("index");
}
}
我的推薦使用第一種
/**
* 跨域配置 容許因此請求進來,可根據需求配置
* @param registry
*/
@Override
public void addCorsMappings(CorsRegistry registry) {
registry.addMapping("/**").allowedHeaders("*")
.allowedMethods("*")
.allowedOrigins("*")
.maxAge(3628800)
.allowCredentials(true);
}
/**
* 攔截器配置 攔截所須要攔截的請求,走addInterceptor內new的方法校驗
* @param registry
*/
@Override
public void addInterceptors(InterceptorRegistry registry){
if(!devWithoutInterception) {
registry.addInterceptor(new UserInterceptor())
.addPathPatterns("/user/*.do", "/order/*.do")
.excludePathPatterns("/common/*.do", "/wechat/*.do");
registry.addInterceptor(new WebRequestInterceptor())
.addPathPatterns("/html/*.do")
.excludePathPatterns("/common/*.do");
}
}
/**
* FilterRegistrationBean
* 用來配置urlpattern
* 來肯定哪些路徑觸發filter
* order 順序
*/
@Bean
public FilterRegistrationBean someFilterRegistration() {
FilterRegistrationBean registration = new FilterRegistrationBean();
registration.setFilter(new DecryptFilter());
registration.addUrlPatterns("/user/*","/order/*");
registration.setOrder(1);
return registration;
}
/**
* 配置微信訪問攔截
* @return
*/
@Bean
public ServletRegistrationBean indexServletRegistration() {
ServletRegistrationBean registration = new ServletRegistrationBean(new AccessTokenServlet());
registration.addUrlMappings("/AccessTokenServlet");
registration.addInitParameter("appId",wxappid);
registration.addInitParameter("appSecret",wxappSecret);
return registration;
}
/**
* 在Springboot程序啓動後,會默認添加OrderedCharacterEncodingFilter和HiddenHttpMethodFilter過濾器。
* 在HiddenHttpMethodFilter過濾器中會調用request.getParameter(),
* 從而形成咱們在controller中經過request的InputStream沒法讀取到RequestBody的數據
* @return
*/
@Bean
public HiddenHttpMethodFilter hiddenHttpMethodFilter() {
return new OrderedHiddenHttpMethodFilter(){
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
throws ServletException, IOException {
filterChain.doFilter(request, response);
}
};
}
以上部分原創其他則是參考別人敬請諒解。跨域
---------------------
做者:yanlingkong741
來源:CSDN
原文:https://blog.csdn.net/yanlingkong741/article/details/83068593?utm_source=copy
版權聲明:本文爲博主原創文章,轉載請附上博文連接!微信