增加根据token获取用户信息
This commit is contained in:
19
src/main/java/com/sczx/order/dto/SimpleUserInfoDTO.java
Normal file
19
src/main/java/com/sczx/order/dto/SimpleUserInfoDTO.java
Normal file
@ -0,0 +1,19 @@
|
||||
package com.sczx.order.dto;
|
||||
|
||||
import io.swagger.annotations.ApiModel;
|
||||
import io.swagger.annotations.ApiModelProperty;
|
||||
import lombok.Data;
|
||||
|
||||
@ApiModel(value = "基本用户信息")
|
||||
@Data
|
||||
public class SimpleUserInfoDTO {
|
||||
|
||||
@ApiModelProperty(value = "用户id")
|
||||
private Integer userId;
|
||||
|
||||
@ApiModelProperty(value = "用户姓名")
|
||||
private String userName;
|
||||
|
||||
@ApiModelProperty(value = "角色id")
|
||||
private Integer roleId;
|
||||
}
|
||||
@ -1,5 +1,7 @@
|
||||
package com.sczx.order.utils;
|
||||
|
||||
import com.google.common.collect.Maps;
|
||||
import com.sczx.order.dto.SimpleUserInfoDTO;
|
||||
import io.jsonwebtoken.JwtException;
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
@ -10,6 +12,7 @@ import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.util.Date;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* @Author: 张黎
|
||||
@ -30,16 +33,38 @@ public class JwtUtil {
|
||||
// this.key = Keys.secretKeyFor(SignatureAlgorithm.HS512);
|
||||
log.info("JWT 密钥:{}", secretKey);
|
||||
}
|
||||
|
||||
public String generateToken(String username, String role) {
|
||||
// 支持自定义 claims 的 generateToken 方法
|
||||
public String generateToken(SimpleUserInfoDTO simpleUserInfoDTO, String subject){
|
||||
Map<String, Object> claims = Maps.newHashMap();
|
||||
claims.put("userInfo", simpleUserInfoDTO.getUserId());
|
||||
return Jwts.builder()
|
||||
.setSubject(username)
|
||||
.claim("role", role)
|
||||
.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)
|
||||
|
||||
@ -1,5 +1,6 @@
|
||||
package com.sczx.order.utils;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.stereotype.Component;
|
||||
@ -11,6 +12,7 @@ import java.util.concurrent.TimeUnit;
|
||||
* @Date: 2025/07/06/14:21
|
||||
* @Description:
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class RedisUtil {
|
||||
|
||||
@ -30,4 +32,39 @@ public class RedisUtil {
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user