Theoretically, should be enough.
if (request.getRemoteAddr().equals(request.getLocalAddr())) { // Locally accessed. } else { // Remotely accessed. }
Strike>
Update according to the comments, request.getLocalAddr() seems to return 0.0.0.0 , which can actually happen when the server is behind the proxy server.
Instead, you can compare it with the addresses allowed by InetAddress .
private Set<String> localAddresses = new HashSet<String>(); @Override public 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"); } } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException { if (localAddresses.contains(request.getRemoteAddr())) {
In my case, localAddresses contains the following:
[192.168.1.101, 0:0:0:0:0:0:0:1, 127.0.0.1]
Balusc
source share