简体   繁体   中英

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

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 .

WebSocketSession 有一个名为 getRemoteAddress() 的方法,您可以使用它的任何实现来获取远程地址。

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();
    }

I did it by

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

    return null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM