public String createStoreJwt(int storeIdx){
Date now = new Date();
return Jwts.builder()
.setHeaderParam("type","jwt")
.claim("storeIdx",storeIdx)
.setIssuedAt(now)
.setExpiration(new Date(System.currentTimeMillis()+1*(1000*60*60*24*365)))
.signWith(SignatureAlgorithm.HS256, Secret.JWT_SECRET_KEY)
.compact();
}
/*
JWT에서 storeIdx 추출
@return int
@throws BaseException
*/
public int getStoreIdx() throws BaseException{
//1. JWT 추출
String accessToken = getJwt();
if(accessToken == null || accessToken.length() == 0){
throw new BaseException(EMPTY_JWT);
}
// 2. JWT parsing
Jws<Claims> claims;
try{
claims = Jwts.parser()
.setSigningKey(Secret.JWT_SECRET_KEY)
.parseClaimsJws(accessToken);
} catch (Exception ignored) {
throw new BaseException(INVALID_JWT);
}
// 3. userIdx 추출
return claims.getBody().get("storeIdx",Integer.class); // jwt 에서 storeIdx를 추출합니다.
}
음식점 생성, 메뉴 수정, 메뉴 추가, 메뉴 삭제 등에서 JWT를 사용한다.