如何在java中从HttpHeaders对象中查找客户端ip地址?

如何在java中从HttpHeaders对象中查找客户端ip地址?,java,http-headers,ip,client,Java,Http Headers,Ip,Client,如何在java中从HttpHeaders对象中查找客户端ip地址 请在下面找到我的服务器端代码: @Path("/login") public class Login { DBConnection dBConnection = new DBConnection(); @GET @Produces(MediaType.APPLICATION_JSON) public String checkLogin(@QueryParam("username")String u

如何在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() + ")" ;
    }
}
您可以尝试以下方法:

request.getHeader("Remote_Addr");
如果返回null,请尝试:

request.getRemoteAddr();

您可以通过
@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) 

为什么您认为客户端ip会出现在头中?我知道,在这种情况下获取rquest不是问题。您不仅可以通过上下文获取httpheades对象,还可以从客户端传递HttpServletRequest?是否有其他方法不更改客户端代码?您只需在checkLogin方法中作为参数注入HttpServletRequest而不是HttpHeaders。无需更改客户端,web服务实现调用中始终有一个请求