OncePerRequestFilter 是一个过滤器,每个请求都会执行一次;一般开发中主要是做检查是否已登录、Token是否过期和授权等操作,而每个操作都是一个过滤器,下面演示一下。
OncePerRequestFilter 使用 检查是否登录过期过滤器 import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Component;import org.springframework.web.filter.OncePerRequestFilter;import javax.servlet.FilterChain;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;/*** 检查是否登录过期** @author francis* @create: 2023-08-30 16:45**/@Component@Slf4jpublic class JwtAuthenticationTokenFilter extends OncePerRequestFilter {@Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {log.info("进入 JwtAuthenticationTokenFilter ...");/*** 从 request 的 header 中拿出来 token*/String token = request.getHeader("token");if (token == null || token.isEmpty()) {// 没有携带 token 则 放行filterChain.doFilter(request, response);return;}/*** 检查 token 是否过期逻辑 .....*/// 放行filterChain.doFilter(request, response);}} 检查是否登录过期过滤器 import lombok.extern.slf4j.Slf4j;import org.springframework.stereotype.Component;import org.springframework.web.filter.OncePerRequestFilter;import javax.servlet.FilterChain;import javax.servlet.ServletException;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import java.io.IOException;/*** 请求日志** @author francis* @create: 2023-08-31 10:15**/@Component@Slf4jpublic class OperationLogFilter extends OncePerRequestFilter {@Overrideprotected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException {log.info("OperationLogFilter ...");/*** 操作日志记录 ...*/// 放行filterChain.doFilter(request, response);}} SecurityConfiguration 配置 import com.security.filter.JwtAuthenticationTokenFilter;import com.security.filter.OperationLogFilter;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.security.authentication.AuthenticationManager;import org.springframework.security.config.annotation.web.builders.HttpSecurity;import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;import org.springframework.security.config.http.SessionCreationPolicy;import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;/*** Security 配置类** @author francis* @create: 2023-08-30 14:19**/@Configuration@EnableWebSecuritypublic class SecurityConfiguration extends WebSecurityConfigurerAdapter {@Autowiredprivate JwtAuthenticationTokenFilter jwtAuthenticationTokenFilter;@Autowiredprivate OperationLogFilter operationLogFilter;@Overrideprotected void configure(HttpSecurity http) throws Exception {http// 关闭csrf.csrf().disable()// 不通过 Session 获取 SecurityContext.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().authorizeRequests()// 对于登录接口 允许匿名访问.antMatchers("/login").permitAll()// 除上面外的所有请求全部需要鉴权认证.anyRequest().authenticated();// 在 UsernamePasswordAuthenticationFilter(验证用户) 之前执行// TODO 需要注意的是下面过滤器的顺序就是执行的顺序,使用 @Order 也没办法改变http// 登录是否过期.addFilterBefore(jwtAuthenticationTokenFilter, UsernamePasswordAuthenticationFilter.class)// 请求日志.addFilterBefore(operationLogFilter, UsernamePasswordAuthenticationFilter.class);}@Bean@Overridepublic AuthenticationManager authenticationManagerBean() throws Exception {return super.authenticationManagerBean();}} End【Spring Security】使用 OncePerRequestFilter 过滤器校验登录过期、请求日志等操作,小米9支持5g吗
cpugpu芯片开发光刻机
智能终端演进
7
文件名:【Spring Security】使用 OncePerRequestFilter 过滤器校验登录过期、请求日志等操作,小米9支持5g吗
【Spring Security】使用 OncePerRequestFilter 过滤器校验登录过期、请求日志等操作
文章目录 前言OncePerRequestFilter 使用检查是否登录过期过滤器检查是否登录过期过滤器 SecurityConfiguration 配置 前言
同类推荐
-

【Ptyhon】关于自定义对象的Json序列化和反序列化,诺亚舟np1100(java自定义序列化和反序列化)
查看 -

【Qt】顶层窗口和普通窗口区别以及用法,聚划算官网首页(qt 顶层窗口)
查看 -

【Qt开发流程】之容器类1-介绍及常用容器类和使用Java风格迭代器进行遍历,诺基亚n83
查看 -

【Qt绘制仪表盘】,lg t320
查看 -

【RK3399Pro学习笔记】七、ROS订阅者Subscriber的编程实现,索尼t90
查看 -

【React】React-Redux基本使用,联想锋行(联想锋行amd)
查看 -

【RealTek sdk-3.4.14b】RTL8197FH-VG+RTL8812FR实现实现Host 网络和Guest 网络隔离以及各个连接终端间隔离功能,c730
查看 -

【RocketMQ】快速入门,索爱c901(索爱c901评测)
查看 -

【RocketMQ集群】Linux搭建RocketMQ双主双从集群,optimus2x(rocketmq双主双从 数据同步)
查看
控制面板
网站分类
搜索
最新留言
文章归档
网站收藏
友情链接