@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter { public static void main(String[] args) {
//String[] arss= environment.getActiveProfiles();
SpringApplication.run(Application.class, args); } @Bean
public HandlerInterceptor getMyInterceptor(){
return new MyInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 多个拦截器组成一个拦截器链
// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用户排除拦截
registry.addInterceptor(getMyInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}
package myboot.example.common;

import org.apache.naming.factory.BeanFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; /**
* Created by cxlings on 2016/7/24.
*/
public class MyInterceptor implements HandlerInterceptor { private static final Logger LOGGER = LoggerFactory.getLogger(MyInterceptor.class); @Autowired
private Environment env; public boolean preHandle(HttpServletRequest request,
HttpServletResponse response, Object handler) throws Exception {
String ss[]=env.getActiveProfiles(); for(String a :ss){
LOGGER.info("aaa="+a);
}
String url = request.getRequestURL().toString();
String id = "2143";// request.getParameter("id");
System.out.printf(" springurl=" + url + "id=" + id);
return true;
} public void postHandle(HttpServletRequest request,
HttpServletResponse response, Object handler,
ModelAndView modelAndView) throws Exception {
} public void afterCompletion(HttpServletRequest request,
HttpServletResponse response, Object handler, Exception ex)
throws Exception { } }

运行启动的命令

 java -jar myboot-1.0-SNAPSHOT.jar --spring.profiles.active=product

参考地址

http://www.bkjia.com/Javabc/1096806.html

https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-external-config.html
@SpringBootApplication
public class Application extends WebMvcConfigurerAdapter { public static void main(String[] args) {
//String[] arss= environment.getActiveProfiles();
SpringApplication.run(Application.class, args); } @Bean
public HandlerInterceptor getMyInterceptor(){
return new MyInterceptor();
}
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 多个拦截器组成一个拦截器链
// addPathPatterns 用于添加拦截规则
// excludePathPatterns 用户排除拦截
registry.addInterceptor(getMyInterceptor()).addPathPatterns("/**");
super.addInterceptors(registry);
}

spring boot 拦截器的更多相关文章

  1. Spring boot拦截器的实现

    Spring boot拦截器的实现 Spring boot自带HandlerInterceptor,可通过继承它来实现拦截功能,其的功能跟过滤器类似,但是提供更精细的的控制能力. 1.注册拦截器 @C ...

  2. 【spring boot】spring boot 拦截器

    今日份代码: 1.定义拦截器 import com.alibaba.fastjson.JSON; import org.apache.commons.collections.CollectionUti ...

  3. spring boot拦截器配置

    1.在spring boot配置文件application.properties中添加要拦截的链接 com.url.interceptor=/user/test 2.编写拦截器代码 ,创建UrlInt ...

  4. spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案(转)

    文章转自 http://blog.51cto.com/12066352/2093750 最近项目采用spring icloud,用的spring boot版本是1.5.x的,spring boot 2 ...

  5. spring boot拦截器WebMvcConfigurerAdapter,以及高版本的替换方案

    Springboot中静态资源和拦截器处理(踩了坑)   背景: 在项目中我使用了自定义的Filter 这时候过滤了很多路径,当然对静态资源我是直接放过去的,但是,还是出现了静态资源没办法访问到spr ...

  6. spring boot拦截器

    实现自定义拦截器只需要3步: 1.创建我们自己的拦截器类并实现 HandlerInterceptor 接口. 2.创建一个Java类继承WebMvcConfigurerAdapter,并重写 addI ...

  7. (22)Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】

    上一篇对过滤器的定义做了说明,也比较简单.过滤器属于Servlet范畴的API,与Spring 没什么关系.     Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Sprin ...

  8. Spring boot 拦截器和过滤器

    1. 过滤器 Filter介绍 Filter可以认为是Servlet的一种“加强版”,是对Servlet的扩展(既可以对请求进行预处理,又可以对处理结果进行后续处理.使用Filter完整的一般流程是: ...

  9. 九、 Spring Boot 拦截器

    过滤器属于Servlet范畴的API,与spring 没什么关系. Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Spring提供的HandlerInterceptor(拦截 ...

随机推荐

  1. JQuery快速入门

    Write less, do more, I like jQuery. jQuery是最常用的js库,整体来说非常轻量并易于扩展,对于移动应用可以使用其更轻量的孪生兄弟Zepto代替.其是由John ...

  2. 代码大全 MSIL语言程序设计

    .NET平台的编译器会将高级语言(C#,VB.NET,F#)编译成MSIL(微软中间语言)格式.熟悉MSIL语言,可以读懂一些加密程序混淆过的算法,这些算法几乎不能还原成高级语言,但是可以还原成MSI ...

  3. Requirejs加载超时问题的一个解决方法:设置waitSeconds=0

    有时Requirejs会遇到加载js超时问题 除了排查js脚本问题,网络问题以外的一个解决方法是加大Require的等待时间waitSeconds,或者直接设置为0,这个参数的意义是:The numb ...

  4. Ubuntu 汉化及kate汉化和使用自带终端的解决方式

    汉化方法:1,打开屏幕上方的“system”(系统),里面有个“Administration”(系统管理),选择“Snaptic Package Manager”(软件包管理器),打开,输入密码后进入 ...

  5. SVO实时全局光照:Sparse Voxel Octree based Global Illumination (SVO GI)

    功能已实现,初步集成进来,暂未进行重度优化.但GI的效果已很明显.这里特地给出了开启实时GI前后的效果对比,比对场景如下:1.只有直接光照(方向光源)的场景.2在直接光照(方向光源)基础上开启了实时G ...

  6. Windows 7 中设置VPN(PPTP连接方式)

    第一步:打开控制面板-网络和共享中心(如图) 第二步:点击-设置新的连接或网络(如图) 第三步:选择-连接到工作区-下一步(如图) 第四步:如果已经存在其他连接,则在这一步选择”否,创建新连接“:如果 ...

  7. 解决Visual Studio 调试时候不能直接访问.json后缀文件的问题

    Web.config中作如下配置: <system.webServer> <staticContent> <mimeMap fileExtension=".js ...

  8. 寻找倒数第K个结点

    #include<stdio.h> #include<iostream> using namespace std; /** * 找到链表中的倒数第k个节点 */ //定义结构体 ...

  9. ATT 解锁手机

    最近豪们都在忙着买买买950 本土鳖左瞅瞅右看看实在钱包不够豪 正好看到美帝640打折39刀 (http://www.microsoftstore.com/store/msusa/en_US/pdp/ ...

  10. [emacs org-mode小技巧] org-indent-mode 让文档更容易阅读

    刚发现Emacs的org-mode里面一个叫做 org-indent-mode 的minor mode对于阅读org-mode文档很不错,版面看起来清晰多了: 从上图可以看到,org-indent-m ...