본문 바로가기
▶ Back-End/Java

VERIFY JWT(nimbus)

by 오늘도 코딩 2023. 10. 12.
728x90
반응형

JWT 검증 메소드

*JWE

*자세한 설명 생략

 

 

/**
 * JWT VERIFY
 * 
 * @param HttpServletRequest
 * @param resVO
 * @return CmmResVO
 */
public CmmResVO JWTVerify(HttpServletRequest req, CmmResVO resVO) {

    try {

        final String jwt = req.getHeader("Authorization");

        if(CmmUtile.nullCheck(jwt)) { return resVO.ERR_9005(); }

        /** JWE RSA KEYPAIR */
        final KeyFactory keyFactory = KeyFactory.getInstance("RSA");

        /* Decryption Key */
        final PKCS8EncodedKeySpec pKCS8EncodedKeySpec = new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey));
        final RSAPrivateKey rsaPrivateKey = (RSAPrivateKey) keyFactory.generatePrivate(pKCS8EncodedKeySpec);
        final RSADecrypter rsaDecrypter = new RSADecrypter(rsaPrivateKey);

        /** Parse */
        final EncryptedJWT decryptedJWE = EncryptedJWT.parse(jwt);

        /* Decryption */
        decryptedJWE.decrypt(rsaDecrypter);

        JWTClaimsSet claimsSet = decryptedJWE.getJWTClaimsSet();

        /** 토큰 만기 체크 */
        // -1 : 현재날짜 이전, 1 : 현재날짜 이후, 0 : 현재날짜와 같음 
        Integer expireCheck = claimsSet.getExpirationTime().compareTo(new Date());
        if("-1".equals(expireCheck.toString())){ return resVO.ERR_9006(); }

    } catch (Exception e) {
        resVO.ERR_9005();
        log.error("JWTVerify ERR Message :", e);
    }

    return resVO;
}

 

 

▷ 관련 글

 

Create JWT(nimbus)

JWT 생성 메소드 *JWE *자세한 설명 생략 /** * JWT CREATE * * @return String */ public String JWTCreate() { String jwt = ""; try { /** JWE RSA KEYPAIR */ final KeyFactory keyFactory = KeyFactory.getInstance("RSA"); /* Encryption Key */ final X509

coding-today.tistory.com

 

JWE Example(nimbus)

JWE Create / Parse *JWT 관련 자세한 설명 생략 ▷ Example /** * JWE Create / Parse * * ▶ JWE(JSON WEB ENCRYPTION) *→ 서버와 클라이언트 간 암호화된 데이터를 Token 화 한것 *→ claim 암호화 * * ▶ JWT(JSON WEB TOKEN) *→

coding-today.tistory.com

 

Create RSA Keypair(.pem)

RSA Keypair 생성 메소드 /** * RSA Keypair(.pem) Create Method */ public static HashMap getKeypair() { HashMap keypairMap = new HashMap(); try { KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("RSA"); keyPairGenerator.initialize(2048);

coding-today.tistory.com

 

 

728x90
728x90

'▶ Back-End > Java' 카테고리의 다른 글

로그파일 생성과 관리(logback-spring.xml)  (2) 2023.10.17
요청 필드 값 공통 체크  (0) 2023.10.12
Create JWT(nimbus)  (0) 2023.10.12
SpringBoot Security Login 기능 추가  (4) 2023.09.26
Objcet to String  (0) 2023.09.21

댓글