Opening port 80 with a Java application on Ubuntu

What I need to do is run a Java application, which is a link to the Restlet's RESTful server server. And this service will be called by another application running on Google App Engine.

Due to the GAE limitation, every HTTP call is limited to ports 80 and 443 (http and https) with the HttpUrlConnection class. As a result, I have to deploy the server-side application on port 80 or 443.

However, since the application is running on Ubuntu, and those ports under 1024 cannot be accessed by a non-root user, then when launching my application, an Access Denied exception will be thrown.

The solutions that came to my mind include:

  • Changing the JRE security policy, which is a file, is located in /lib/security/java.policy, for grantjava.net.SocketPermission "* .80" "listen, connect, accept, allow" permission. However, without using the command line to include this file or overriding the contents in the JRE java.policy file, the same exception continues to be thrown.

  • try logging in as the root user, however, due to my unfamiliarity with Unix, I don't know how to do this.

  • another solution that I have not tried is to match all calls from 80 to a higher port, such as 1234, then I can deploy my application to 1234 without problems, and the request to send a GAE request to port 80. But how to connect the missing space is still a problem.

I am currently using the “hack” method, which is to pack the application into a jar file and sudo run the jar file with root privileges. It works now, but it is definitely not suitable in a real deployment environment.

So, if anyone knows about a solution, thank you very much!

+6
java ubuntu port
source share
3 answers

Solution 1: This will not change anything, this is not a Java restriction, this is the OS that prevents you from using privileged port numbers (ports below 1024).

Solution 2: IMO is not a good idea, there are good reasons not to run the process with root privileges.

Solution 3: Use setcap or iptables . See this previous question .

+2
source share

You can use iptables to redirect using something like this:

 iptables -t nat -A PREROUTING -i eth0 -p tcp --dport http -j REDIRECT --to-ports 8080 

Make changes permanent (saved after reboot) with:

 iptables-save 
+6
source share

A simpler solution is to install a reverse proxy server in Apache httpd, which Ubuntu will run for you on port 80 from /etc/init.d.

There are also ways to get here with iptables, but I have no recent personal experience. Now I have such a proxy server.

+1
source share

All Articles