What is the IP format returned by ServletRequest.getRemoteAddr()
The Javadoc of ServletRequest.getRemoteAddr() does not mention anything about t开发者_Python百科he format of returned IP addresses. This is annoying when implementing filters. What can we count on? Is there any official specification one can rely on? Thanks.
Let's start at ServletRequest#getRemoteAddr()
javadoc:
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
.
(emphasis mine)
Okay, let's read CGI REMOTE_ADDR
spec:
4.1.8. REMOTE_ADDR
The
REMOTE_ADDR
variable MUST be set to the network address of the client sending the request to the server.REMOTE_ADDR = hostnumber hostnumber = ipv4-address | ipv6-address ipv4-address = 1*3digit "." 1*3digit "." 1*3digit "." 1*3digit ipv6-address = hexpart [ ":" ipv4-address ] hexpart = hexseq | ( [ hexseq ] "::" [ hexseq ] ) hexseq = 1*4hex *( ":" 1*4hex )
The format of an IPv6 address is described in RFC 3513 [15].
There, you've all possible formats.
It's an IPv4 or IPv6 address in standard textual notation.
- For IPv4, it's the familiar "dot-decimal notation", e.g.
192.168.254.1
. I couldn't find an RFC that defines it. Wikipedia mentions that it's acceptable to use octal, binary or hexadecimal notation for any of the quads (and even to mix-and-match), but I've never seen anything but decimal. - For IPv6, it's specified in RFC-2373.
I was always getting IPv4 addresses in normal dot-decimal notation. But recently I noticed that for some clients HttpServletRequest.getRemoteAddr() returns strings that look like this: static.(IPv4 in dot-decimal notation).mydomain.net
精彩评论