This commit is contained in:
2025-09-27 22:57:59 +08:00
parent 8a458ff0a4
commit ed26244cdb
12585 changed files with 1914308 additions and 3474 deletions

View File

@@ -32,6 +32,34 @@ public class JwtService {
return jwt.sign(alg);
}
public String signToken(Long userId, Long shopId, String phone, String provider, String email) {
Instant now = Instant.now();
Algorithm alg = Algorithm.HMAC256(props.getSecret() == null ? "dev-secret" : props.getSecret());
var jwt = JWT.create()
.withIssuer(props.getIssuer())
.withIssuedAt(java.util.Date.from(now))
.withExpiresAt(java.util.Date.from(now.plusSeconds(props.getTtlSeconds())))
.withClaim("userId", userId)
.withClaim("shopId", shopId)
.withClaim("provider", provider);
if (phone != null && !phone.isBlank()) jwt.withClaim("phone", phone);
if (email != null && !email.isBlank()) jwt.withClaim("email", email);
return jwt.sign(alg);
}
public String signAdminToken(Long adminId, String username) {
Instant now = Instant.now();
Algorithm alg = Algorithm.HMAC256(props.getSecret() == null ? "dev-secret" : props.getSecret());
var jwt = JWT.create()
.withIssuer(props.getIssuer())
.withIssuedAt(java.util.Date.from(now))
.withExpiresAt(java.util.Date.from(now.plusSeconds(props.getTtlSeconds())))
.withClaim("adminId", adminId)
.withClaim("role", "admin");
if (username != null && !username.isBlank()) jwt.withClaim("username", username);
return jwt.sign(alg);
}
public Map<String,Object> parseClaims(String authorizationHeader) {
Map<String,Object> out = new HashMap<>();
if (authorizationHeader == null || authorizationHeader.isBlank()) return out;
@@ -48,9 +76,15 @@ public class JwtService {
Long userId = jwt.getClaim("userId").asLong();
Long shopId = jwt.getClaim("shopId").asLong();
String phone = jwt.getClaim("phone").asString();
String email = jwt.getClaim("email").asString();
Long adminId = jwt.getClaim("adminId").asLong();
String role = jwt.getClaim("role").asString();
if (userId != null) out.put("userId", userId);
if (shopId != null) out.put("shopId", shopId);
if (phone != null && !phone.isBlank()) out.put("phone", phone);
if (email != null && !email.isBlank()) out.put("email", email);
if (adminId != null) out.put("adminId", adminId);
if (role != null && !role.isBlank()) out.put("role", role);
} catch (Exception ignore) { }
return out;
}