springboot拦截器拦截提示_SpringBoot拦截器

在实际开发中,总存在着这样的场景,比如拦截请求的ip地址,或者在所有的请求都返回相同的数据,如果每一个方法都写出相同数据固然可以实现,但是随着项目的变大,重复的代码会越来越多,所以在这种情况我们可以用拦截器来实现。

最近一直在研究thymeleaf,越发的感觉这个很好用,所以这篇文章也选择结合这个来使用。

新建项目,pom文件如下:

xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

4.0.0

com.dalaoyang

springboot_interceptor

0.0.1-SNAPSHOT

jar

springboot_interceptor

springboot_interceptor

org.springframework.boot

spring-boot-starter-parent

1.5.9.RELEASE

UTF-8

UTF-8

1.8

org.springframework.boot

spring-boot-starter-thymeleaf

org.springframework.boot

spring-boot-starter-web

org.springframework.boot

spring-boot-devtools

runtime

org.springframework.boot

spring-boot-starter-test

test

net.sourceforge.nekohtml

nekohtml

1.9.15

org.springframework.boot

spring-boot-maven-plugin

新建一个拦截器CommonInterceptor,继承HandlerInterceptorAdapter。给大家说一下,在继承HandlerInterceptorAdapter有三个拦截器是经常使用的:

1.preHandle在业务处理器处理请求之前被调用

2.postHandle在业务处理器处理请求执行完成后,生成视图之前执行

3.afterCompletion在DispatcherServlet完全处理完请求后被调用

本文使用的是postHandle,代码如下:

package com.dalaoyang.interceptor;

import org.apache.log4j.Logger;

import org.springframework.stereotype.Component;

import org.springframework.ui.ModelMap;

import org.springframework.web.servlet.ModelAndView;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

/**

* @author dalaoyang

* @Description

* @project springboot_learn

* @package com.dalaoyang.interceptor

* @email yangyang@dalaoyang.cn

* @date 2018/4/27

*/

@Component

public class CommonInterceptor extends HandlerInterceptorAdapter {

Logger logger = Logger.getLogger(CommonInterceptor.class);

//preHandle在业务处理器处理请求之前被调用,

//postHandle在业务处理器处理请求执行完成后,生成视图之前执行

//afterCompletion在DispatcherServlet完全处理完请求后被调用

public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,

ModelAndView modelAndView) throws Exception {

logger.info("请求ip:"+request.getRemoteAddr());

logger.info("请求的方法:"+request.getMethod());

ModelMap modelMap = modelAndView.getModelMap();

modelMap.addAttribute("title","dalaoyang");

}

}

在启动类继承WebMvcConfigurerAdapter来为项目添加拦截器,代码如下:

package com.dalaoyang;

import com.dalaoyang.interceptor.CommonInterceptor;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.boot.SpringApplication;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.web.servlet.config.annotation.InterceptorRegistry;

import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

@SpringBootApplication

public class SpringbootInterceptorApplication extends WebMvcConfigurerAdapter {

public static void main(String[] args) {

SpringApplication.run(SpringbootInterceptorApplication.class, args);

}

@Autowired

CommonInterceptor commonInterceptor;

// 增加拦截器

@Override

public void addInterceptors(InterceptorRegistry registry) {

registry.addInterceptor(commonInterceptor);

}

}

IndexController负责跳转,代码如下:

package com.dalaoyang.controller;

import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;

import org.springframework.web.bind.annotation.RequestMapping;

/**

* @author dalaoyang

* @Description

* @project springboot_learn

* @package com.dalaaoyang.controller

* @email yangyang@dalaoyang.cn

* @date 2018/4/27

*/

@Controller

public class IndexController {

@RequestMapping("/")

public String index(Model model){

model.addAttribute("content","hi , dalaoyang !");

return "index";

}

}

在templates下新建index.html,其中content是controller返回的内容,title是在拦截器中返回的内容,代码如下:

在看一下页面:

源码下载 :大老杨码云


版权声明:本文为weixin_42499919原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。