[Token] 토큰에 loginId 추가. 기본 username을 loginid 로 변경

This commit is contained in:
Hyojin Ahn 2025-11-28 13:22:28 -05:00
parent 30ec07c368
commit dc4c345d35
2 changed files with 10 additions and 21 deletions

View File

@ -73,12 +73,12 @@ public class JwtAuthenticationFilter extends OncePerRequestFilter {
// JWT 유효성 검증 + DB 토큰 검증 // JWT 유효성 검증 + DB 토큰 검증
if (jwtService.isTokenValid(jwt, employee) && isTokenValid) { if (jwtService.isTokenValid(jwt, employee) && isTokenValid) {
UsernamePasswordAuthenticationToken authToken = UsernamePasswordAuthenticationToken authToken =
new UsernamePasswordAuthenticationToken( new UsernamePasswordAuthenticationToken(
employeeDetails, employee.getEmpLoginId(), // principal loginId
null, null,
employeeDetails.getAuthorities() employeeDetails.getAuthorities()
); );
authToken.setDetails( authToken.setDetails(
new WebAuthenticationDetailsSource().buildDetails(request) new WebAuthenticationDetailsSource().buildDetails(request)
); );

View File

@ -57,6 +57,7 @@ public class JwtService {
// 직원 이름 추가 // 직원 이름 추가
extraClaims.put("firstName", employee.getEmpFirstName()); extraClaims.put("firstName", employee.getEmpFirstName());
extraClaims.put("lastName", employee.getEmpLastName()); extraClaims.put("lastName", employee.getEmpLastName());
extraClaims.put("loginId", employee.getEmpLoginId());
return buildToken(extraClaims, employee.getEmpUuid().toString(), jwtExpiration); return buildToken(extraClaims, employee.getEmpUuid().toString(), jwtExpiration);
} }
@ -78,6 +79,7 @@ public class JwtService {
// 직원 이름 추가 // 직원 이름 추가
extraClaims.put("firstName", employee.getEmpFirstName()); extraClaims.put("firstName", employee.getEmpFirstName());
extraClaims.put("lastName", employee.getEmpLastName()); extraClaims.put("lastName", employee.getEmpLastName());
extraClaims.put("loginId", employee.getEmpLoginId());
return buildToken(extraClaims, employee.getEmpUuid().toString(), refreshExpiration); return buildToken(extraClaims, employee.getEmpUuid().toString(), refreshExpiration);
} }
@ -113,26 +115,13 @@ public class JwtService {
public static void main(String[] args) { public static void main(String[] args) {
JwtService jwtService = new JwtService(); JwtService jwtService = new JwtService();
jwtService.secretKey = "D0HaHnTPKLkUO9ULL1Ulm6XDZjhzuFtvTCcxTxSoCS8="; jwtService.secretKey = "";
String token = "eyJhbGciOiJIUzI1NiJ9.eyJwZXJtaXNzaW9ucyI6WyJIOlI6UCIsIk86QzpBIiwiTzpSOkEiLCJPOlU6QSIsIk86RDpBIiwiUzpDOkEiLCJTOlI6QSIsIlM6VTpBIl0sInJvbGVzIjpbIk9wZXJhdGlvbnMgTWFuYWdlciJdLCJzdWIiOiJmZGE1NGZkZS03MTBmLTQ4ZDItYTRmYi00NzM2YjJhM2RhNWEiLCJpYXQiOjE3NjMxMzU4MzMsImV4cCI6MTc2MzIyMjIzM30.ie38b2JnkP3k4Vz7TzAwI7oRgOsIFYf0yMYADq5EhNM"; String token = "";
// user 정보 // user 정보
Claims claims = jwtService.extractAllClaims(token); Claims claims = jwtService.extractAllClaims(token);
System.out.println("Subject (emp_uuid): " + claims.getSubject());
System.out.println("Roles: " + claims.get("roles"));
System.out.println("Roles: " + claims.get("permissions"));
System.out.println("IssuedAt: " + claims.getIssuedAt());
System.out.println("Expiration: " + claims.getExpiration());
// 모든 Claims 확인
// Claims claims = Jwts.parserBuilder()
// .setSigningKey(Keys.hmacShaKeyFor("<secret_key>".getBytes()))
// .build()
// .parseClaimsJws(token)
// .getBody();
System.out.println("Claims: " + claims); System.out.println("Claims: " + claims);
} }
} }