首页 > 其他分享 >创建springboot过滤器

创建springboot过滤器

时间:2022-12-09 12:32:37浏览次数:46  
标签:registrationBean springboot 创建 springframework org 过滤器 import servlet javax


springboot过滤器

import org.springframework.core.ParameterizedTypeReference;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import org.springframework.web.filter.OncePerRequestFilter;

import javax.servlet.FilterChain;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

public class BmXyCnsFilter extends OncePerRequestFilter {

MyRestTemplate restTemplate;
List list;

@Override
protected void initFilterBean() throws ServletException {
ServletContext servletContext = getServletContext();
WebApplicationContext webApplicationContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);
restTemplate= webApplicationContext.getBean(MyRestTemplate.class);
//从spring容器中获取到jxJson bean对象
//可查看上一个博客,路径
list=(List) webApplicationContext.getBean("jxJson");
}
@Override
protected void doFilterInternal(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, FilterChain filterChain) throws ServletException, IOException {
HttpSession session = httpServletRequest.getSession();
Visit visit = (Visit) session.getAttribute("SESSION_VISIT_______");
String requestURI = httpServletRequest.getRequestURI();
String substring = requestURI.substring(requestURI.indexOf("/"), requestURI.length());
//业务逻辑
{业务逻辑}
filterChain.doFilter(httpServletRequest,httpServletResponse);


}



}

使过滤器生效

import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FilterConfig {

@Bean
public FilterRegistrationBean getBmXyFilter(){
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(new BmXyCnsFilter());
registrationBean.addUrlPatterns("/*");
registrationBean.setName("bmxyFilter");
registrationBean.setOrder(2);
return registrationBean;
}

}

标签:registrationBean,springboot,创建,springframework,org,过滤器,import,servlet,javax
From: https://blog.51cto.com/u_15907536/5924879

相关文章

  • springboot+mybatis+log4j日志sql输出和文件输出
    pom引入依赖:<dependency><!--排除spring-boot-starter-logging--><groupId>org.springframework.boot</groupId><artifactId>sprin......
  • Hello,SpringBoot
    什么是SpringBoot一个javaweb的开发框架,和SpringMVC类似,对比其他javaweb框架的好处,官方说是简化开发,约定大于配置,youcan"justrun",能迅速的开发web应用,几行代码开发......
  • SpringBoot内置tomcat启动过程及原理
    1背景SpringBoot是一个框架,一种全新的编程规范,他的产生简化了框架的使用,同时也提供了很多便捷的功能,比如内置tomcat就是其中一项,他让我们省去了搭建tomcat容器,生成war,部署,......
  • threeJs 创建文字方式
    一,使用 TextGeometry创建3D文字1.需加载字体配合使用,使用THREE.FontLoader//加载字体loadFont(){returnnewPromise(function(resolve,reject){......
  • springboot中数据库批量新增InsertListMapper两种情况
    pom中mybatis的依赖包版本必须高点满足两种情况<dependency><groupId>tk.mybatis</groupId><artifactId>mapper-spring-boot-starter</artifactId><version>4.......
  • SpringBoot统一日志管理
    Springboot中统一日志管理一、为什么要用日志?一般分为两个大类:操作日志和系统日志**操作日志:**用户在操作软件时记录下来的操作步骤,便于用户自己查看。主要针对的是用户。**......
  • SpringBoot中统一日志管理
    Springboot中统一日志管理一、为什么要用日志?一般分为两个大类:操作日志和系统日志**操作日志:**用户在操作软件时记录下来的操作步骤,便于用户自己查看。主要针对......
  • Threejs:创建矩阵
     设置顶点创建矩形constgeometry3=newTHREE.BufferGeometry();constvertices=newFloat32Array([-1.0,-1.0,1.0,1.0,-1.0,1.0,1.0,1.0,1.0,......
  • Threejs:创建纹理
    创建纹理//导入纹理constloader=newTHREE.TextureLoader();//加载所需要的纹理图片consttexture1=loader.load('./dist/texture/sea.jpg')constmaterial5......
  • Threejs:创建几何体——图元
     BoxGeometry盒子+MeshBasicMaterialconstgeometry=newTHREE.BoxGeometry(1,1,1);constmaterial=newTHREE.MeshBasicMaterial({color:0x00ff00});const......