ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

过滤器

2022-08-06 14:00:54  阅读:13  来源: 互联网

标签:servlet springframework org 过滤器 import servletRequest


使用过滤器,首先需要实现过滤器的三个方法:init(),doFilter(),destroy()

 1 package com.dayee.gateway.filter;
 2 
 3 import lombok.extern.slf4j.Slf4j;
 4 import org.springframework.core.annotation.Order;
 5 import org.springframework.stereotype.Component;
 6 
 7 import javax.servlet.*;
 8 import javax.servlet.annotation.WebFilter;
 9 import javax.servlet.http.HttpServletRequest;
10 import javax.servlet.http.HttpServletRequestWrapper;
11 import javax.servlet.http.HttpServletResponse;
12 import javax.servlet.http.HttpServletResponseWrapper;
13 import java.io.IOException;
14 
15 /**
16  * @Author OuYangFL
17  * @Date 2022/8/5 4:26 下午
18  */
19 @Component // 如果是用该注解,则CommonFilter过滤器类已经被注入到Spring容器中进行管理,不需要再添加过滤器配置类
20 @Slf4j
21 public class CommonFilter implements Filter {
22     @Override
23     public void init(FilterConfig filterConfig) throws ServletException {
24         log.info("初始化过滤器init");
25     }
26 
27     @Override
28     public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
29         HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
30         HttpServletResponseWrapper httpServletResponseWrapper = new HttpServletResponseWrapper((HttpServletResponse) servletResponse);
31         String requestURI = httpServletRequest.getRequestURI();
32         log.info(String.format("请求地址URI:%s", requestURI));
33         log.info("没有权限访问,跳转到index");
34         servletRequest.getRequestDispatcher("/index").forward(servletRequest, servletResponse);
35         /**if (requestURI.contains("/3")){
36             filterChain.doFilter(servletRequest, servletResponse);
37 
38         }else {
39             log.info("没有权限访问,跳转到index");
40             //httpServletResponseWrapper.sendRedirect("/");
41             servletRequest.getRequestDispatcher("/index").forward(servletRequest, servletResponse);
42         }*/
43     }
44 
45     @Override
46     public void destroy() {
47         log.info("过滤器被销毁");
48     }
49 }

编写过滤器配置类,用来加载过滤器类CommonFilter(在过滤器类没有添加@Component注解时)

 1 package com.dayee.gateway.config;
 2 
 3 import com.dayee.gateway.filter.CommonFilter;
 4 import org.springframework.beans.factory.annotation.Autowired;
 5 import org.springframework.boot.web.servlet.FilterRegistrationBean;
 6 import org.springframework.context.annotation.Bean;
 7 import org.springframework.context.annotation.Configuration;
 8 import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
 9 
10 /**
11  * @Author OuYangFL
12  * @Date 2022/8/6 9:47 上午
13  */
14 
15 @Configuration
16 public class FilterConfig implements WebMvcConfigurer {
17 
18 
19     @Bean
20     public FilterRegistrationBean filterRegistrationBean(){
21         FilterRegistrationBean filterRegistration = new FilterRegistrationBean(new CommonFilter());
22         //filterRegistration.setFilter(new CommonFilter());
23         filterRegistration.addUrlPatterns("/1");
24         filterRegistration.setName("select");
25         filterRegistration.setOrder(1);
26         return filterRegistration;
27     }
28 }

如果是用配置类与过滤器类相结合使用,在FilterConfig中addUrlPatterns方法可以进行URL拦截等相关设置

如果不使用配置类进行加载过滤器,直接使用@Component注解将过滤器类交给Spring容器,那么对URL的拦截设置可以在doFilter方法中进行创建。

暂时理解程度到此,后续研究继续深入

 

标签:servlet,springframework,org,过滤器,import,servletRequest
来源: https://www.cnblogs.com/ihearts/p/16556964.html

专注分享技术,共同学习,共同进步。侵权联系[admin#icode9.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有