BWC semaJ
BWC semaJ

Reputation: 139

How do you get Client's IP address? (Spring WebFlux WebSocket)

As the title says, how do you obtain the details of the connection so to speak. Is there a way to get it through the WebSocketSession? I feel like I am missing something...

I need a way in order to ban ip addresses of bad users and also I wanted to display all users who are online on a map (like a dot on a map). I don't need help with the later I need help with getting a client's IP address.

I am using Spring's WebFlux WebSocket.

EDIT: I created a feature request : https://jira.spring.io/browse/SWF-1728

Upvotes: 0

Views: 3538

Answers (4)

michael_best
michael_best

Reputation: 138

I did it by

public Mono<ServerResponse> hello(ServerRequest request) {
    String remoteAddr = request.remoteAddress().get().getAddress().getHostAddress());

    return null;
}

Upvotes: 0

Brian Clozel
Brian Clozel

Reputation: 59076

The Servlet-based WebSocketSession object does provide that information. This seems to be missing from the reactive flavor.

You should create a Spring Framework issue to request this as an enhancement.

Upvotes: 1

Faiz Akram
Faiz Akram

Reputation: 558

you can find ip address using HttpServletRequest for example

 String remoteHost = request.getRemoteHost();
    String remoteAddr = request.getRemoteAddr();
    if (remoteAddr.equals("0:0:0:0:0:0:0:1")) {
        InetAddress localip = java.net.InetAddress.getLocalHost();
        remoteAddr = localip.getHostAddress();
        remoteHost = localip.getHostName();
    }

Upvotes: 0

bRiJeSh
bRiJeSh

Reputation: 810

WebSocketSession has a method called getRemoteAddress(), you can get the remote address using any of its implemetation.

Upvotes: 0

Related Questions