如何从 java 中的 HttpHeaders 对象中查找客户端 ip 地址?
请在下面找到我的服务器端代码:
@Path("/login")
public class Login
{
DBConnection dBConnection = new DBConnection();
@GET
@Produces(MediaType.APPLICATION_JSON)
public String checkLogin(@QueryParam("username")String username,@QueryParam("password")String password,@QueryParam("clientid")String clientno,@QueryParam("callback")String callback,@Context HttpHeaders headers)
{
JSONObject loginresult = new JSONObject();
try
{
String query = QuerySelector.getQuery("user.checklogin");
loginresult = dBConnection.queryForJSONObject(query, username,password,clientno);
//here i need to get the client ip address from my headers object.
}
catch(Exception ex)
{
ex.printStackTrace();
}
return callback + "(" + loginresult.toString() + ")" ;
}
}
最佳答案
您可以通过 @Context HttpServletRequest 请求
获取它,就像:
String ip = request.getRemoteAddr();
新方法签名:
@GET
@Produces(MediaType.APPLICATION_JSON)
public String checkLogin(@QueryParam("username")String username,@QueryParam("password")String password,@QueryParam("clientid")String clientno,@QueryParam("callback")String callback,@Context HttpServletRequest request)
关于java - 如何从 java 中的 HttpHeaders 对象中查找客户端 ip 地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32835315/