상세 컨텐츠

본문 제목

SpringSecurity [JWT 메소드 구경하기]

SpringSecurity

by 성찬우 2022. 7. 6. 03:23

본문

오늘은 jwt.class를 뜯어볼 것이다. 

 

기본적인 설명 ->

JSON  Web Token 은 당사자 간의 정보를 json 형태로 안전하게 전송하는 간단하고 독립적인 방법을 정의하는 개방형 표준(RFC 7519)입니다.

이 정보는 digitelly signed 되어있기 때문에 확인 및 신뢰가 가능합니다.

jwt 는 secret key 또는 RSA 또는 ECDSA 를 사용해서 공개/비공개 키 쌍을 사용하여 signed 될 수 있습니다. 

 

구조 -> 각각 . 을 통해 구분된다. { Header코드 . Payload 코드 . Signature 코드 }

  • Header: 토큰 유형과 해싱 알고리즘 타입을 정의
  • Payload: 토큰 발급자, 만료시간, 내용 등을 정의
  • Signature : 헤더와 페이로드 인코딩 값을 비밀키를 통해 암호화 하고 다시 해쉬하여 인코딩.

 

전체 코드 ->

 

더보기
public final class Jwts {

    private Jwts(){}

    /**
     * Creates a new {@link Header} instance suitable for <em>plaintext</em> (not digitally signed) JWTs.  As this
     * is a less common use of JWTs, consider using the {@link #jwsHeader()} factory method instead if you will later
     * digitally sign the JWT.
     *
     * @return a new {@link Header} instance suitable for <em>plaintext</em> (not digitally signed) JWTs.
     */
    public static Header header() {
        return new DefaultHeader();
    }

    /**
     * Creates a new {@link Header} instance suitable for <em>plaintext</em> (not digitally signed) JWTs, populated
     * with the specified name/value pairs.  As this is a less common use of JWTs, consider using the
     * {@link #jwsHeader(java.util.Map)} factory method instead if you will later digitally sign the JWT.
     *
     * @return a new {@link Header} instance suitable for <em>plaintext</em> (not digitally signed) JWTs.
     */
    public static Header header(Map<String, Object> header) {
        return new DefaultHeader(header);
    }

    /**
     * Returns a new {@link JwsHeader} instance suitable for digitally signed JWTs (aka 'JWS's).
     *
     * @return a new {@link JwsHeader} instance suitable for digitally signed JWTs (aka 'JWS's).
     * @see JwtBuilder#setHeader(Header)
     */
    public static JwsHeader jwsHeader() {
        return new DefaultJwsHeader();
    }

    /**
     * Returns a new {@link JwsHeader} instance suitable for digitally signed JWTs (aka 'JWS's), populated with the
     * specified name/value pairs.
     *
     * @return a new {@link JwsHeader} instance suitable for digitally signed JWTs (aka 'JWS's), populated with the
     * specified name/value pairs.
     * @see JwtBuilder#setHeader(Header)
     */
    public static JwsHeader jwsHeader(Map<String, Object> header) {
        return new DefaultJwsHeader(header);
    }

    /**
     * Returns a new {@link Claims} instance to be used as a JWT body.
     *
     * @return a new {@link Claims} instance to be used as a JWT body.
     */
    public static Claims claims() {
        return new DefaultClaims();
    }

    /**
     * Returns a new {@link Claims} instance populated with the specified name/value pairs.
     *
     * @param claims the name/value pairs to populate the new Claims instance.
     * @return a new {@link Claims} instance populated with the specified name/value pairs.
     */
    public static Claims claims(Map<String, Object> claims) {
        return new DefaultClaims(claims);
    }

    /**
     * Returns a new {@link JwtParser} instance that can be configured and then used to parse JWT strings.
     *
     * @return a new {@link JwtParser} instance that can be configured and then used to parse JWT strings.
     */
    public static JwtParser parser() {
        return new DefaultJwtParser();
    }

    /**
     * Returns a new {@link JwtBuilder} instance that can be configured and then used to create JWT compact serialized
     * strings.
     *
     * @return a new {@link JwtBuilder} instance that can be configured and then used to create JWT compact serialized
     * strings.
     */
    public static JwtBuilder builder() {
        return new DefaultJwtBuilder();
    }
}

 

기본적인 설명 

JWT 인터페이스의 인스턴스를 만드는데 쓰이는 Factory 클래스입니다. 

코드를 구현 클래스에 tightly 하게 연결 가능합니다. 

 

1. 기본 생성자 

private Jwts(){}

 

2. 

public static Header header() {
    return new DefaultHeader();
}

 

plaintext (일반적인 문자열)JWT 에 적합한 새로운 헤더 인스턴스를 생성합니다. 

if you will later digitally sign the JWT.

통신 방식의 JWT 사용은 일반적으로 사용이 되지 않으므로, jwsHeader() 펙토리메서드 사용을 고려해야합니다.

 

Returns : JWT에 적합한 새로운 헤더 인스턴스.

 

 

3.

public static Header header(Map<String, Object> header) {
    return new DefaultHeader(header);
}

2.번과 동일 Map 사용 유의 

 

4.

public static JwsHeader jwsHeader() {
    return new DefaultJwsHeader();
}

digitally sighned JWTs 에  적합한 새로운 jwsHeader 인스턴스를 반환합니다. 

Returns : digitally sighned JWT 에 적합한 새로운 JwsHeader 인스턴스.

참고 사항 : JwtBuilder.setHeader(Header)

 

5. 

public static JwsHeader jwsHeader(Map<String, Object> header) {
    return new DefaultJwsHeader(header);
}

4번과 동일 Map 사용 유의 

 

6. 

public static Claims claims() {
    return new DefaultClaims();
}

JWT의 Body (payload) 에 사용할 새로운 Claims 인스턴스를 반환합니다. 

Returns : JWT Body 로 사용할 새로운 Claims 인스턴스

 

7.

public static Claims claims(Map<String, Object> claims) {
    return new DefaultClaims(claims);
}

지정된 이름key/값value 쌍으로 채워진 새 Claims인스턴스를 반환합니다.

params : 새로운 Claims 인스턴스를 채울 key/value Map

Returns : 지정된 key/value 쌍으로 채워진 새로운 Claims 인스턴스.

 

8. 

public static JwtParser parser() {
    return new DefaultJwtParser();
}

JWT 문자열을 구성한 다음 구문 분석하는 데 사용할 수 있는 새 JwtParser 인스턴스를 반환합니다.

Returns : JWT 문자열을 구문 분석하는 데 사용할 수 있는 새 JwtParser 인스턴스.

 

9.

public static JwtBuilder builder() {
    return new DefaultJwtBuilder();
}

구성한 다음 JWT 압축 직렬화 문자열을 만드는 데 사용할 수 있는 새 JWtBuilder 인스턴스를 반환합니다.

 

Returns: 새로운 JWtBuilder 인스턴스를 구성하여 JWT 간단한 직렬화 문자열을 만드는 데 사용할 수 있습니다.

직렬화란?
serialization
객체를 전송 가능한 형태로 변형하는 것.
object -> Json 변환

 

 

 

전체적인 메소드의 설명은 끝이 낫다.

 

가장 핵심적인 부분은 9번 JwtBuilder 이다. 

builder 를 통해서 내부의 설정을 간단하게 할 수있다. 

 

JwtBuilder 내부 살펴 보기는 다음글에서 살펴 보자.

 

관련글 더보기

댓글 영역