feat: 初始化 Spring Boot 项目模板,搭建完整的用户认证与管理系统
- 新增项目基础配置:pom.xml 依赖管理、多环境配置(dev/prod)、Dockerfile、.env.example - 新增安全认证模块:JWT 工具类、JWT 过滤器、Spring Security 配置、自定义 UserDetails - 新增用户管理功能:注册/登录/查询/修改、角色管理(USER/ADMIN/ROOT)、分页查询、状态启禁用 - 新增密码重置功能:邮箱验证码发送、验证码校验重置、频率限制与过期机制 - 新增基础架构层:统一响应体 RestBean、全局异常处理、日志拦截器、Redis 工具类、JPA 配置 - 新增 Swagger/OpenAPI 文档配置与完整的 API 接口文档(API_DOCUMENT.md) - 新增数据库初始化 SQL 脚本(init.sql)
This commit is contained in:
57
src/main/java/com/aisi/template/utils/JwtUtil.java
Normal file
57
src/main/java/com/aisi/template/utils/JwtUtil.java
Normal file
@@ -0,0 +1,57 @@
|
||||
package com.aisi.template.utils;
|
||||
|
||||
import io.jsonwebtoken.*;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.Key;
|
||||
import java.util.Date;
|
||||
|
||||
@Component
|
||||
public class JwtUtil {
|
||||
|
||||
@Value("${jwt.secret}")
|
||||
private String secret;
|
||||
|
||||
private final long expiration = 1000 * 60 * 60 *24; // 24小时
|
||||
|
||||
private Key getSigningKey() {
|
||||
// HS512 要求密钥至少 512 位 = 64 字节
|
||||
if (secret.length() < 64) {
|
||||
throw new IllegalArgumentException("JWT secret must be at least 64 characters long for HS512");
|
||||
}
|
||||
return Keys.hmacShaKeyFor(secret.getBytes(StandardCharsets.UTF_8));
|
||||
}
|
||||
|
||||
public String generateToken(Long userId,String username) {
|
||||
return Jwts.builder()
|
||||
.setSubject(username)
|
||||
.claim("id", userId)
|
||||
.setIssuedAt(new Date())
|
||||
.setExpiration(new Date(System.currentTimeMillis() + expiration))
|
||||
.signWith(getSigningKey(), SignatureAlgorithm.HS512)
|
||||
.compact();
|
||||
}
|
||||
|
||||
public String extractUsername(String token) {
|
||||
return parseClaims(token).getBody().getSubject();
|
||||
}
|
||||
|
||||
public boolean validateToken(String token) {
|
||||
try {
|
||||
parseClaims(token);
|
||||
return true;
|
||||
} catch (JwtException e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private Jws<Claims> parseClaims(String token) {
|
||||
return Jwts.parserBuilder()
|
||||
.setSigningKey(getSigningKey())
|
||||
.build()
|
||||
.parseClaimsJws(token);
|
||||
}
|
||||
}
|
||||
337
src/main/java/com/aisi/template/utils/RedisUtils.java
Normal file
337
src/main/java/com/aisi/template/utils/RedisUtils.java
Normal file
@@ -0,0 +1,337 @@
|
||||
package com.aisi.template.utils;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.data.redis.core.ZSetOperations;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class RedisUtils {
|
||||
|
||||
private final RedisTemplate<String, Object> redisTemplate;
|
||||
|
||||
// ======================== 通用 ========================
|
||||
|
||||
public Boolean delete(String key) {
|
||||
return redisTemplate.delete(key);
|
||||
}
|
||||
|
||||
public Long delete(Collection<String> keys) {
|
||||
return redisTemplate.delete(keys);
|
||||
}
|
||||
|
||||
public Boolean hasKey(String key) {
|
||||
return redisTemplate.hasKey(key);
|
||||
}
|
||||
|
||||
public Boolean expire(String key, long timeout, TimeUnit unit) {
|
||||
return redisTemplate.expire(key, timeout, unit);
|
||||
}
|
||||
|
||||
public Long getExpire(String key) {
|
||||
return redisTemplate.getExpire(key);
|
||||
}
|
||||
|
||||
public Boolean expireAt(String key, long timestamp) {
|
||||
return redisTemplate.expireAt(key, new java.util.Date(timestamp));
|
||||
}
|
||||
|
||||
public Set<String> keys(String pattern) {
|
||||
return redisTemplate.keys(pattern);
|
||||
}
|
||||
|
||||
public Boolean rename(String oldKey, String newKey) {
|
||||
redisTemplate.rename(oldKey, newKey);
|
||||
return true;
|
||||
}
|
||||
|
||||
public Long ttl(String key) {
|
||||
return redisTemplate.getExpire(key, TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
// ======================== String ========================
|
||||
|
||||
public void set(String key, Object value) {
|
||||
redisTemplate.opsForValue().set(key, value);
|
||||
}
|
||||
|
||||
public void set(String key, Object value, long timeout, TimeUnit unit) {
|
||||
redisTemplate.opsForValue().set(key, value, timeout, unit);
|
||||
}
|
||||
|
||||
public void setIfAbsent(String key, Object value, long timeout, TimeUnit unit) {
|
||||
redisTemplate.opsForValue().setIfAbsent(key, value, timeout, unit);
|
||||
}
|
||||
|
||||
public Object get(String key) {
|
||||
return redisTemplate.opsForValue().get(key);
|
||||
}
|
||||
|
||||
public <T> T get(String key, Class<T> clazz) {
|
||||
Object value = redisTemplate.opsForValue().get(key);
|
||||
if (value == null) {
|
||||
return null;
|
||||
}
|
||||
return clazz.cast(value);
|
||||
}
|
||||
|
||||
public String getAndSet(String key, Object value) {
|
||||
Object old = redisTemplate.opsForValue().getAndSet(key, value);
|
||||
return old != null ? old.toString() : null;
|
||||
}
|
||||
|
||||
public Long increment(String key) {
|
||||
return redisTemplate.opsForValue().increment(key);
|
||||
}
|
||||
|
||||
public Long increment(String key, long delta) {
|
||||
return redisTemplate.opsForValue().increment(key, delta);
|
||||
}
|
||||
|
||||
public Long decrement(String key) {
|
||||
return redisTemplate.opsForValue().decrement(key);
|
||||
}
|
||||
|
||||
public Long decrement(String key, long delta) {
|
||||
return redisTemplate.opsForValue().decrement(key, delta);
|
||||
}
|
||||
|
||||
public Long strLength(String key) {
|
||||
return redisTemplate.opsForValue().size(key);
|
||||
}
|
||||
|
||||
// ======================== Hash ========================
|
||||
|
||||
public void hSet(String key, String hashKey, Object value) {
|
||||
redisTemplate.opsForHash().put(key, hashKey, value);
|
||||
}
|
||||
|
||||
public void hSetAll(String key, Map<String, Object> map) {
|
||||
redisTemplate.opsForHash().putAll(key, map);
|
||||
}
|
||||
|
||||
public Object hGet(String key, String hashKey) {
|
||||
return redisTemplate.opsForHash().get(key, hashKey);
|
||||
}
|
||||
|
||||
public Map<Object, Object> hGetAll(String key) {
|
||||
return redisTemplate.opsForHash().entries(key);
|
||||
}
|
||||
|
||||
public void hDelete(String key, Object... hashKeys) {
|
||||
redisTemplate.opsForHash().delete(key, hashKeys);
|
||||
}
|
||||
|
||||
public Boolean hHasKey(String key, String hashKey) {
|
||||
return redisTemplate.opsForHash().hasKey(key, hashKey);
|
||||
}
|
||||
|
||||
public Long hSize(String key) {
|
||||
return redisTemplate.opsForHash().size(key);
|
||||
}
|
||||
|
||||
public Long hIncrement(String key, String hashKey, long delta) {
|
||||
return redisTemplate.opsForHash().increment(key, hashKey, delta);
|
||||
}
|
||||
|
||||
public Set<Object> hKeys(String key) {
|
||||
return redisTemplate.opsForHash().keys(key);
|
||||
}
|
||||
|
||||
public List<Object> hValues(String key) {
|
||||
return redisTemplate.opsForHash().values(key);
|
||||
}
|
||||
|
||||
// ======================== List ========================
|
||||
|
||||
public Long lPush(String key, Object value) {
|
||||
return redisTemplate.opsForList().leftPush(key, value);
|
||||
}
|
||||
|
||||
public Long lPushAll(String key, Object... values) {
|
||||
return redisTemplate.opsForList().leftPushAll(key, values);
|
||||
}
|
||||
|
||||
public Long rPush(String key, Object value) {
|
||||
return redisTemplate.opsForList().rightPush(key, value);
|
||||
}
|
||||
|
||||
public Long rPushAll(String key, Object... values) {
|
||||
return redisTemplate.opsForList().rightPushAll(key, values);
|
||||
}
|
||||
|
||||
public Object lPop(String key) {
|
||||
return redisTemplate.opsForList().leftPop(key);
|
||||
}
|
||||
|
||||
public List<Object> lPop(String key, long count) {
|
||||
return redisTemplate.opsForList().leftPop(key, count);
|
||||
}
|
||||
|
||||
public Object rPop(String key) {
|
||||
return redisTemplate.opsForList().rightPop(key);
|
||||
}
|
||||
|
||||
public List<Object> rPop(String key, long count) {
|
||||
return redisTemplate.opsForList().rightPop(key, count);
|
||||
}
|
||||
|
||||
public Object lIndex(String key, long index) {
|
||||
return redisTemplate.opsForList().index(key, index);
|
||||
}
|
||||
|
||||
public Long lSize(String key) {
|
||||
return redisTemplate.opsForList().size(key);
|
||||
}
|
||||
|
||||
public List<Object> lRange(String key, long start, long end) {
|
||||
return redisTemplate.opsForList().range(key, start, end);
|
||||
}
|
||||
|
||||
public void lTrim(String key, long start, long end) {
|
||||
redisTemplate.opsForList().trim(key, start, end);
|
||||
}
|
||||
|
||||
public void lSet(String key, long index, Object value) {
|
||||
redisTemplate.opsForList().set(key, index, value);
|
||||
}
|
||||
|
||||
public Long lRemove(String key, long count, Object value) {
|
||||
return redisTemplate.opsForList().remove(key, count, value);
|
||||
}
|
||||
|
||||
// ======================== Set ========================
|
||||
|
||||
public Long sAdd(String key, Object... values) {
|
||||
return redisTemplate.opsForSet().add(key, values);
|
||||
}
|
||||
|
||||
public Long sRemove(String key, Object... values) {
|
||||
return redisTemplate.opsForSet().remove(key, values);
|
||||
}
|
||||
|
||||
public Set<Object> sMembers(String key) {
|
||||
return redisTemplate.opsForSet().members(key);
|
||||
}
|
||||
|
||||
public Boolean sIsMember(String key, Object value) {
|
||||
return redisTemplate.opsForSet().isMember(key, value);
|
||||
}
|
||||
|
||||
public Long sSize(String key) {
|
||||
return redisTemplate.opsForSet().size(key);
|
||||
}
|
||||
|
||||
public Object sRandomMember(String key) {
|
||||
return redisTemplate.opsForSet().randomMember(key);
|
||||
}
|
||||
|
||||
public Set<Object> sRandomMembers(String key, long count) {
|
||||
return redisTemplate.opsForSet().distinctRandomMembers(key, count);
|
||||
}
|
||||
|
||||
public Set<Object> sIntersect(String key1, String key2) {
|
||||
return redisTemplate.opsForSet().intersect(key1, key2);
|
||||
}
|
||||
|
||||
public Set<Object> sUnion(String key1, String key2) {
|
||||
return redisTemplate.opsForSet().union(key1, key2);
|
||||
}
|
||||
|
||||
public Set<Object> sDifference(String key1, String key2) {
|
||||
return redisTemplate.opsForSet().difference(key1, key2);
|
||||
}
|
||||
|
||||
// ======================== ZSet(有序集合)========================
|
||||
|
||||
public Boolean zAdd(String key, Object value, double score) {
|
||||
return redisTemplate.opsForZSet().add(key, value, score);
|
||||
}
|
||||
|
||||
public Long zAdd(String key, Set<ZSetOperations.TypedTuple<Object>> tuples) {
|
||||
return redisTemplate.opsForZSet().add(key, tuples);
|
||||
}
|
||||
|
||||
public Long zRemove(String key, Object... values) {
|
||||
return redisTemplate.opsForZSet().remove(key, values);
|
||||
}
|
||||
|
||||
public Long zRank(String key, Object value) {
|
||||
return redisTemplate.opsForZSet().rank(key, value);
|
||||
}
|
||||
|
||||
public Long zReverseRank(String key, Object value) {
|
||||
return redisTemplate.opsForZSet().reverseRank(key, value);
|
||||
}
|
||||
|
||||
public Set<Object> zRange(String key, long start, long end) {
|
||||
return redisTemplate.opsForZSet().range(key, start, end);
|
||||
}
|
||||
|
||||
public Set<Object> zReverseRange(String key, long start, long end) {
|
||||
return redisTemplate.opsForZSet().reverseRange(key, start, end);
|
||||
}
|
||||
|
||||
public Set<ZSetOperations.TypedTuple<Object>> zRangeWithScores(String key, long start, long end) {
|
||||
return redisTemplate.opsForZSet().rangeWithScores(key, start, end);
|
||||
}
|
||||
|
||||
public Set<Object> zRangeByScore(String key, double min, double max) {
|
||||
return redisTemplate.opsForZSet().rangeByScore(key, min, max);
|
||||
}
|
||||
|
||||
public Set<Object> zRangeByScore(String key, double min, double max, long offset, long count) {
|
||||
return redisTemplate.opsForZSet().rangeByScore(key, min, max, offset, count);
|
||||
}
|
||||
|
||||
public Long zCount(String key, double min, double max) {
|
||||
return redisTemplate.opsForZSet().count(key, min, max);
|
||||
}
|
||||
|
||||
public Long zSize(String key) {
|
||||
return redisTemplate.opsForZSet().size(key);
|
||||
}
|
||||
|
||||
public Double zScore(String key, Object value) {
|
||||
return redisTemplate.opsForZSet().score(key, value);
|
||||
}
|
||||
|
||||
public Long zRemoveRange(String key, long start, long end) {
|
||||
return redisTemplate.opsForZSet().removeRange(key, start, end);
|
||||
}
|
||||
|
||||
public Long zRemoveRangeByScore(String key, double min, double max) {
|
||||
return redisTemplate.opsForZSet().removeRangeByScore(key, min, max);
|
||||
}
|
||||
|
||||
// ======================== Bitmap ========================
|
||||
|
||||
public Boolean setBit(String key, long offset, boolean value) {
|
||||
return redisTemplate.opsForValue().setBit(key, offset, value);
|
||||
}
|
||||
|
||||
public Boolean getBit(String key, long offset) {
|
||||
return redisTemplate.opsForValue().getBit(key, offset);
|
||||
}
|
||||
|
||||
// ======================== HyperLogLog ========================
|
||||
|
||||
public Long pfAdd(String key, Object... values) {
|
||||
return redisTemplate.opsForHyperLogLog().add(key, values);
|
||||
}
|
||||
|
||||
public Long pfCount(String... keys) {
|
||||
return redisTemplate.opsForHyperLogLog().size(keys);
|
||||
}
|
||||
|
||||
public void pfMerge(String destination, String... sourceKeys) {
|
||||
redisTemplate.opsForHyperLogLog().union(destination, sourceKeys);
|
||||
}
|
||||
}
|
||||
46
src/main/java/com/aisi/template/utils/SecurityUtils.java
Normal file
46
src/main/java/com/aisi/template/utils/SecurityUtils.java
Normal file
@@ -0,0 +1,46 @@
|
||||
package com.aisi.template.utils;
|
||||
|
||||
import com.aisi.template.domain.CustomUserDetails;
|
||||
import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.context.SecurityContext;
|
||||
import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Component
|
||||
public class SecurityUtils {
|
||||
|
||||
/**
|
||||
* 获取当前 Authentication
|
||||
*/
|
||||
public static Authentication getAuthentication(){
|
||||
return SecurityContextHolder.getContext().getAuthentication();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取登录名
|
||||
*/
|
||||
public static String getUsername(){
|
||||
Authentication auth = getAuthentication();
|
||||
if (auth != null && auth.isAuthenticated()) {
|
||||
Object principal = auth.getPrincipal();
|
||||
if (principal instanceof CustomUserDetails user) {
|
||||
return user.getUsername();
|
||||
}else if (principal instanceof String username) {
|
||||
return username;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取用户Id
|
||||
*/
|
||||
public static Long getUserId() {
|
||||
Authentication auth = getAuthentication();
|
||||
if (auth != null && auth.isAuthenticated() && auth.getPrincipal() instanceof CustomUserDetails user) {
|
||||
return user.getId();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user