简体   繁体   中英

Get the requester IP address from inside Quarkus

So let me first try to explain what I need and the use case so it will be easy to circle around my request:

We are building a voting system based on Quarkus and have to restrict under certain conditions the voter to be able to vote, eg he registered that he lives in France but the request is coming from Zambia.

For that we are thinking about getting the IP address of the request and with the help of ip2location.com, we plan on determining the location of the voter. The only mention that I saw to get the IP address was in a sample code regarding logging and I have found none to be able to get it from inside my code to be processed (in our case, get the IP, call the ip2location service, match against the database, reject or validate the vote).

So how can I get that IP address from inside an application built with Quarkus?

@Inject HttpServletRequest or @Inject HttpRequest definitely don't work.

Regards, D.

You can use:

   @Context
   HttpServerRequest request;

...


   request.remoteAddress();

in a JAX-RS resource class.

But even that won't be enough if you are behind a proxy. Im that case you would likely need to the read the X-Forwarded-For HTTP header.

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