feat(login): 添加记住我登录功能
前端新增记住我勾选框,登录页挂载时自动恢复已保存的用户名,勾选时将用户名存入本地存储并在登录请求中携带对应参数;后端新增rememberMe参数处理逻辑,配置7天免登录的JWT有效期,重载JWT工具类以支持自定义令牌过期时间
This commit is contained in:
1 parent
f400a93bdd
commit
058e1fb8a7
6 files changed
+56
-4
No files matched your search
@@ -25,24 +25,45 @@ public class JwtUtil {
|
||||
@Value("${jwt.expiration}")
|
||||
private Long expiration;
|
||||
|
||||
@Value("${jwt.remember-expiration:604800000}")
|
||||
private Long rememberExpiration;
|
||||
|
||||
public Long getRememberExpiration() {
|
||||
return rememberExpiration;
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 Token
|
||||
*/
|
||||
public String generateToken(Long userId, String username, Integer role, Long dealerId) {
|
||||
return generateToken(userId, username, role, dealerId, expiration);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成指定有效期的 Token(用于"记住我"延长有效期)
|
||||
*/
|
||||
public String generateToken(Long userId, String username, Integer role, Long dealerId, long expirationMillis) {
|
||||
Map<String, Object> claims = new HashMap<>();
|
||||
claims.put("userId", userId);
|
||||
claims.put("username", username);
|
||||
claims.put("role", role);
|
||||
claims.put("dealerId", dealerId);
|
||||
return generateToken(claims);
|
||||
return generateToken(claims, expirationMillis);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成 Token
|
||||
*/
|
||||
public String generateToken(Map<String, Object> claims) {
|
||||
return generateToken(claims, expiration);
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成指定有效期的 Token
|
||||
*/
|
||||
public String generateToken(Map<String, Object> claims, long expirationMillis) {
|
||||
Date now = new Date();
|
||||
Date expiryDate = new Date(now.getTime() + expiration);
|
||||
Date expiryDate = new Date(now.getTime() + expirationMillis);
|
||||
|
||||
return Jwts.builder()
|
||||
.setClaims(claims)
|
||||
|
||||
Reference in new issue
Block a user