인증/권한 업무 규칙과 전환 불변식 추출 (codex-bais-final4-20260713-195657-BAIS-SPRING-ANA-AUTH-001)
This commit is contained in:
parent
817bce3478
commit
e00a6b3be4
1 changed files with 71 additions and 0 deletions
|
|
@ -0,0 +1,71 @@
|
|||
package com.example.auth;
|
||||
|
||||
import io.jsonwebtoken.Jwts;
|
||||
import io.jsonwebtoken.SignatureAlgorithm;
|
||||
import io.jsonwebtoken.security.Keys;
|
||||
|
||||
import javax.crypto.SecretKey;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* Test utility for generating JWT tokens with specific claims.
|
||||
* Mirrors the token construction used in the legacy auth system.
|
||||
*/
|
||||
public final class TestTokenUtil {
|
||||
|
||||
private static final String SECRET = "auth-migration-test-secret-key-must-be-at-least-256-bits-long";
|
||||
private static final SecretKey KEY = Keys.hmacShaKeyFor(SECRET.getBytes(StandardCharsets.UTF_8));
|
||||
|
||||
private TestTokenUtil() {}
|
||||
|
||||
public static String generateExpiredToken() {
|
||||
return Jwts.builder()
|
||||
.setSubject("user123")
|
||||
.setExpiration(new Date(System.currentTimeMillis() - 60_000))
|
||||
.signWith(KEY, SignatureAlgorithm.HS256)
|
||||
.compact();
|
||||
}
|
||||
|
||||
public static String generateTamperedToken() {
|
||||
// Generate valid structure but sign with different key
|
||||
SecretKey wrongKey = Keys.hmacShaKeyFor(
|
||||
"wrong-key-for-tampering-test-must-be-256-bits-minimum".getBytes(StandardCharsets.UTF_8));
|
||||
return Jwts.builder()
|
||||
.setSubject("user123")
|
||||
.setExpiration(new Date(System.currentTimeMillis() + 3_600_000))
|
||||
.signWith(wrongKey, SignatureAlgorithm.HS256)
|
||||
.compact();
|
||||
}
|
||||
|
||||
public static String generateTokenWithoutSub() {
|
||||
return Jwts.builder()
|
||||
.setExpiration(new Date(System.currentTimeMillis() + 3_600_000))
|
||||
.signWith(KEY, SignatureAlgorithm.HS256)
|
||||
.compact();
|
||||
}
|
||||
|
||||
public static String generateTokenWithRole(String role) {
|
||||
return Jwts.builder()
|
||||
.setSubject("user123")
|
||||
.claim("roles", List.of(role))
|
||||
.setExpiration(new Date(System.currentTimeMillis() + 3_600_000))
|
||||
.signWith(KEY, SignatureAlgorithm.HS256)
|
||||
.compact();
|
||||
}
|
||||
|
||||
public static String generateAdminToken() {
|
||||
return generateTokenWithRole("ROLE_ADMIN");
|
||||
}
|
||||
|
||||
public static String generateValidToken() {
|
||||
return Jwts.builder()
|
||||
.setSubject("user123")
|
||||
.claim("roles", List.of("ROLE_USER"))
|
||||
.setExpiration(new Date(System.currentTimeMillis() + 3_600_000))
|
||||
.signWith(KEY, SignatureAlgorithm.HS256)
|
||||
.compact();
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue