首页 > 其他分享 >Springboot之OncePerRequestFilter 过滤器

Springboot之OncePerRequestFilter 过滤器

时间:2023-01-17 16:12:58浏览次数:60  
标签:return Springboot url request name OncePerRequestFilter 过滤器 method String

Springboot之OncePerRequestFilter 过滤器
原文链接:https://www.cnblogs.com/javalinux1/p/16389683.html

类说明
OncePerRequestFilter能够确保在一次请求只通过一次filter,而不需要重复执行。

记录到问题
在使用springSecurity的过程中遇到已经放权校验的url,在请求头依然携带有效的token信息,依然被拦截。

解决方案
使用JwtAuthenticationTokenFilter继承OncePerRequestFilter,重写doFilterInternal()方法,将请求头中的token信息去掉。
实现代码如下。

@Component
@Slf4j
public class JwtAuthenticationTokenFilter extends OncePerRequestFilter
{
@Autowired
private TokenService tokenService;
@Override
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain chain)
throws ServletException, IOException
{
log.info("当前访问的地址:{}", request.getRequestURI());
String url = request.getRequestURI();
// 校验请求的url是否在忽略鉴权的url中
if (checkUrl(url)) {
request = new HttpServletRequestWrapper(request) {
private Set<String> headerNameSet;
@Override
public Enumeration<String> getHeaderNames() {
if (headerNameSet == null) {
// first time this method is called, cache the wrapped request's header names:
headerNameSet = new HashSet<>();
Enumeration<String> wrappedHeaderNames = super.getHeaderNames();
while (wrappedHeaderNames.hasMoreElements()) {
String headerName = wrappedHeaderNames.nextElement();
if (!"Authorization".equalsIgnoreCase(headerName)) {
headerNameSet.add(headerName);
}
}
}
return Collections.enumeration(headerNameSet);
}
@Override
public Enumeration<String> getHeaders(String name) {
if ("Authorization".equalsIgnoreCase(name)) {
return Collections.<String>emptyEnumeration();
}
return super.getHeaders(name);
}
@Override
public String getHeader(String name) {
if ("Authorization".equalsIgnoreCase(name)) {
return null;
}
return super.getHeader(name);
}
};
}
LoginUser loginUser = tokenService.getLoginUser(request);
if (StringUtils.isNotNull(loginUser) && StringUtils.isNull(SecurityUtils.getAuthentication()))
{
tokenService.verifyToken(loginUser);
UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(loginUser, null, loginUser.getAuthorities());
authenticationToken.setDetails(new WebAuthenticationDetailsSource().buildDetails(request));
SecurityContextHolder.getContext().setAuthentication(authenticationToken);
}
chain.doFilter(request, response);
}

/**
* 校验路径是否是要忽略鉴权路径
* @param url
*/
private boolean checkUrl(String url){
for (String method : PatternsConstant.PATTERNS) {
if (!method.equals("/**") && !method.equals("**")) {
if (method.endsWith("/**")){
method = method.substring(0, method.length() - 3);
if (url.startsWith(method)) {
return true;
}
}else {
if (url.equals(method)) {
return true;
}
}
}else {
return false;
}
}
return false;
}

此处将忽略鉴权的url写在一个具体的类中,否则上面的方法拿不到需要忽略鉴权的url。

————————————————
版权声明:本文为CSDN博主「布道谷」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_44636027/article/details/109697469

标签:return,Springboot,url,request,name,OncePerRequestFilter,过滤器,method,String
From: https://www.cnblogs.com/sunny3158/p/17058043.html

相关文章

  • SpringBoot自定义starter
    SpringBoot自定义starter目录SpringBoot自定义starter1自定义starter1.1新建project和Module1.2对于starter-provider1.3install1.4对于starter-user1.5测试2执行......
  • 230116_50_SpringBoot入门
    指定自定义的配置文件bill.propertiesbill.properitesname=billage=11happy=falsebirth=2021/12/2通过@PropertySource注解指定自定义的配置文件@PropertyS......
  • 写一个 Hello SpringBoot2 项目
     项目目录:helloSpringBoot2、MainApplication、pom.xml  helloSpringBoot2逻辑类,标记@RestController/*@RestController的作用等同于@Controller+@ResponseB......
  • 过滤器Filter
       ......
  • yml文件配置了但是springboot读不到yml的内容的问题(已解决)
    在写项目时可能误操作加了这一句代码,结果就导致了系统读不到yml配置解决方法:删除pom.xml中的<packagin>pom</packagin>或者尝试 https://blog.csdn.net/qq_41555595/art......
  • springboot返回json和xml
    在项目中某些情况下接口需要返回的是xml数据或者json数据1、在springboot项目加入依赖jackson-dataformat-xml<dependency><groupId>com.fasterxml.jacks......
  • springboot的定时任务
    1、定时任务的创建方式:基于注解@Schedule和@EnableScheduling(待完善。。。)2、@Schedule和@EnableScheduling适用于定时任务执行时间较短,并且比较单......
  • SpringBoot集成Thymeleaf
    1、引入依赖在maven项目的pom.xml中引入spingboot-boot-starter-thymeleaf依赖,同时为了解决Html严格校验报错的问题,增加nekohtml依赖<!--thymeleaf模板引擎-->......
  • springboot集成nacos 注册中心
     接上一篇集成配置中心,本文介绍注册中心,目录结构如下在nacosregister的pom.xml文件中添加引用<dependency><groupId>com.alibaba.cloud</groupId>......
  • springboot反射 + 自定义注解
    利用反射调用方法获取bean寻找bean中指定的方法method(方法名可能匹配,参数类型不匹配,故还要分析参数类型)利用method.invoke方法Spring已经为此实现了完整的机制,......