내일은개발왕

[Springboot] Spring API Gateway + SpringWebFlux 로그인 폼 제발 사라져줘 제발;;; 본문

오류삽질,기타삽질

[Springboot] Spring API Gateway + SpringWebFlux 로그인 폼 제발 사라져줘 제발;;;

NDN 2025. 2. 9. 12:06

📌 해결하기전에 잠깐! 개념 짚고 가자~

Spring Cloud Gateway : Spring web Flux, Project Reactor 를 기반으로 구축된 Routing 및 보안 기능등을 제공하는 API Gateway다.

Routing : 사전의 뜻처럼 경로를 지정해주는 역할을 한다. ex) http://a.com 으로 요청이 오면 http://b.com으로 연결 

Gateway : 다른 네트워크로 접속하기 위해 거치는 관문, 즉 게이트 웨이는 A에서 오는 요청을 다른 곳으로 보내주는 역할을 한다고 이해하면 편하다.


🚨 문제 상황

 spring cloud GateWay 프로젝트에 관련 의존성을 추가해주고 local에서 테스트 시 security login폼이 계속 뜨는 상황


👍해결

Spring Cloud Gateway는 공식 문서에 기술되어 있는 것처럼 기본적으로 spring WebFlux로 구현되어 있다.
그래서 기본적으로 security가 포함이 되어 있긴한데 다른 점은, 이친구는 WebFluxSecurity를 사용한다.
그렇기에 아무리 spring security를 사용해 form을 disable 해도 먹히지 않았던 것이 원인이었다.

이를 해결하기 위해서, 새로운 java class 파일 하나 만들어 주고, 아래와 같은 코드를 작성해주면 된다.
혹시 모르니 의존성도 추가했다.

dependencies {
	implementation 'org.springframework.cloud:spring-cloud-starter-gateway'
	implementation 'org.springdoc:springdoc-openapi-starter-webflux-ui:2.7.0'
	implementation 'org.springframework.boot:spring-boot-starter-security'
	implementation 'org.springframework:spring-webflux:6.2.2'
	implementation 'io.grpc:grpc-netty:1.70.0'
}
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;

@Configuration
@EnableWebFluxSecurity //WebFluxSecurity를 조정해줘야함
public class WebFluxSecurityConfig {
    @Bean
    public SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http){
       //return http. 이렇게해서 필요한 부분들 조정해주면 gate way에 적용이 된다.
    }
}

 


진짜 ㅋㅋ 이거 하나 잡겠다고 회사에서 이틀간 난리쳤는데 안되서 어제 집에서 해결했다 ㅋ.....
하.......... 개킹받음 그래도 이거 절대 안까먹을듯 ㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋㅋ 

https://docs.spring.io/spring-cloud-gateway/reference/spring-cloud-gateway/starter.html

 

How to Include Spring Cloud Gateway :: Spring Cloud Gateway

If you include the starter, but you do not want the gateway to be enabled, set spring.cloud.gateway.enabled=false.

docs.spring.io