一、創(chuàng)建SpringBoot項目
二、配置文件:
1、啟動文件InterceptortestApplication.java
成都創(chuàng)新互聯(lián)公司是專業(yè)的曲沃網(wǎng)站建設(shè)公司,曲沃接單;提供網(wǎng)站設(shè)計制作、網(wǎng)站建設(shè),網(wǎng)頁設(shè)計,網(wǎng)站設(shè)計,建網(wǎng)站,PHP網(wǎng)站建設(shè)等專業(yè)做網(wǎng)站服務(wù);采用PHP框架,可快速的進行曲沃網(wǎng)站開發(fā)網(wǎng)頁制作和功能擴展;專業(yè)做搜索引擎喜愛的網(wǎng)站,專業(yè)的做網(wǎng)站團隊,希望更多企業(yè)前來合作!
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
4.0.0
org.springframework.boot
spring-boot-starter-parent
2.1.4.RELEASE
com.jane
interceptortest
0.0.1-SNAPSHOT
interceptortest
Demo project for Spring Boot
1.8
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-test
test
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-maven-plugin
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 執(zhí)行 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,如入?yún)ⅲ? + 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/**");
}
}
三、啟動運行