当前位置:   article > 正文

Spring Boot笔记-拦截器相关(用户权限方面)_interceptor拦截器初步赋子不同类型用户权限

interceptor拦截器初步赋子不同类型用户权限

Spring Boot拦截器的使用

使用注解@Configuration

继承WebMvcConfigurerAdapter

重写addInterceptors添加需要拦截器地址:

 

运行截图如下:

后端拦截如下:

这里主要是依靠注册拦截器实现的

这里一个拦截器可以进行多个链接过滤,只需在后面加入addPathPatterns即可

程序结构如下:

WebMvcConfig.java

  1. package com.example.demo.config;
  2. import com.example.demo.interceptor.OneInterceptor;
  3. import com.example.demo.interceptor.TwoInterceptor;
  4. import org.springframework.context.annotation.Configuration;
  5. import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
  6. import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;
  7. @Configuration
  8. public class WebMvcConfig extends WebMvcConfigurerAdapter {
  9. @Override
  10. public void addInterceptors(InterceptorRegistry registry) {
  11. registry.addInterceptor(new OneInterceptor()).addPathPatterns("/one/**");
  12. registry.addInterceptor(new TwoInterceptor()).addPathPatterns("/two/**");
  13. registry.addInterceptor(new OneInterceptor()).addPathPatterns("/three/**");
  14. registry.addInterceptor(new TwoInterceptor()).addPathPatterns("/three/**");
  15. super.addInterceptors(registry);
  16. }
  17. }

OneController.java

  1. package com.example.demo.interceptor;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. @Controller
  5. @RequestMapping("one")
  6. public class OneController {
  7. @RequestMapping("/index")
  8. public String index(){
  9. return "index";
  10. }
  11. @RequestMapping("/index2")
  12. public String index2(){
  13. return "index2";
  14. }
  15. @RequestMapping("/index3")
  16. public String index3(){
  17. return "index3";
  18. }
  19. }

OneInterceptor.java

  1. package com.example.demo.interceptor;
  2. import org.springframework.web.servlet.HandlerInterceptor;
  3. import org.springframework.web.servlet.ModelAndView;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. public class OneInterceptor implements HandlerInterceptor {
  7. /**
  8. * 在请求处理之前进行调用(Controller方法调用之前)
  9. */
  10. @Override
  11. public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
  12. Object object) throws Exception {
  13. System.out.println("OneInterceptor preHandle");
  14. return true;
  15. }
  16. @Override
  17. public void postHandle(HttpServletRequest request, HttpServletResponse response,
  18. Object object, ModelAndView mv)
  19. throws Exception {
  20. System.out.println("OneInterceptor postHandle");
  21. }
  22. /**
  23. * 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行
  24. * (主要是用于进行资源清理工作)
  25. */
  26. @Override
  27. public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
  28. Object object, Exception ex)
  29. throws Exception {
  30. System.out.println("OneInterceptor afterCompletion");
  31. }
  32. }

ThreeController.java

  1. package com.example.demo.interceptor;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. @Controller
  5. @RequestMapping("three")
  6. public class ThreeController {
  7. @RequestMapping("/index")
  8. public String index(){
  9. return "index";
  10. }
  11. @RequestMapping("/index2")
  12. public String index2(){
  13. return "index2";
  14. }
  15. @RequestMapping("/index3")
  16. public String index3(){
  17. return "index3";
  18. }
  19. }

TwoController.java

  1. package com.example.demo.interceptor;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.servlet.HandlerInterceptor;
  5. import org.springframework.web.servlet.ModelAndView;
  6. import javax.servlet.http.HttpServletRequest;
  7. import javax.servlet.http.HttpServletResponse;
  8. @Controller
  9. @RequestMapping("two")
  10. public class TwoController{
  11. @RequestMapping("/index")
  12. public String index(){
  13. return "index";
  14. }
  15. @RequestMapping("/index2")
  16. public String index2(){
  17. return "index2";
  18. }
  19. @RequestMapping("/index3")
  20. public String index3(){
  21. return "index3";
  22. }
  23. }

TwoInterceptor.java

  1. package com.example.demo.interceptor;
  2. import org.springframework.web.servlet.HandlerInterceptor;
  3. import org.springframework.web.servlet.ModelAndView;
  4. import javax.servlet.http.HttpServletRequest;
  5. import javax.servlet.http.HttpServletResponse;
  6. public class TwoInterceptor implements HandlerInterceptor {
  7. /**
  8. * 在请求处理之前进行调用(Controller方法调用之前)
  9. */
  10. @Override
  11. public boolean preHandle(HttpServletRequest request, HttpServletResponse response,
  12. Object object) throws Exception {
  13. System.out.println("TwoController preHandle");
  14. return false;
  15. }
  16. @Override
  17. public void postHandle(HttpServletRequest request, HttpServletResponse response,
  18. Object object, ModelAndView mv)
  19. throws Exception {
  20. System.out.println("TwoController postHandle");
  21. }
  22. /**
  23. * 在整个请求结束之后被调用,也就是在DispatcherServlet 渲染了对应的视图之后执行
  24. * (主要是用于进行资源清理工作)
  25. */
  26. @Override
  27. public void afterCompletion(HttpServletRequest request, HttpServletResponse response,
  28. Object object, Exception ex)
  29. throws Exception {
  30. System.out.println("TwoController afterCompletion");
  31. }
  32. }

index相关.html

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>INDEX</title>
  6. </head>
  7. <body>
  8. <h1>INDEX PAGE</h1>
  9. </body>
  10. </html>

porn.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.12.RELEASE</version>
  9. <relativePath/> <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.example</groupId>
  12. <artifactId>demo</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>demo</name>
  15. <description>Demo project for Spring Boot</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-web</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.projectlombok</groupId>
  26. <artifactId>lombok</artifactId>
  27. <optional>true</optional>
  28. </dependency>
  29. <dependency>
  30. <groupId>org.springframework.boot</groupId>
  31. <artifactId>spring-boot-starter-test</artifactId>
  32. <scope>test</scope>
  33. </dependency>
  34. <dependency>
  35. <groupId>org.springframework.boot</groupId>
  36. <artifactId>spring-boot-starter-thymeleaf</artifactId>
  37. </dependency>
  38. </dependencies>
  39. <build>
  40. <plugins>
  41. <plugin>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-maven-plugin</artifactId>
  44. </plugin>
  45. </plugins>
  46. </build>
  47. </project>

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/86771
推荐阅读
相关标签
  

闽ICP备14008679号