タイトル通り
あんまり使い道はないと思われるが、ちょっと簡単に削除が出来るのか気になったので個人的なメモ
消すのはRemoteIpValve
設定の話はこの辺
バージョン
- SpringBoot(
2.7.5
)
# application.properties server.tomcat.remoteip.protocol-header=x-forwarded-for
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory import org.springframework.boot.web.server.WebServerFactoryCustomizer import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration import org.apache.catalina.valves.RemoteIpValve @Configuration class TomcatConfig { @Bean fun remoteIpValveRemover(): WebServerFactoryCustomizer<TomcatServletWebServerFactory> { return WebServerFactoryCustomizer { factory: TomcatServletWebServerFactory -> factory.engineValves.removeIf { it is RemoteIpValve } } } }
import org.springframework.http.ResponseEntity import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.RequestHeader import org.springframework.web.bind.annotation.RestController import javax.servlet.http.HttpServletRequest @RestController class BaseController { @GetMapping("/") fun base( request: HttpServletRequest, @RequestHeader("X-Forwarded-For") xForwardedFor: String?, ): ResponseEntity<Any> { println("X-Forwarded-For: $xForwardedFor") return ResponseEntity.ok(request.remoteAddr) } }
Removeした場合
curl http://localhost:8080/ -H 'X-Forwarded-For: 192.168.0.1' 127.0.0.1 # サーバ側のログ X-Forwarded-For: 192.168.0.1
Removeしなかった場合
curl http://localhost:8080/ -H 'X-Forwarded-For: 192.168.0.1' 192.168.0.1 # サーバ側のログ X-Forwarded-For: null
結論: 簡単に出来る