spring拦截器覆盖_spring boot 添加拦截器

构建一个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的配置失效,还原默认配置。那么我们需要在这个类中再次指定静态资源位置:

@Overridepublic voidaddResourceHandlers(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,然后只需要覆盖你想要拦截的位置就可以了。比如,我只是拦截访问方法之前:

packagehello.interceptor;importorg.slf4j.Logger;importorg.slf4j.LoggerFactory;importorg.springframework.web.servlet.handler.HandlerInterceptorAdapter;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;/*** Created by miaorf on 2016/8/3.*/

public class LoginInterceptor extendsHandlerInterceptorAdapter {private Logger logger = LoggerFactory.getLogger(LoginInterceptor.class);

@Overridepublic boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throwsException {

String authorization= request.getHeader("Authorization");

logger.info("The authorization is: {}",authorization);return super.preHandle(request, response, handler);

}

}

写好interceptor之后需要在开始创建的ServletContextConfig中添加这个拦截器:

@Overridepublic voidaddInterceptors(InterceptorRegistry registry) {

registry.addInterceptor(newLoginInterceptor())

.addPathPatterns("/**")

.excludePathPatterns(FAVICON_URL)

;

}

完整的ServletContextConfig为:

/** The MIT License (MIT)

*

* Copyright (c) 2014-2016 abel533@gmail.com

*

* Permission is hereby granted, free of charge, to any person obtaining a copy

* of this software and associated documentation files (the "Software"), to deal

* in the Software without restriction, including without limitation the rights

* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell

* copies of the Software, and to permit persons to whom the Software is

* furnished to do so, subject to the following conditions:

*

* The above copyright notice and this permission notice shall be included in

* all copies or substantial portions of the Software.

*

* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR

* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,

* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE

* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER

* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,

* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN

* THE SOFTWARE.*/

packagehello.config;importhello.Application;importhello.interceptor.LoginInterceptor;importorg.springframework.context.annotation.ComponentScan;importorg.springframework.context.annotation.Configuration;importorg.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;importorg.springframework.web.servlet.config.annotation.InterceptorRegistry;importorg.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;importorg.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;/****/@Configuration

@ComponentScan(basePackageClasses= Application.class, useDefaultFilters = true)public class ServletContextConfig extendsWebMvcConfigurationSupport {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中配置的相关内容会失效。

*@paramregistry*/@Overridepublic voidaddResourceHandlers(ResourceHandlerRegistry registry) {

registry.addResourceHandler("/").addResourceLocations("/**");

registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");

}/*** 配置servlet处理*/@Overridepublic voidconfigureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {

configurer.enable();

}

@Overridepublic voidaddInterceptors(InterceptorRegistry registry) {

registry.addInterceptor(newLoginInterceptor())

.addPathPatterns("/**")

.excludePathPatterns(FAVICON_URL)

;

}

}

View Code

3.AOP拦截方法

本demo源码:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值