springboot配置拦截器使用方法实例详解

  

本文介绍Spring-Boot中使用拦截器,一般在拦截器中处理跨域处理,允许跨域访问项目,拦截器使用详细资料请查阅官网。

  

<>强实现自定义拦截器步骤:

  

1,创建一个类并实现HandlerInterceptor接口。

  

2,创建一个Java类继承WebMvcConfigurerAdapter,并重写addInterceptors方法。

  

2,将自定义的拦截器交由弹簧管理,然后将对像手动添加到拦截器链中(在addInterceptors方法中添加)。

  

<>强创建拦截器类

        包com.example.springboot.config.intercepter;   进口org.springframework.stereotype.Component;   进口org.springframework.web.servlet.HandlerInterceptor;   进口org.springframework.web.servlet.ModelAndView;   进口javax.servlet.http.HttpServletRequest;   进口javax.servlet.http.HttpServletResponse;/* *   * @desc自定义拦截器,使用@ component让弹簧管理其生命周期   * @Author wangsh   * @date 2018/5/6 17:06   * @return   */@ component   公共类TestIntercepter实现HandlerInterceptor {   @Override   公共布尔preHandle (HttpServletRequest请求,HttpServletResponse响应对象处理程序){抛出异常   System.out.println(“,,,在的在的在preHandle>的在在在在在在在请求处理之前进行调用(控制器方法调用之前)");   setCrossDomain(响应);   返回true;//只有返回真实才会继续向下执行,返回假取消当前请求   }   @Override   公共空白postHandle (HttpServletRequest请求,HttpServletResponse响应对象处理程序,ModelAndView ModelAndView){抛出异常//请求处理之后进行调用,但是在视图被渲染之前(控制器方法调用之后);   System.out.println(“,,,在的在的在postHandle>的在在在在在在请求处理之后进行调用,但是在视图被渲染之前(控制器方法调用之后)");   }   @Override   公共空间afterCompletion (HttpServletRequest请求,HttpServletResponse响应、对象处理程序异常交货){抛出异常//在整个请求结束之后被调用,也就是在DispatcherServlet渲染了对应的视图之后执行(主要是用于进行资源清理工作);   System.out.println(“,,,在的在的在postHandle>的在在在在在在在整个请求结束之后被调用,也就是在DispatcherServlet渲染了对应的视图之后执行(主要是用于进行资源清理工作)");   }/* *   * @param @param反应设定文件   * @return空返回类型   * @throws   * @Title: setCrossDomain   * @Description:待办事项(设置跨域问题)   */私人空间setCrossDomain (HttpServletResponse响应){   响应。addHeader (“Access-Control-Allow-Origin”、“*”);   响应。addHeader (“Access-Control-Allow-Methods”、“POST、GET”);   响应。addHeader (“Access-Control-Allow-Credentials”,“真正的”);   }   }      

将自定义拦截器添加到拦截器链中

        包com.example.springboot.config.config;   进口com.example.springboot.config.intercepter.TestIntercepter;   进口org.springframework.beans.factory.annotation.Autowired;   进口org.springframework.context.annotation.Configuration;   进口org.springframework.web.servlet.config.annotation.InterceptorRegistry;   进口org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;/* *   *注册豆   */@ configuration   公开课InterceptorConfig延伸WebMvcConfigurerAdapter {   @ autowired   私人TestIntercepter testInterceptor;   @Override   公共空间addInterceptors (InterceptorRegistry注册表){   registry.addInterceptor (testInterceptor);   }   }      

创建控制器类

        包com.example.springboot.config.conroller;   进口org.springframework.web.bind.annotation.RequestMapping;   进口org.springframework.web.bind.annotation.RestController;   @RestController   @RequestMapping(“/用户”)   公开课用户控件{   @RequestMapping (“/hello”)   公共字符串hello () {   返回“你好”;   }   }      

创建启动服务类

        包com.example.springboot.config;   进口org.springframework.boot.SpringApplication;   进口org.springframework.boot.autoconfigure.SpringBootApplication;   @SpringBootApplication   公开课SpringbootConfigApplication {   公共静态void main (String [] args) {   SpringApplication.run (SpringbootConfigApplication.class, args);   }   }      

启动服务测试,在浏览器访问http://localhost: 8088/你好,打印日志如下,可以看出经过了拦截器。

springboot配置拦截器使用方法实例详解