栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java > SpringBoot

SpringBoot拦截器

SpringBoot 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

SpringBoot拦截器

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

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

新建项目,pom文件如下:



    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;


@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;



@Controller
public class IndexController {

    @RequestMapping("/")
    public String index(Model model){
 model.addAttribute("content","hi , dalaoyang !");
 return "index";
    }
}

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





    
    


启动项目,访问http://localhost:8888/,控制台如下:

在看一下页面:

源码下载 :大老杨码云

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/235330.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号