gpt4 book ai didi

java - 如何从 ContainerRequestContext JERSEY 2.1 获取 IP?

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:52:41 25 4
gpt4 key购买 nike

我的代码是这样的:

public void filter(ContainerRequestContext request) throws IOException 
{
// can I get Ip from request?????

}

如何从request获取IP地址?

最佳答案

根据这个JIRA门票,基于此 discussion ,他们添加了对将 HttpServletRequest 注入(inject)过滤器的支持,上述 JIRA 的代码片段如下所示:

public class MyRequestFilter implements ContainerRequestFilter {

@Context
private HttpServletRequest servletRequest;

然后您可以使用 HttpServletRequest API 获取远程 IP,请参阅完整的 Javadoc here , 摘录:

getRemoteAddr

java.lang.String getRemoteAddr()

Returns the Internet Protocol (IP) address of the client or last proxy that sent the request. For HTTP servlets, same as the value of the CGI variable REMOTE_ADDR.

Returns:

a String containing the IP address of the client that sent the request

关于java - 如何从 ContainerRequestContext JERSEY 2.1 获取 IP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28280491/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com