SpringBoot拦截器Filter的使用方法详解

2020-03-13 18:02:43王冬梅

3.添加测试控制器

package top.ytheng.demo.controller;

import java.util.HashMap;
import java.util.Map;

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

@RestController
@RequestMapping("/api/v1/filter")
public class FilterController {

  @RequestMapping("/test")
  public Object testFilter() {
    Map<String, Object> map = new HashMap<>();
    map.put("name", "theng");
    map.put("pwd", "123456");
    return map;
  }
}

4.添加启动类

package top.ytheng.demo;

import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.ServletComponentScan;

@SpringBootApplication //等于下面3个
//@SpringBootConfiguration
//@EnableAutoConfiguration
//@ComponentScan
//拦截器用到
@ServletComponentScan
public class DemoApplication {

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

5.添加拦截后调整的页面filter.html

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
  <h4>hello theng</h4>
  <h3>filter success</h3>
</body>
</html>

6.右键项目Run As启动项目,测试地址

http://localhost:8080/api/v1/filter/test?username=theng
http://localhost:8080/api/v1/filter/test?username=ytheng

另附:

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持易采站长站。