본문 바로가기

Spring18

Spring Cloud Gateway CustomGlobalExceptionHandler 구현 기본 응답 Gateway에서 예외가 발생하면 기본으로 응답되는 포맷은 다음과 같음 { "timestamp": "2024-02-14T08:43:24.955+00:00", "path": "/first-service/hello-post", "status": 401, "error": "Unauthorized", "requestId": "3cafcce1" } 응답을 커스텀하거나 예외 발생 시 특별한 처리를 하려고 하는 경우, Gateway 필터를 구현하는 것처럼 ExceptionHandler를 구현할 수 있음 GatewayResponseVO Gateway에서 응답을 커스텀하기 위해 먼저 VO를 정의 @Data public class GatewayResponseVO { private int status; priva.. 2024. 2. 14.
Spring Cloud Gateway Filter에서 응답 보내기 Gateway Filter에서 인증 등이 실패했을 때, 에러 코드와 메세지를 응답하려고 하는 경우 @Override public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { // ... 로직 if (error) { // 인증 실패 등의 에러가 발생했을 경우 return sendUnauthorizedResponse(exchange); } // 정상 진행 return chain.filter(exchange); } private Mono sendUnauthorizedResponse(ServerWebExchange exchange) { // Map을 byte array로 변환하기 위해 선언, 다른 유틸 함수를 사용해도 됨 ObjectM.. 2024. 2. 8.
Spring Boot spring-security-oauth2-jose 사용해 JWKS URI로 JWT 검증하기 Dependency 추가 1. Maven org.springframework.security spring-security-oauth2-jose 6.2.1 2. Gradle // https://mvnrepository.com/artifact/org.springframework.security/spring-security-oauth2-jose implementation group: 'org.springframework.security', name: 'spring-security-oauth2-jose', version: '6.2.1' 구현 Spring Cloud Gateway에서 요청을 받았을 때 JWT를 검증하는 필터를 구현 @Slf4j @Component public class AuthorizationHe.. 2024. 2. 8.
Spring Cloud Gateway 구현 정리 버전 정보 > Spring Boot 3.2.2 > OpenJDK 17.0.8 > spring-cloud-starter-gateway 4.1.0 > spring-cloud-starter-netflix-eureka-client 4.1.0 Gradle Dependecies dependencies { implementation 'org.springframework.cloud:spring-cloud-starter-gateway' implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' } Eureka Client로 등록 @EnableDiscoveryClient @SpringBootApplication public cl.. 2024. 2. 6.
Java Spring JWT 생성 및 검증 로직 구현 이 글은 https://wadekang.tistory.com/70 포스트에 이어서 작성되었습니다. 해당 포스트에서 프로젝트 설정들을 확인하세요. Java Spring에서 JWKS(JSON Web Key Set) API 구현 버전 정보 java: openjdk 17.0.8 spring: Spring Boot 3.2.1 nimbus-jose-jwt: 9.31 프로젝트 생성 start.spring.io 에서 위와 같이 프로젝트 생성 JWK & JWKS JWK (JSON Web Key) Json Web Key (JWK)는 JSON 형식으로 표현된 공개키 또는 wadekang.tistory.com JWT JWT는 JSON Web Token의 약어로, 웹에서 정보를 안전하게 전달하기 위한 토큰 형식 중의 하나 주로.. 2024. 1. 11.
Java Spring에서 JWKS(JSON Web Key Set) API 구현 버전 정보 java: openjdk 17.0.8 spring: Spring Boot 3.2.1 nimbus-jose-jwt: 9.31 프로젝트 생성 start.spring.io 에서 위와 같이 프로젝트 생성 JWK & JWKS JWK (JSON Web Key) Json Web Key (JWK)는 JSON 형식으로 표현된 공개키 또는 비밀키를 나타내는 표준 JWK는 웹에서 사용되는 서비스 및 애플리케이션 간에 공유되는 키의 표준 표현 방법을 제공 JWK의 기본적인 구성 kty (Key Type): 키의 유형을 나타냄. RSA, EC, OCT 등이 있음 use (Key Use): 키의 사용 용도를 나타냄. 서명용인지, 암호화용인지 등을 지정 kid (Key ID): 키의 고유 식별자 alg (Algorith.. 2024. 1. 11.