Spring Security采用JWT验证时filter异常处理和JWT续期问题

2023-11-04 15:15

本文主要是介绍Spring Security采用JWT验证时filter异常处理和JWT续期问题,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

问题描述

security+jwt进行身份认证授权时,在登录前通常增加一个jwtfilter进行jwt验证,filter中的异常并不会被全局异常捕获,导致返回数据格式与统一格式不对

具体描述为: jwtfilter中对分发的token进行验证时抛出的异常捕获统一返回

问题出现原因

通常的filter时java web提供,并不被spring接管

解决方案 将异常抛给controller

JWTFilter中处理异常并跳转到异常处理控制器,再由控制器抛出异常,最终被全局异常捕获,统一返回格式

jwt验证时通常会抛出4中异常

  • ExpiredJwtException token过期时抛出
  • MalformedJwtException token的格式错误,即token被篡改
  • SignatureException token签名错误,生成 token 和解析 token 使用的 SECRET 签名不一致就会报这个错误
  • UnsupportedJwtException jwt不支持

在进行jwt验证时捕获这4种异常,再处理异常信息抛给controller处理

具体步骤

1 在JwtAuthenticationTokenFilter extends OncePerRequestFilter 中
try {Claims claims = JwtUtil.parseJWT(token);
}catch (ExpiredJwtException exception){log.error("身份验证过期"+exception);request.setAttribute("jwtFilter.error",new BaseException("身份验证过期"));request.getRequestDispatcher("/error/jwtFilter").forward(request,response);
}catch (MalformedJwtException exception){log.error("JWT Token格式不对"+exception);request.setAttribute("jwtFilter.error",new BaseException("JWT Token格式不对"));request.getRequestDispatcher("/error/jwtFilter").forward(request,response);
}catch (SignatureException exception){//生成 token 和解析 token 使用的 SECRET 签名不一致就会报这个错误log.error("JWT 签名错误"+exception);request.setAttribute("jwtFilter.error",new BaseException("JWT 签名错误"));request.getRequestDispatcher("/error/jwtFilter").forward(request,response);
}catch (UnsupportedJwtException exception){log.error("不支持的 Jwt "+exception);request.setAttribute("jwtFilter.error",new BaseException("不支持的 Jwt"));request.getRequestDispatcher("/error/jwtFilter").forward(request,response);
}catch (Exception e) {log.error("jwt验证:"+e);request.setAttribute("jwtFilter.error",new BaseException("JWT 验证失败"));request.getRequestDispatcher("/error/jwtFilter").forward(request,response);}
2 FilterExceptionController 中 最终被抛出,会被全局异常处理器处理返回统一的数据格式
    @RequestMapping("/error/jwtFilter")public void jwtFilterException(HttpServletRequest reques){log.warn("jwt controller ");Exception e = (Exception) reques.getAttribute("jwtFilter.error");log.warn(e.getMessage());throw e;}

其他相关

security在认证授权时抛出的异常

认证时 重写 AuthenticationEntryPoint

授权时 重写 AccessDeniedHandler

@Component
public class CustomAuthenticationEntryPoint implements AuthenticationEntryPoint {@Overridepublic void commence(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AuthenticationException e) throws IOException, ServletException {httpServletResponse.setCharacterEncoding("UTF-8");httpServletResponse.setContentType("application/json");httpServletResponse.getWriter().println(JSON.toJSON(BaseResponse.fail("1005",e.getMessage())));httpServletResponse.getWriter().flush();}
}@Component
public class CustomAccessDeniedHandler implements AccessDeniedHandler {@Overridepublic void handle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, AccessDeniedException e) throws IOException, ServletException {httpServletResponse.setCharacterEncoding("UTF-8");httpServletResponse.setContentType("application/json");httpServletResponse.getWriter().println(JSON.toJSON(BaseResponse.fail("1004",e.getMessage())));httpServletResponse.getWriter().flush();}
}

在security配置中加入

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)//EnableGlobalMethodSecurity开启全局方法级别授权控制,prePostEnabled在执行方法前进行授权
public class SecurityConfig  extends WebSecurityConfigurerAdapter {@Overrideprotected void configure(HttpSecurity http) throws Exception {//认证失败时的提示返回自定义信息http.exceptionHandling().accessDeniedHandler(customAccessDeniedHandler).authenticationEntryPoint(customAuthenticationEntryPoint);}
}

JWT续期问题

当jwt token快过期时,可以选择不续期,那么过期后需要用户重新登录再次下发token

续期方案

1 每次请求时续期 【完全不考虑】

即每次请求都重新下发新的token

缺点:下发频率太高,影响性能

2 快过期时续期【可以考虑】

比如30分钟过期时间,当还剩10分钟以内时请求过来进行续期

缺点:

        a. 泄露后容易导致token一直活跃

        b. 且如果是并发请求,容易导致下发多个token        

关于该方案泄露的补丁

可以在token中添加ip或者地域等信息【这些信息应该被加密】,一旦改变ip或者地域就需要重新登录

但是这样如果客户经常更换ip,就需要频繁登录,体验感会不好;可以让用户选择安全模式,如果选择安全模式就需要这样

3 双token【可以考虑】

登录时一次下发两个token

token1用于用户认证,过期时间设置短些,比如1个小时

token2用于续期,过期时间可以设置长些,比如1天

token2的key可以根据自己的习惯设置,可以设置一为无相关性的key,这样能在一定程度上混淆

过程:

登录请求返回 token1、token2和token1的过期时间

前端判断,当token1快过期了,携带token2请求

后端根据token2重新续期token1

缺点:

        前端每次请求都需要先判断token1是否快过期

这篇关于Spring Security采用JWT验证时filter异常处理和JWT续期问题的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/345360

相关文章

Spring Boot集成/输出/日志级别控制/持久化开发实践

《SpringBoot集成/输出/日志级别控制/持久化开发实践》SpringBoot默认集成Logback,支持灵活日志级别配置(INFO/DEBUG等),输出包含时间戳、级别、类名等信息,并可通过... 目录一、日志概述1.1、Spring Boot日志简介1.2、日志框架与默认配置1.3、日志的核心作用

破茧 JDBC:MyBatis 在 Spring Boot 中的轻量实践指南

《破茧JDBC:MyBatis在SpringBoot中的轻量实践指南》MyBatis是持久层框架,简化JDBC开发,通过接口+XML/注解实现数据访问,动态代理生成实现类,支持增删改查及参数... 目录一、什么是 MyBATis二、 MyBatis 入门2.1、创建项目2.2、配置数据库连接字符串2.3、入

Springboot项目启动失败提示找不到dao类的解决

《Springboot项目启动失败提示找不到dao类的解决》SpringBoot启动失败,因ProductServiceImpl未正确注入ProductDao,原因:Dao未注册为Bean,解决:在启... 目录错误描述原因解决方法总结***************************APPLICA编

深度解析Spring Security 中的 SecurityFilterChain核心功能

《深度解析SpringSecurity中的SecurityFilterChain核心功能》SecurityFilterChain通过组件化配置、类型安全路径匹配、多链协同三大特性,重构了Spri... 目录Spring Security 中的SecurityFilterChain深度解析一、Security

SpringBoot多环境配置数据读取方式

《SpringBoot多环境配置数据读取方式》SpringBoot通过环境隔离机制,支持properties/yaml/yml多格式配置,结合@Value、Environment和@Configura... 目录一、多环境配置的核心思路二、3种配置文件格式详解2.1 properties格式(传统格式)1.

Apache Ignite 与 Spring Boot 集成详细指南

《ApacheIgnite与SpringBoot集成详细指南》ApacheIgnite官方指南详解如何通过SpringBootStarter扩展实现自动配置,支持厚/轻客户端模式,简化Ign... 目录 一、背景:为什么需要这个集成? 二、两种集成方式(对应两种客户端模型) 三、方式一:自动配置 Thick

解决pandas无法读取csv文件数据的问题

《解决pandas无法读取csv文件数据的问题》本文讲述作者用Pandas读取CSV文件时因参数设置不当导致数据错位,通过调整delimiter和on_bad_lines参数最终解决问题,并强调正确参... 目录一、前言二、问题复现1. 问题2. 通过 on_bad_lines=‘warn’ 跳过异常数据3

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Java.lang.InterruptedException被中止异常的原因及解决方案

《Java.lang.InterruptedException被中止异常的原因及解决方案》Java.lang.InterruptedException是线程被中断时抛出的异常,用于协作停止执行,常见于... 目录报错问题报错原因解决方法Java.lang.InterruptedException 是 Jav

Python进行JSON和Excel文件转换处理指南

《Python进行JSON和Excel文件转换处理指南》在数据交换与系统集成中,JSON与Excel是两种极为常见的数据格式,本文将介绍如何使用Python实现将JSON转换为格式化的Excel文件,... 目录将 jsON 导入为格式化 Excel将 Excel 导出为结构化 JSON处理嵌套 JSON: