关于SpringBoot2.x集成SpringSecurity+JJWT(0.7.0-->0.11.5)生成Token登录鉴权的问题

news2024/10/5 23:27:38

项目场景:

问题:遵循版本稳定的前提下,搭建权限认证框架,基于SpringBoot2.x+SpringSecurity向上依赖+jjwt0.7.0构建用户认证鉴权,起因是某L觉得jjwt0.7.0版本,官方已经放弃维护,且从maven仓库对0.7.0版本使用量对比来看,遂放弃0.7.0转而升级为0.11.5,由此引发的token生成和解析的一系列BUG
pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>
	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>2.7.14</version>
		<relativePath/> <!-- lookup parent from repository -->
	</parent>
	<groupId>cn.isungent.ird</groupId>
	<artifactId>epdx</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<name>epdx</name>
	<description>Enterprise Data Platform</description>
	<properties>
		<java.version>1.8</java.version>
		<mybatis-plus.version>3.5.3.2</mybatis-plus.version>
	</properties>
	<dependencies>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<!-- NOTE: cannot exclude jackson because swagger needs it. -->
		</dependency>
		<dependency>
			<groupId>org.mybatis.spring.boot</groupId>
			<artifactId>mybatis-spring-boot-starter</artifactId>
			<version>2.3.1</version>
		</dependency>
		<dependency>
			<groupId>com.h2database</groupId>
			<artifactId>h2</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>com.mysql</groupId>
			<artifactId>mysql-connector-j</artifactId>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-devtools</artifactId>
			<scope>runtime</scope>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-configuration-processor</artifactId>
			<optional>true</optional>
		</dependency>
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-test</artifactId>
			<scope>test</scope>
		</dependency>
<!--		<dependency>-->
<!--			<groupId>org.mybatis.spring.boot</groupId>-->
<!--			<artifactId>mybatis-spring-boot-starter-test</artifactId>-->
<!--			<version>2.3.1</version>-->
<!--			<scope>test</scope>-->
<!--		</dependency>-->
		<!-- https://mvnrepository.com/artifact/com.baomidou/mybatis-plus-boot-starter-test -->
		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter-test</artifactId>
			<version>${mybatis-plus.version}</version>
			<scope>test</scope>
		</dependency>

		<!-- https://mvnrepository.com/artifact/com.google.code.gson/gson -->
		<dependency>
			<groupId>com.google.code.gson</groupId>
			<artifactId>gson</artifactId>
			<version>2.10.1</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.apache.commons/commons-lang3 -->
		<dependency>
			<groupId>org.apache.commons</groupId>
			<artifactId>commons-lang3</artifactId>
			<version>3.12.0</version>
		</dependency>
		<!-- https://mvnrepository.com/artifact/org.mockito/mockito-core -->
		<dependency>
			<groupId>org.mockito</groupId>
			<artifactId>mockito-core</artifactId>
			<version>4.11.0</version>
			<scope>test</scope>
		</dependency>
		<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>3.0.0</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-boot-starter</artifactId>
			<version>3.0.0</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>3.0.0</version>
		</dependency>

		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-boot-starter</artifactId>
			<version>${mybatis-plus.version}</version>
		</dependency>

		<dependency>
			<groupId>com.baomidou</groupId>
			<artifactId>mybatis-plus-extension</artifactId>
			<version>${mybatis-plus.version}</version>
		</dependency>

		<dependency>
			<groupId>org.mariadb.jdbc</groupId>
			<artifactId>mariadb-java-client</artifactId>
			<scope>runtime</scope>
			<version>2.7.10</version>
		</dependency>

		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-security</artifactId>
		</dependency>

		<dependency>
			<groupId>io.jsonwebtoken</groupId>
			<artifactId>jjwt-api</artifactId>
			<version>0.11.5</version>
		</dependency>
		<dependency>
			<groupId>io.jsonwebtoken</groupId>
			<artifactId>jjwt-impl</artifactId>
			<version>0.11.5</version>
			<scope>runtime</scope>
		</dependency>
		<dependency>
			<groupId>io.jsonwebtoken</groupId>
			<artifactId>jjwt-jackson</artifactId> <!-- or jjwt-gson if Gson is preferred -->
			<version>0.11.5</version>
			<scope>runtime</scope>
		</dependency>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

yml配置

spring:
  datasource:
    driverClassName: org.h2.Driver
    url: jdbc:h2:mem:test-db
  sql:
    init:
      data-locations: classpath:db/data.sql
      encoding: utf8
      schema-locations: classpath:db/schema.sql

  h2:
    console:
      enabled: true
      path: /h2-console


jwt:
  secret: kiki
  expiration: 18000
  tokenHeader: Authorization
  loginFailMaxNum: 5
  tokenPrefix: authorization-
  authUserName: AuthorizationUserName
  authRole: admin
  authEmail: admin@example.com

JwtTokenUtil

package cn.isungent.ird.epdx.common.util;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author kiki
 * @date 2023/10/10
 * @description
 */
@Component
@ConfigurationProperties(prefix = "jwt")
public class JwtTokenUtil {

    private static final String CLAIM_KEY_USERNAME = "sub";
    private static final String CLAIM_KEY_CREATED = "$2a$10$khMzocJpIzuhkiqSFLQ/euwedqw/ocRjZUI0SsS/TDY0e4RZIxh0C";

    private String secret;

    private Integer expiration;

    public Claims getClaimsFromToken(String token, String secret) {
        Claims claims = Jwts.parser()
                .setSigningKey(secret)
                .parseClaimsJws(token)
                .getBody();
        return claims;
    }

    private String generateToken(Map<String, Object> claims) {
        return Jwts.builder()
                .setClaims(claims)
                .setExpiration(generateExpirationDate())
                .signWith(SignatureAlgorithm.HS512, secret)
                .compact();
    }

    public String generateToken(String userEmail) {
        Map<String, Object> claims = new HashMap<>(16);
        claims.put(CLAIM_KEY_USERNAME, userEmail);
        claims.put(CLAIM_KEY_CREATED, new Date());
        return generateToken(claims);
    }

    private Date generateExpirationDate() {
        return new Date(System.currentTimeMillis() + expiration * 1000);
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public Integer getExpiration() {
        return expiration;
    }

    public void setExpiration(Integer expiration) {
        this.expiration = expiration;
    }
}

如下,升级版本后,postman模拟登录报错


在这里插入图片描述
consul打印

io.jsonwebtoken.security.WeakKeyException: The signing key's size is 24 bits which is not secure enough for the HS512 algorithm.  The JWT JWA Specification (RFC 7518, Section 3.2) states that keys used with HS512 MUST have a size >= 512 bits (the key size must be greater than or equal to the hash output size).  Consider using the io.jsonwebtoken.security.Keys class's 'secretKeyFor(SignatureAlgorithm.HS512)' method to create a key guaranteed to be secure enough for HS512.  See https://tools.ietf.org/html/rfc7518#section-3.2 for more information.
	at io.jsonwebtoken.SignatureAlgorithm.assertValid(SignatureAlgorithm.java:387)

问题描述

翻译:The signing key's size is 24 bits which is not secure enough for the HS512 algorithm. The JWT JWA Specification (RFC 7518, Section 3.2) states that keys used with HS512 MUST have a size >= 512 bits (the key size must be greater than or equal to the hash output size). Consider using the io.jsonwebtoken.security.Keys class's 'secretKeyFor(SignatureAlgorithm.HS512)' method to create a key guaranteed to be secure enough for HS512. See https://tools.ietf.org/html/rfc7518#section-3.2 for more information.大概意思是说:io.jsonwebtoken.security.WeakKeyException:签名密钥的大小是24位,对于HS512算法来说不够安全。JWT JWA规范(RFC 7518, Section 3.2)规定,与HS512一起使用的密钥的大小必须大于= 512位(密钥大小必须大于或等于哈希输出的大小)。考虑使用io.jsonwebtoken.security.Keys类的'secretKeyFor(signaturealalgorithm .HS512)'方法来创建一个保证对HS512足够安全的密钥。参见https:tools.ietf.orghtmlrfc7518section-3.2获取更多信息

综上:就是说升级后,那个密钥的长度要大于512,就是yml中配置secrect的配置项,太短了


解决方案:

提示:修改yml配置项jwt.secrect的密钥字符串的值为AAAAB3NzaC1yc2EAAAADAQABAAABgQDb73eBHCS6avoS2hnC3Zzf3N1JQax2Wg2Z9uvxIq6MlORGGFnrhV3jlDD2+iXsoM1UUEOIvhGMeeAt9EWJ8MVIbKOfHzChwkHojUlTFd87qWxCEfS9LWcl1d1Hsx9R1R5Uj31xfP76XlQAR+vhOGQtk1RW1RInxWZVo/++Bts9iNCvxMCx5c/v9Dhgrkj+CD2b1YoYILn6xZjjRMOVB33+xV5ERO5mluHQV+8xKght75WQqaOz4lARBsfxgOpMAzVU6IYCz2qGztrYNCjnjgBLee+Kok4belJM7fjW5ntuP7kPGLAnxfkZQWdR/iG71PuZmcNngesxaqIxVD3Hwa6Qfv2cUsS2bJn/L51uT5k07obZF5poxssCx7qzhe+1Aa1sDfct2vfkqzYCikI9ioKqM999dKiqYPP13ThfYfaWRuVKor3/tgZkbKmhf7+BH8pLnS03/ewZZgwxxx=

注意:值可以是任意字符串,非固定。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1085138.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

C++11发展史

文章目录 1.ChatGpt怎么说?2.C官方文档3.C11的诞生4.C11的意义 1.ChatGpt怎么说? C11是C编程语言的一个重要版本&#xff0c;也被称为C0x。它于2011年发布&#xff0c;并引入了许多新的特性和改进&#xff0c;使得C编程更加现代化和强大。 下面是C11的一些主要特性和发展历…

IDEA报Error:java:无效的源发行版13解决方式

出现问题原因&#xff1a;原本项目是spingboot2.0版本开发的&#xff0c;IDEA启动正常&#xff0c;后期新项目使用spingboot3.0&#xff0c;通过原来的IDEA版本及JDK1.8启动报上述错误&#xff0c;以下为版本文件 解决方式&#xff1a; 项目背景&#xff1a;项目已经上线&…

C++算法:图中的最短环

题目 现有一个含 n 个顶点的 双向 图&#xff0c;每个顶点按从 0 到 n - 1 标记。图中的边由二维整数数组 edges 表示&#xff0c;其中 edges[i] [ui, vi] 表示顶点 ui 和 vi 之间存在一条边。每对顶点最多通过一条边连接&#xff0c;并且不存在与自身相连的顶点。 返回图中 …

【Python中单引号、双引号和三引号具体的用法及注意点】

Python中单引号、双引号和三引号具体的用法及注意点 这篇文章主要给大家介绍了关于Python中单引号、双引号和三引号具体的用法及注意点的相关资料,Python中单引号、双引号、三引号中使用常常困惑,想弄明白这三者相同点和不同点,需要的朋友可以参考下 文章目录 Python中单引号、…

Zabbix监控系统详解1 :zabbix服务部署、自定义监控项、自动发现与自动注册

文章目录 1. Zabbix 概述1.1 简介1.2 zabbix的功能组件1.2.1 Zabbix Server1.2.2 数据库1.2.3 Web 界面1.2.4 Zabbix Agent1.2.5 Zabbix Proxy1.2.6 Java Gateway 1.3 工作原理1.4 常用端口号1.5 zabbix中预设的键值1.6 自定义监控项相关流程1.7 邮件报警配置思路1.8 Zabbix自动…

气膜建筑的可持续性:能源效益与环境影响

气膜建筑作为现代建筑技术的一种创新形式&#xff0c;不仅为城市景观增添了未来感&#xff0c;同时也在建筑领域引发了可持续性发展的讨论。本文将探讨气膜建筑在可持续性方面的关键议题&#xff0c;特别聚焦于其能源效益和环境影响&#xff0c;以期为未来气膜建筑设计和规划提…

dm关键字提示报错

问题出现 还是那个项目&#xff0c;然后呢因为其中涉及到了关键字&#xff0c;导致查询报错&#xff0c; 提示是REFERENCE出现错误。 问题处理 对于所有的关键字增加双引号可以处理。

服务器中了balckhoues勒索病毒怎么办?勒索病毒解密,数据恢复

近日&#xff0c;云天数据恢复中心发现&#xff0c;有多位用户的服务器中了一种名为balckhoues的勒索病毒&#xff0c;因为绝大多数用户是第一次遇到这种情况&#xff0c;所以对这种类型的勒索病毒并不是很了解。那接下来我们将对balckhoues勒索病毒做一个分析。 中毒特征 服务…

10.12作业

以下是一个简单的比喻&#xff0c;将多态概念与生活中的实际情况相联系&#xff1a; 比喻&#xff1a;动物园的讲解员和动物表演 想象一下你去了一家动物园&#xff0c;看到了许多不同种类的动物&#xff0c;如狮子、大象、猴子等。现在&#xff0c;动物园里有一位讲解员&…

运放供电设计 以及电压反馈电流反馈选择

因为OPA350可以直接驱动大电容 不需要对称&#xff0c;只要输出在电压范围内就可以 注&#xff1a;电流反馈运放一定要注意电阻取值&#xff0c;并且不能并电容

如何swagger关闭及给swagger加参数信息

项目的swagger方便研发人员调试&#xff0c;上线之后需要将swagger关闭这时候需要给原来的Configuration注解换成ConditionlOnProperty注解及可。注解参数信息 ConditionalOnProperty(name "swagger.enable", havingValue "true")若swagger想加入额外参…

Halcon我的基础教程(一)(我的菜鸟教程笔记)-halcon仿射变换(Affine Transformation)的探究与学习

目录 什么是仿射变换?仿射变换有哪些方式?任何仿射变换都能由以下基本变换构造而来:在Halocn中,仿射变换具有重要的作用,那我们本文章重点讨论仿射变换基础性知识。 使用Halcon中的重要算子,仿射变换一般解决步骤,案例应用会在以后的文章中我们重点解答与讨论。 我们首先…

基于 ceph-deploy 部署 Ceph 集群 超详细

Ceph part1 一、存储基础1.1 单机存储设备1.2 单机存储的问题1.3 单机存储问题的解决方案1.3.1 商业存储解决方案1.3.2 分布式存储&#xff08;软件定义的存储 SDS&#xff09; 二、分布式存储2.1 常见的分布式存储2.2 分布式存储的类型 三、Ceph概述3.1 Ceph简介3.2 Ceph 优势…

【另类加法】

目录 一、题目解析二、算法原理三、代码实现 一、题目解析 二、算法原理 三、代码实现 class Solution {public:int add(int A, int B){if(A 0)return B;else if(B 0)return A;elsereturn add(A^B, (A&B)<<1);} }

ASEMI整流桥KBU810参数,KBU810特性

编辑-Z KBU810参数描述&#xff1a; 型号&#xff1a;KBU810 最大直流反向电压VR&#xff1a;1000V 最大工作峰值反向电压VRWM&#xff1a;700V 最大平均正向电流IF&#xff1a;8A 非重复正向浪涌电流IFSM&#xff1a;300A 操作和储存温度范围TJ ,TSTG&#xff1a;-55 t…

LeetCode【1】两数之和

题目&#xff1a; 代码&#xff1a; public int[] twoSum(int[] nums, int target) {int[] result new int[2];Map<Integer, Integer> map new HashMap<>();// for (int i 0; i < nums.length; i) { // 这么写不能防重复啊&#xff01;注意这里不…

低功耗Sub-1G全频段收发一体芯片DP4306 适用无线对讲机 工业数据采集等应用

无线电对讲机既是移动通信中的一种专业无线通信工具&#xff0c;又是一种能满足人们生活需要的具有消费类产品特点的消费工具。顾名思义移动通信就是通信一方和另一方在移动中实现通信。 它是一种无线的可在移动中使用的一点对多点进行通信的终端设备&#xff0c;可使许多人同时…

【工具软件】在 window11 下使用微信读书app

作者&#xff1a;你们是大牛 链接&#xff1a;https://www.zhihu.com/question/340264583/answer/2439158730 来源&#xff1a;知乎 著作权归作者所有。商业转载请联系作者获得授权&#xff0c;非商业转载请注明出处。 在windows11上可以使用wsa, 也就是windows下的安卓子系统…

完美解决 :ModuleNotFoundError: No module named ‘dlib‘

目录 1&#xff0c;报错信息 解决办法&#xff1a; 1&#xff0c;报错信息 尝试 pip install dlib 报错&#xff1a; 尝试解决办法&#xff1a; Python3.6版本——下载文件&#xff1a; dlib-19.6.0-cp36-cp36m-win_amd64.whl 下载网址 :dlib PyPI Python3.7版本——下载…

react中利用useRef、forwardRef、useImperativeHandle获取并处理dom

React如何给组件设置ref属性&#xff0c;如果直接绑给组件&#xff0c;代码如下&#xff1a; import { useRef } from "react"function MyInput() {return (<input type"text"/>) }function App() {const myRef useRef(null)const handleClick ()…