SpringBoot自定义拦截器

时间:2023-01-17 08:54:29

????博客主页:????不会压弯的小飞侠
✨欢迎关注:????点赞????收藏⭐留言✒
✨系列专栏:????SpringBoot专栏
????欢迎大佬指正,一起学习!一起加油!

SpringBoot自定义拦截器


????整合SpringMVC

  • 1.先创建个SpringBoot项目,导入依赖
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.2.2.RELEASE</version>
    </parent>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
  • 创建个启动类
@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class,args);
    }

}
  • 创建实体类User
public class User {
    private String username ;
    private String password ;
    private Integer age ;
    private String sex ;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public Integer getAge() {
        return age;
    }

    public void setAge(Integer age) {
        this.age = age;
    }

    public String getSex() {
        return sex;
    }

    public void setSex(String sex) {
        this.sex = sex;
    }

    @Override
    public String toString() {
        return "User{" +
                "username='" + username + '\'' +
                ", password='" + password + '\'' +
                ", age=" + age +
                ", sex='" + sex + '\'' +
                '}';
    }
}

  • 编写Controller类
@Controller
@RequestMapping("/user")
public class UserController {
    @RequestMapping("/findAll")
    @ResponseBody
    public List<User> findAll() {
        //查询所有
        List<User> users = new ArrayList<User>();

        User user1 = new User();
        user1.setUsername("xfx");
        user1.setPassword("123456");
        user1.setAge(20);
        user1.setSex("男");

        User user2 = new User();
        user2.setUsername("mks");
        user2.setPassword("234567");
        user2.setAge(34);
        user2.setSex("女");

        User user3 = new User();
        user3.setUsername("ls");
        user3.setPassword("34567");
        user3.setAge(34);
        user3.setSex("女");

        users.add(user1);
        users.add(user2);
        users.add(user3);

        return users;
    }
}
  • 测试

SpringBoot自定义拦截器

????静态资源目录

  • 在springboot中有一个叫做ResourceProperties的类,里面就定义了静态资源的默认查找路径:
  • 默认的静态资源路径为:
    • classpath:/META-INF/resources/
    • classpath:/resources/
    • classpath:/static/
    • classpath:/public
  • 在项目中创建路径进行测试
  • 在static目录下创建index.html文件
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>static目录下的静态页面</title>
</head>
<body>
<h1>static目录下的静态页面</h1>

</body>
</html>
  • 浏览器输入 : http://localhost:8080/index.html进行测试

SpringBoot自定义拦截器

  • 在public目录下创建public.html文件
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>public目录下的静态页面</title>
</head>
<body>
<h1>public目录下的静态页面</h1>
</body>
</html>
  • 浏览器输入 : http://localhost:8080/public.html进行测试

SpringBoot自定义拦截器

????自定义拦截器

  • SpringMVC中配置拦截器的步骤 :
    • 编写一个拦截器(实现HandlerInterceptor接口)
    • 注册拦截器(mvc:interceptors)
  • SpringBoot实现拦截器的具体步骤:
    • 编写一个拦截器
@Component
public class MyInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        System.out.println("preHandle方法执行....");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
        System.out.println("postHandle方法执行....");
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
        System.out.println("afterCompletion方法执行....");
    }
}

  • 通过WebMvcConfigurer注册拦截器
@Configuration
public class MVCConfig implements WebMvcConfigurer {
    @Autowired
    private MyInterceptor myInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(myInterceptor)
                .addPathPatterns("/user/findAll")
                .addPathPatterns("/public.html")
                .excludePathPatterns("/index.html");
    }
}

  • 测试拦截效果

SpringBoot自定义拦截器
SpringBoot自定义拦截器