初次提交

This commit is contained in:
李玉东 2025-08-07 19:36:32 +08:00
parent 92528a70c1
commit ad3f99c9ab
4 changed files with 81 additions and 72 deletions

View File

@ -5,14 +5,10 @@
</component>
<component name="ChangeListManager">
<list default="true" id="af4c07b7-4ec6-4591-87e0-621479a360cc" name="更改" comment="初次提交">
<change beforePath="$PROJECT_DIR$/.idea/compiler.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/compiler.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/hivekion/baseData/service/impl/TblDataModelServiceImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/hivekion/baseData/service/impl/TblDataModelServiceImpl.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/hivekion/baseData/service/impl/TblShipFuelServiceImpl.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/hivekion/baseData/service/impl/TblShipFuelServiceImpl.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/hivekion/common/encrypt/AESUtil.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/hivekion/common/encrypt/AESUtil.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/hivekion/system/controller/SysAuthorityController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/hivekion/system/controller/SysAuthorityController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/hivekion/common/filter/JwtAuthenticationFilter.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/hivekion/common/filter/JwtAuthenticationFilter.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/com/hivekion/common/security/JwtTokenUtils.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/com/hivekion/common/security/JwtTokenUtils.java" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -43,8 +39,12 @@
"Maven.springbootmybatis [clean].executor": "Run",
"Maven.springbootmybatis [compile].executor": "Run",
"ModuleVcsDetector.initialDetectionPerformed": "true",
"RequestMappingsPanelOrder0": "0",
"RequestMappingsPanelOrder1": "1",
"RequestMappingsPanelWidth0": "75",
"RequestMappingsPanelWidth1": "75",
"RunOnceActivity.ShowReadmeOnStart": "true",
"Spring Boot.DemoApplication.executor": "Run",
"Spring Boot.DemoApplication.executor": "Debug",
"git-widget-placeholder": "main",
"node.js.detected.package.eslint": "true",
"node.js.detected.package.tslint": "true",
@ -58,6 +58,9 @@
"vue.rearranger.settings.migration": "true"
}
}]]></component>
<component name="ReactorSettings">
<option name="notificationShown" value="true" />
</component>
<component name="RunManager">
<configuration name="DemoApplication" type="SpringBootApplicationConfigurationType" factoryName="Spring Boot" nameIsGenerated="true">
<module name="springbootmybatis" />
@ -84,7 +87,7 @@
<updated>1754560710248</updated>
<workItem from="1754560714503" duration="2195000" />
<workItem from="1754563543522" duration="8000" />
<workItem from="1754564611042" duration="517000" />
<workItem from="1754564611042" duration="1866000" />
</task>
<task id="LOCAL-00001" summary="初次提交">
<option name="closed" value="true" />
@ -102,7 +105,15 @@
<option name="project" value="LOCAL" />
<updated>1754562853375</updated>
</task>
<option name="localTasksCounter" value="3" />
<task id="LOCAL-00003" summary="初次提交">
<option name="closed" value="true" />
<created>1754565249597</created>
<option name="number" value="00003" />
<option name="presentableId" value="LOCAL-00003" />
<option name="project" value="LOCAL" />
<updated>1754565249597</updated>
</task>
<option name="localTasksCounter" value="4" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -112,6 +123,17 @@
<MESSAGE value="初次提交" />
<option name="LAST_COMMIT_MESSAGE" value="初次提交" />
</component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<line-breakpoint enabled="true" type="java-line">
<url>file://$PROJECT_DIR$/src/main/java/com/hivekion/common/filter/JwtAuthenticationFilter.java</url>
<line>83</line>
<option name="timeStamp" value="1" />
</line-breakpoint>
</breakpoints>
</breakpoint-manager>
</component>
<component name="XSLT-Support.FileAssociations.UIState">
<expand />
<select />

16
pom.xml
View File

@ -137,8 +137,20 @@
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
<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>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<!--代码自动生成器的依赖-->
<dependency>

View File

@ -87,7 +87,7 @@ public class JwtAuthenticationFilter extends BasicAuthenticationFilter {
}
try {
String userName = JwtTokenUtils.getUsernameFromToken(token);
log.info("");
if (StringUtils.isNotBlank(userName)) {
Object obj = redisUtil.getUsernameByToken(token);
if (obj == null) {

View File

@ -2,26 +2,15 @@ package com.hivekion.common.security;
import com.hivekion.common.entity.vo.LoginUser;
import com.hivekion.common.exception.BusinessException;
import com.hivekion.system.service.ISysUserService;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.nio.charset.StandardCharsets;
import java.security.Key;
import java.util.*;
/**
* JWT工具类
*
* @author Louis
* @date Jun 29, 2019
*/
public class JwtTokenUtils {
public static final String TOKEN_PREFIX = "Bearer "; // 前缀
@ -31,15 +20,20 @@ public class JwtTokenUtils {
private static final String USERID = Claims.ID; // 用户ID
private static final String CREATED = "created"; // 创建时间
private static final String AUTHORITIES = "authorities"; // 权限列表
private static final String SECRET = "1q2w3E*"; // 密钥
private static final long EXPIRE_TIME = 2 * 60 * 60 * 1000; // 有效期2小时
@Autowired
ISysUserService userService;
// 密钥长度必须大于等于 64 字节
private static final String SECRET = "this_is_a_very_long_secret_key_for_hs512_algorithm_1234567890!@#$%^&*()_+abcd";
private static final long EXPIRE_TIME = 24 * 60 * 60 * 1000; // 有效期24小时
// 新增统一的 Key 获取方法
private static Key getKey() {
return Keys.hmacShaKeyFor(SECRET.getBytes(StandardCharsets.UTF_8));
}
/**
* 生成令牌
*
* @return 令牌
*/
public static String generateToken(Authentication authentication) {
Map<String, Object> claims = new HashMap<>(3);
@ -49,32 +43,26 @@ public class JwtTokenUtils {
Date expirationDate = new Date(System.currentTimeMillis() + EXPIRE_TIME);
return Jwts.builder()
.setClaims(claims) //
.setExpiration(expirationDate) // 过期时间
.signWith(SignatureAlgorithm.HS512, SECRET)
.compact();
.setClaims(claims)
.setExpiration(expirationDate)
.signWith(getKey(), SignatureAlgorithm.HS512)
.compact();
}
/**
* 从数据声明生成令牌
*
* @param claims 数据声明
* @return 令牌
*/
private static String generateToken(Map<String, Object> claims) {
Date expirationDate = new Date(System.currentTimeMillis() + EXPIRE_TIME);
return Jwts.builder()
.setClaims(claims) //
.setExpiration(expirationDate) // 过期时间
.signWith(SignatureAlgorithm.HS512, SECRET)
.compact();
.setClaims(claims)
.setExpiration(expirationDate)
.signWith(getKey(), SignatureAlgorithm.HS512)
.compact();
}
/**
* 从令牌中获取用户名
*
* @param token 令牌
* @return 用户名
*/
public static String getUsernameFromToken(String token) {
String username;
@ -89,8 +77,6 @@ public class JwtTokenUtils {
/**
* 根据请求令牌获取登录认证信息
*
* @return 用户名
*/
public static Authentication getAuthenticationeFromToken(HttpServletRequest request) {
Authentication authentication = null;
@ -114,12 +100,12 @@ public class JwtTokenUtils {
Object authors = claims.get(AUTHORITIES); // 权限列表
LoginUser user = new LoginUser();
authentication =
new UsernamePasswordAuthenticationToken(user, null, new ArrayList<>());
new UsernamePasswordAuthenticationToken(user, null, new ArrayList<>());
} else {
if (validateToken(token, SecurityUtils.getUsername())) {
authentication =
SecurityUtils
.getAuthentication(); // 如果上下文中Authentication非空且请求令牌合法直接返回当前登录认证信息
SecurityUtils
.getAuthentication(); // 如果上下文中Authentication非空且请求令牌合法直接返回当前登录认证信息
}
}
}
@ -128,14 +114,16 @@ public class JwtTokenUtils {
/**
* 从令牌中获取数据声明
*
* @param token 令牌
* @return 数据声明
*/
private static Claims getClaimsFromToken(String token) {
Claims claims;
try {
claims = Jwts.parser().setSigningKey(SECRET).parseClaimsJws(token).getBody();
// 这里用新版API
claims = Jwts.parserBuilder()
.setSigningKey(getKey())
.build()
.parseClaimsJws(token)
.getBody();
} catch (Exception e) {
claims = null;
}
@ -144,21 +132,14 @@ public class JwtTokenUtils {
/**
* 验证令牌
*
* @param token
* @param username
* @return
*/
public static Boolean validateToken(String token, String username) {
String userName = getUsernameFromToken(token);
return (userName.equals(username) && !isTokenExpired(token));
return (userName != null && userName.equals(username) && !isTokenExpired(token));
}
/**
* 刷新令牌
*
* @param token
* @return
*/
public static String refreshToken(String token) {
String refreshedToken;
@ -174,9 +155,6 @@ public class JwtTokenUtils {
/**
* 判断令牌是否过期
*
* @param token 令牌
* @return 是否过期
*/
public static Boolean isTokenExpired(String token) {
try {
@ -190,9 +168,6 @@ public class JwtTokenUtils {
/**
* 获取请求token
*
* @param request
* @return
*/
public static String getToken(HttpServletRequest request) {
String token = request.getHeader("Authorization");
@ -208,4 +183,4 @@ public class JwtTokenUtils {
}
return token;
}
}
}