springmvc中拦截器的概念已经被弱化了, springboot中使用的也不甚广泛, 通常在用户登录等方面仍有用处

创建拦截器步骤:

, 创建拦截器类继承HandlerInterceptor
, 创建java类继承WebMvcConfigurerAdapter, 重写addInterceptors()方法
, 在addInterceptor中添加自己的拦截器组成拦截器链

1, 对请求的拦截器类:

package com.iwhere.test.web.intercepter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; /**
* springboot 配置intercepter
* @author wenbronk
* @time 2017年3月17日 下午4:58:58 2017
*/ @Component
public class MyIntercepter implements HandlerInterceptor { @Override
public void afterCompletion(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, Exception arg3)
throws Exception {
System.out.println(">>>MyInterceptor1>>>>>>>在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行(主要是用于进行资源清理工作)");
} @Override
public void postHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2, ModelAndView arg3)
throws Exception {
System.out.println(">>>MyInterceptor1>>>>>>>请求处理之后进行调用,但是在视图被渲染之前(Controller方法调用之后)");
} @Override
public boolean preHandle(HttpServletRequest arg0, HttpServletResponse arg1, Object arg2) throws Exception {
System.out.println(">>>MyInterceptor1>>>>>>>在请求处理之前进行调用(Controller方法调用之前)");
return true;
} }

2, 注册拦截器

package com.iwhere.test.web.intercepter;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter; /**
* 在此类中配置拦截器, 可添加拦截器栈
* @author wenbronk
* @time 2017年3月17日 下午5:01:38 2017
*/ @SpringBootConfiguration
public class MyWebMvcConfigureAdapter extends WebMvcConfigurerAdapter { /**
* 此方法中添加连接器
*/
@Override
public void addInterceptors(InterceptorRegistry registry) {
super.addInterceptors(registry); registry.addInterceptor(new MyIntercepter()).addPathPatterns("/**");
// 添加多个拦截器 , 组成拦截器栈
// registry.addInterceptor(new MyIntercepter2()).addPathPatterns("/intercepter/pre");
}
}

然后, 进行项目访问, 既可以进行拦截. . .

3. 对方法的拦截

import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component; @Aspect
@Component
public class EnhanceMonitor { //定义切点
@Pointcut("execution(* com.peilian.*..attack(..))")
public void pointCutMethod(){} //前置
@Before("pointCutMethod()")
public void magicEnhance1(){
System.out.println("----冰霜特效----");
} //前置
@Before("pointCutMethod()")
public void magicEnhance2(){
System.out.println("----火焰特效----");
} //后置
@After("pointCutMethod()")
public void afterAttack(){
System.out.println("---攻击后僵直---");
} /*环绕
@Around("pointCutMethod()")
public void buffAttack(ProceedingJoinPoint pjp) throws Throwable {
System.out.println("----骑士光环---");
pjp.proceed();
System.out.println("-----骑士光环消失----");
}*/
}

springboot-12-自定义拦截器的配置interceptor的更多相关文章

  1. struts2框架之自定义拦截器和配置

    struts框架中也存在拦截器,只不过系统自动调用.框架自带的拦截器的配置文件所在的位置为: java Resources--->Libraries--->struts2-core-2.3 ...

  2. 【学习】SpringBoot之自定义拦截器

    /** * 自定义拦截器 **/ @Configuration//声明这是一个拦截器 public class MyInterceptor extends WebMvcConfigurerAdapte ...

  3. SpringBoot之自定义拦截器

    一.自定义拦截器实现步骤 1.创建拦截器类并实现HandlerInterceptor接口 2.创建SpringMVC自定义配置类,实现WebMvcConfigurer接口中addInterceptor ...

  4. springboot实现自定义拦截器

    为了更容易理解,我们通过一个代码例子来演示. 例子: 我们现在要访问http://localhost:8080/main.html页面,这个页面需要登录之后才能够浏览,没登录不能浏览. 那么现在问题来 ...

  5. 12.Struts2自定义拦截器

    12.自定义拦截器        拦截器是Struts2的一个重要特性.因为Struts2的大多数核心功能都是通过拦截器实现的. 拦截器之所以称之为“拦截器”,是因为它可以拦截Action方法的执行, ...

  6. SpringMVC拦截器与SpringBoot自定义拦截器

    首先我们先回顾一下传统拦截器的写法: 第一步创建一个类实现HandlerInterceptor接口,重写接口的方法. 第二步在XML中进行如下配置,就可以实现自定义拦截器了 SpringBoot实现自 ...

  7. Struts2透过自定义拦截器实现登录之后跳转到原页面

    Struts2通过自定义拦截器实现登录之后跳转到原页面 这个功能对用户体验来说是非常重要的.实现起来其实很简单. 拦截器的代码如下: package go.derek.advice; import g ...

  8. 2018.12.17 struts.xml 配置自定义拦截器配置

    自定义拦截器有三个步骤哦 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE struts PU ...

  9. SpringBoot2.X自定义拦截器实战及新旧配置对比(核心知识)

    简介: 讲解拦截器使用,Spingboot2.x新版本配置拦截拦截器和旧版本SpringBoot配置拦截器区别讲解 1.@Configuration 继承WebMvcConfigurationAdap ...

随机推荐

  1. 四、创建覆盖网络--Flannel

      Flannel是 CoreOS 团队针对 Kubernetes 设计的一个覆盖网络(Overlay Network)工具,其目的在于帮助每一个使用 Kuberentes 的 CoreOS 主机拥有 ...

  2. Android实现带下划线的EditText(BUG修正)

    之前写了一个关于实现EditText显示下划线的例子,发现仍然存在一些问题,在此继续探索,原文链接:http://www.cnblogs.com/ayqy/p/3599414.html (零)另一个b ...

  3. Microsoft Sql Server 2016安装在CentOS7下

    安装过程 如何安装直接参考这个文章:安装sql server 整个安装过程非常简单. 上面的文档里是通过 sudo 命令,用root身份来执行,不过这里为了简单,就用root账号来安装的. (1)下载 ...

  4. c# HashSet 列表去重

    List<, , , }; HashSet<int> hs = new HashSet<int>(list); List<, , , }; HashSet<i ...

  5. C#模拟请求,模拟登录,Cookie设置、文件上传等问题汇总

    由于业务需求,最近需要模拟完成登陆某个网站,并上传所需要的文件.在开发途中,遇到了很多问题,现在,就我遇到的一些问题及解决办法说明如下,希望对遇到同样问题的人有所帮助.因为技术有限,可能有些内容并不完 ...

  6. JQuery - Ajax和Tomcat跨域请求问题解决方法!

    在JQuery里面使用Ajax和Tomcat服务器之间进行数据交互,遇到了跨域请求问题,无法成功得到想要的数据! 错误信息部分截图: 通过错误信息判断知道已经发生在Ajax跨域请求问题了! 当前Tom ...

  7. 670. Maximum Swap

    Given a non-negative integer, you could swap two digits at most once to get the maximum valued numbe ...

  8. Brainteaser-292. Nim Game

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  9. sublime 把 tab 转成 4 个空格

    Preferences -> Settings-User {    "tab_size":4,    "translate_tabs_to_spaces" ...

  10. Web安全学习图径——系列课程推荐

    本文作者:i春秋作家 大哥哥团长 说到Web安全必须要了解Web方面的一些基础知识做为铺垫的去的去学习这门技术,因为不是人人都可以直接先渗透在进行编程等方面学习的.所以为了更好的入门的Web安全必须要 ...