用户校验

This commit is contained in:
2025-07-22 00:58:19 +08:00
parent b1ef12c800
commit db8504fb7a
8 changed files with 229 additions and 18 deletions

View File

@ -0,0 +1,84 @@
package com.sczx.gateway.util;
import io.jsonwebtoken.JwtException;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.security.Keys;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import javax.crypto.SecretKey;
/**
* @Author: 张黎
* @Date: 2025/07/06/14:00
* @Description:
*/
@Slf4j
@Component
public class JwtUtil {
@Value("${auth.token-expiration}")
private long expiration;
private final SecretKey key;
public JwtUtil(@Value("${auth.secret-key}") String secretKey) {
this.key = Keys.hmacShaKeyFor(secretKey.getBytes());
// this.key = Keys.secretKeyFor(SignatureAlgorithm.HS512);
log.info("JWT 密钥:{}", secretKey);
}
// 支持自定义 claims 的 generateToken 方法
// public String generateToken(SimpleUserInfoDTO simpleUserInfoDTO, String subject){
// Map<String, Object> claims = Maps.newHashMap();
// claims.put("userInfo", simpleUserInfoDTO.getUserId());
// return Jwts.builder()
// .setClaims(claims) // 设置自定义 claims
// .setSubject(subject)
// .setExpiration(new Date(System.currentTimeMillis() + expiration))
// .signWith(key, SignatureAlgorithm.HS512)
// .compact();
// }
// 从 token 中提取某个字段
// public <T> T getClaim(String token, String claimKey, Class<T> clazz) {
// return Jwts.parserBuilder()
// .setSigningKey(key)
// .build()
// .parseClaimsJws(token)
// .getBody()
// .get(claimKey, clazz);
// }
//
// // 从 token 中提取某个对象类型的字段(支持复杂对象)
// public <T> T getClaimFromToken(String token, String claimKey, Class<T> targetClass) {
// return Jwts.parserBuilder()
// .setSigningKey(key)
// .build()
// .parseClaimsJws(token)
// .getBody()
// .get(claimKey, targetClass);
// }
public String extractUsername(String token) {
return Jwts.parserBuilder()
.setSigningKey(key)
.build()
.parseClaimsJws(token)
.getBody()
.getSubject();
}
public boolean validateToken(String token) {
try {
Jwts.parserBuilder()
.setSigningKey(key)
.build()
.parseClaimsJws(token);
return true;
} catch (JwtException ex) {
return false;
}
}
}

View File

@ -0,0 +1,70 @@
package com.sczx.gateway.util;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Component;
import java.util.concurrent.TimeUnit;
/**
* @Author: 张黎
* @Date: 2025/07/06/14:21
* @Description:
*/
@Slf4j
@Component
public class RedisUtil {
@Autowired
private StringRedisTemplate redisTemplate;
public void set(String key, String value, long timeout, TimeUnit unit) {
redisTemplate.opsForValue().set(key, value, timeout, unit);
}
public String get(String key) {
return redisTemplate.opsForValue().get(key);
}
public void delete(String key) {
redisTemplate.delete(key);
}
private static final Long DEFAULT_EXPIRE = 120L;
public boolean getRedisLock(String key, String lockName) {
log.info("获取锁 - {}", key);
return getRedisLockWithTimeout(key, lockName, 120L);
}
/**
* 获取redislock
* 加锁并设置过期时间
*
* @param key
* @param lockName
* @return
*/
public boolean getRedisLockWithTimeout(String key, String lockName, Long lockExpire) {
boolean lock = false;
if (Boolean.TRUE.equals(redisTemplate.opsForValue().setIfAbsent(key, lockName))) {
redisTemplate.expire(key, lockExpire, TimeUnit.SECONDS);
return true;
}
if (redisTemplate.getExpire(key) == -1) {
//保证锁一定设置过期时间
redisTemplate.expire(key, DEFAULT_EXPIRE, TimeUnit.SECONDS);
}
log.info("未获取到锁:" + lock + ", lockName={},key={}", lockName, key);
return lock;
}
public void deleteRedisLock(String key) {
log.info("释放锁 - {}", key);
redisTemplate.delete(key);
}
}