码迷,mamicode.com
首页 > 编程语言 > 详细

Idea基于springBoot的拦截器的简单实现

时间:2019-05-14 00:38:03      阅读:117      评论:0      收藏:0      [点我收藏+]

标签:parent   framework   boot   inter   pat   map   string   end   start   

一、创建SpringBoot项目
二、配置文件:
1、启动文件InterceptortestApplication.java

package com.jane.interceptortest;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

@SpringBootApplication
@ComponentScan("com.jane")
public class InterceptortestApplication {

    public static void main(String[] args) {
        SpringApplication.run(InterceptortestApplication.class, args);
    }

}

2、属性文件,applicaiton.properties

server.port=6009

3、依赖文件,pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.4.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.jane</groupId>
    <artifactId>interceptortest</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>interceptortest</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

4、控制器,TestController

package com.jane.interceptortest.controller;

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

@RestController
@RequestMapping("/job")
public class TestController {

    @RequestMapping("/test")
    public String test() {
        System.out.println("RUL 执行 test");
        return "test";
    }

}

5、拦截器TestInterceptor

package com.jane.interceptortest.interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Configuration
public class TestInterceptor implements HandlerInterceptor {

    public TestInterceptor(){}

    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
        String queryString = request.getQueryString();
        String requestURL = request.getRequestURL().toString();
        System.out.println("拦截器1,如入参:" + queryString + "访问地址," + requestURL);
        return true;
    }

    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) {
        System.out.println("拦截器2");
    }

    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)  {
        System.out.println("拦截器3");
    }

}

6、注册拦截器

package com.jane.interceptortest.interceptor;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class WebMvcConfg extends WebMvcConfigurationSupport {

    @Autowired
    private TestInterceptor interceptorConfig;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        //注册自定义拦截器,添加拦截路径和排除拦截路径
        registry.addInterceptor(interceptorConfig).addPathPatterns("/job/**");
    }

}

三、启动运行
技术图片
技术图片

Idea基于springBoot的拦截器的简单实现

标签:parent   framework   boot   inter   pat   map   string   end   start   

原文地址:https://blog.51cto.com/janephp/2394004

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!