This document is outdated! Please change to HOWTO: Setup Jetty 11 as a Service on Debian with SSL Support and Basic Authentication
When operating a webserver it may be useful to have a simple mechanism to protect particular web contents against public access, for example to create a member's area within a website, containing information which is accessible by members only, and require user/password authentication to be read or downloaded. Most web hosters run Apache webservers and thus provide the well known htaccess/htpassword mechanism for that purpose. That mechanism basicly works by placing text files named .htaccess, in which access information to the folder and it's subfolders or to particular files is placed, in folders of the site's webspace. Each .htaccess file contains a reference to a list of user/password credentials stored in a file named .password, which is usually placed in some central administration folder of the site's webspace.
That mechanism, however, is specific to the most popular Apache webserver. Jetty provides a similar mechanism, which is little more
complex, but still quite simple. Unfortunately it is not well documented, so here's a brief introduction to get started.
The following example is based on Jetty 9.3.6, an instance of which is running on a Debian or Ubuntu driven computer.
A realm can be viewed as a part of the webserver-accessible file system, which can be affected by the specified authentification mechanism.
If Jetty s installed on /opt/jetty, this is, by default, everything underneath /opt/jetty/webapps. In this HOWTO, we regard this default only, skipping more sophisticated realm definition options provided by Jetty.
To configure a realm in Jetty, create the following entry in the jetty.xml file:
vi /opt/jetty/etc/jetty.xml
vi /opt/jetty/etc/realm.properties guest: guest11,user-role jim: MD5:d45e977bfef260da159d651b9de7035d,user-roleThere are two users, guest and jim, registered in the realm.properties file. guest has the plain password "guest11", while jim has the MD5-coded Password "zigzag". Both users bear the role user-role. While user guest's password is written in plain text, user jim has his password coded into a MD5 string. Note that MD5: needs to be the password prefix in realm.properties. There are plenty of sources in the web to create the MD5 sum representation of a (password) string, but you can also use the Unix command line:
echo -n "zigzag" | md5sum d45e977bfef260da159d651b9de7035d -So, the general format of a user entry in a realm porperty file is
<user>: <password-string>,<role> where <user> is a username, followed by a : and a whitespace character, <password-string> is the user's password, either as plain text or coded as MD5, SHA, OBFuscated or CRYPTed string with their respective prefixes MD5:, SHA:, OBF: or CRYPT: ( no blank between : and the password string ) and <role> is an identifier to distinguish users with different access according to their roles, e.g. user-role, admin-role, ...For more user options, check the Jetty documentation. Credentials can be added, changed or removed in the realm property file as needed.
/opt/jetty/webapps/ +-- README.TXT +-- root ��� +-- index.html +-- samplewebservice1 ��� +-- servicehome.class ��� +-- servicetool.class ��� +-- WEB-INF ��� +-- web.xml +-- samplewebservice2.war +-- samplewebservice3.war +-- samplesite +-- index.htmlNow, in comparison to Apache, the realm.properties file can be regarded as the Jetty counterpart to the .htpasswd file. And within the sammplewebservice1 web application in the above example, the counterpart of the Apache .htaccess file would be a section of the WEB-INF/web.xml file from the Jetty perspective. Before having a closer look into WEB-INF/web.xml, let's turn to the example once more. Web applications like samplewebservice1, samplewebservice2 and samplewebservice3 always need and include a WEB-INF/web.xml file. This is immediately visible for samplewebservice1, but also the samplewebservice2.war and samplewebservice3.war WARs both contain a WEB-INF/web.xml file. However "standalone" HTML sites like samplesite do not contain a WEB-INF/web.xml file by default, because Jetty doesn't need it to just serve HTML pages. So, if authentication is to be established for HTML pages, the WEB-INF/web.xml needs to be created explicitly within the samplesite folder, and the file structure of the site will then look like this:
/opt/jetty/webapps/ . . . +-- samplesite +-- index.html +-- WEB-INF � +-- web.xml . . .Now, let's have a look into a sample web.xml file:
/opt/jetty/webapps/samplesite/ +-- index.html +-- protected ��� +-- protected_document.html +-- public ��� +-- public_document.html +-- WEB-INF +-- web.xmlNow, the
<url-pattern>/protected/*</url-pattern>line in security-constraint / web-resource-collection section of the web.xml file indicates that all contents (files) in the protected subfolder requires authentication, whereas the root folder, including index.html as well as all the contents of the public subfolder are public and can be accessed without authentication.
Documentations on whitehorseplanet.org
HOME (whitehorseplanet.org)