초코비니

오랜만에 학습 과정에서... 본문

Spring Boot

오랜만에 학습 과정에서...

초코비니 2024. 2. 25. 18:32
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.

시행 착오들이 좀 있었다. 

갑자기 React를 해보고 싶은 생각에 책한권을 읽고 난후 이제 만들어보자 하면서...

백엔드로 스프링 부트부터 만들기 시작했다.

스프링부트를 시작하려니... 최소 자바 17버전;; ㅎㄷㄷ...

그나마 나한테는 최신이라고 할 수 있는 자바버전은 1.8이었다. ㅠ_ㅠ;

별 차이 있겠어? 다 똑같은 자바지?

기존 라이브러리랑 호환 안되는것들이 많았다.

Deprecate된건 그러려니 각자 알아서 하시길...

시큐리티에 WebSecurityConfigurerAdapter  의 Deprecate 

새로 만드는건데 없애고 시작해야겠다 싶어서...

다음 블로그를 참조했다.

https://covenant.tistory.com/277

 

WebSecurityConfigurerAdapter Deprecated 대응법

WebSecurityConfigurerAdapter란? 스프링 시큐리티를 사용하면 기본적인 시큐리티 설정을 하기 위해서 WebSecurityConfigurerAdapter라는 추상 클래스를 상속하고, configure 메서드를 오버라이드하여 설정하였습

covenant.tistory.com

기존 방식을 그대로 쓰다보면 Token을 만들때 이 코드를 많이 보았을것이다.

Claims claims = Jwts.claims().setSubject(authentication.getName());

Date now = new Date();

Date expireDate = new Date(now.getTime() + refreshExpirationTime);

 

String refreshToken = Jwts.builder()

.setClaims(claims)

.setIssuedAt(now)

.setExpiration(expireDate)

.signWith(SignatureAlgorithm.HS256, Base64.getEncoder().encodeToString(secretKey.getBytes()))

.compact();

이거 자바 17 환경에서 돌리면 에러난다. 

에러 내용은 java.lang.ClassNotFoundException: javax.xml.bind.DatatypeConverter 이다.

1.9 이후로 DatatypeConver 클래스가 제외되었다.그래서 이걸 해결하기위해

<dependency>

<groupId>io.jsonwebtoken</groupId>

<artifactId>jjwt-api</artifactId>

<version>0.12.3</version>

</dependency>

<dependency>

<groupId>io.jsonwebtoken</groupId>

<artifactId>jjwt-impl</artifactId>

<version>0.12.3</version>

<scope>runtime</scope>

</dependency>

<dependency>

<groupId>io.jsonwebtoken</groupId>

<artifactId>jjwt-jackson</artifactId>

<version>0.12.3</version>

<scope>runtime</scope>

</dependency>

 

<dependency>

<groupId>org.springdoc</groupId>

<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>

<version>2.2.0</version>

</dependency>

<dependency>

<groupId>jakarta.xml.bind</groupId>

<artifactId>jakarta.xml.bind-api</artifactId>

<version>3.0.1</version>

</dependency>

<dependency>

<groupId>org.glassfish.jaxb</groupId>

<artifactId>jaxb-runtime</artifactId>

<version>3.0.1</version>

</dependency>

maven들을 설정했다.

jsonwebtoken 은 자바 17 기준 0.12.3 이가 미니멈 버전이다.

근데 또 에러가 0.12.3으로 올리니 또 에러가 난다.

Type mismatch: cannot convert from ClaimsBuilder to Claims 엠x

그래서 다음과 같이 수정했다.

// 시크릿 키를 Base64 인코딩된 문자열에서 바이트 배열로 디코딩

Key key = Keys.hmacShaKeyFor(Decoders.BASE64.decode(secretKey));

// 바이트 배열을 사용해 Key 인스턴스 생성

// Key key = Keys.hmacShaKeyFor(keyBytes);

Date now = new Date();

Date expireDate = new Date(now.getTime() + accessExpirationTime);

 

return Jwts.builder()

.claims()

.subject(authentication.getName())

.issuedAt(now)

.expiration(expireDate)

.and()

.signWith(key)

.compact();

이걸로 토큰 생성에 겨우 성공했다. 뭔 삽질인가... ㅠ_ㅠ;

자바 1.8에서 17은 정말 많은게 변했다. 코드 사용법이 바뀐게 아니라 import해서 가져다 쓰는 것들이 바뀌어

오래된 라이브러리 참조시 에러가 많이 나게된다.

 

Comments