Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get client IP in webflux?

I used to call HttpServletRequest.getRemoteAddr() to get client ip.

I'm wondering how can I get it via ServerWebExchange.

My best guess is:

  serverWebExchange.getRequest().getRemoteAddress().getAddress().getHostAddress();

Is it correct?

like image 357
anuni Avatar asked Oct 28 '22 09:10

anuni


1 Answers

you can use org.springframework.http.server.reactive.ServerHttpRequest,

String remoteAddress = serverHttpRequest.getRemoteAddress().getAddress().getHostAddress();

as it sad in doc: .reactive.ServerHttpRequest represents a reactive server-side HTTP request.

https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/http/server/reactive/ServerHttpRequest.html

like image 86
Blednov Roman Avatar answered Dec 30 '22 18:12

Blednov Roman