从理论上讲,以下内容应足够。
if (request.getRemoteAddr().equals(request.getLocalAddr())) { // Locally accessed.} else { // Remotely accessed.}*根据评论 *更新 ,
request.getLocalAddr()似乎返回
0.0.0.0,当服务器位于代理后面时确实可能发生。
您可能需要将其与解析的地址进行比较
InetAddress。
private Set<String> localAddresses = new HashSet<String>();@Overridepublic void init(FilterConfig config) throws ServletException { try { localAddresses.add(InetAddress.getLocalHost().getHostAddress()); for (InetAddress inetAddress : InetAddress.getAllByName("localhost")) { localAddresses.add(inetAddress.getHostAddress()); } } catch (IOException e) { throw new ServletException("Unable to lookup local addresses"); }}@Overridepublic void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { if (localAddresses.contains(request.getRemoteAddr())) { // Locally accessed. } else { // Remotely accessed. }}就我而言,
localAddresses包含以下内容:
[192.168.1.101, 0:0:0:0:0:0:0:1, 127.0.0.1]



