<document>
<header>
<product>resin</product>
<type>tutorial</type>
<title>Basic Security and Resin's XmlAuthenticator</title>
<description>
<p>
This tutorial covers the basics of JSP and
Servlet security and the use of Resin's XmlAuthenticator.
</p>
</description>
<tutorial-startpage>index.jsp</tutorial-startpage>
<keywords>
<keyword>XmlAuthenticator</keyword>
<keyword>com.caucho.http.security.XmlAuthenticator</keyword>
<keyword>authenticator</keyword>
<keyword>security</keyword>
<keyword>basic</keyword>
</keywords>
</header>

<body>
<summary/>

<s1 title="Files in this example">
<deftable>
<tr>
  <th>File</th>
  <th>Description</th>
</tr>
<tr>
  <td><viewfile-link file="WEB-INF/web.xml"/></td>
  <td>The main JSP/Servlet configuration file</td>
</tr>
<tr>
  <td><viewfile-link file="index.jsp"/></td>
  <td>The home page for the website</td>
</tr>
<tr>
  <td><viewfile-link file="login.jsp"/></td>
  <td>The JSP page containing the login form</td>
</tr>
<tr>
  <td><viewfile-link file="logout.jsp"/></td>
  <td>A JSP page that causes a logout</td>
</tr>
<tr>
  <td><viewfile-link file="home.jsp"/></td>
  <td>The home page for authenticated users.</td>
</tr>
<tr>
  <td><viewfile-link file="professors/index.jsp"/></td>
  <td>The more specific home page for Professor's, available only to users in role 'professor'</td>
</tr>
<tr>
  <td><viewfile-link file="students/index.jsp"/></td>
  <td>The more specific home page for Student's, available to users in role 'student' or in role 'professor'</td>
</tr>
<tr>
  <td><viewfile-link file="staff/index.jsp"/></td>
  <td>The more specific home page for Staff, available to users in role 'staff' or in role 'professor'</td>
</tr>
<tr>
  <td><viewfile-link file="inc/buttonbar.jspf"/></td>
  <td>An include file to render a button bar</td>
</tr>
<tr>
  <td><viewfile-link file="inc/footer.jspf"/></td>
  <td>An include file to render a footer</td>
</tr>
<tr>
  <td><viewfile-link file="inc/nobrowsercache.jspf"/></td>
  <td>An include file to stop the browser from caching pages</td>
</tr>
</deftable>
</s1>
    
<s1 title="Specifying roles">
<p>Each user belongs to one or more <var>roles</var>.  These roles are
similar to groups in Unix.  The possible roles are specified
in <code>web.xml</code>.</p>

<p>In this example, a user is either a <var>professor</var>, a
<var>student</var>, or a <var>staff</var>.  They can also optionally
have an additional role of <var>gryffindor</var>,
<var>slytherin</var>, <var>hufflepuf</var>, or <var>ravenclaw</var>,
indicating which house they belong to (or none at all).</p>

</s1>
    
<s1 title="Specifying secure areas">
<p>You can limit areas of the website to users in a certain
<var>role</var>.  You specify url patterns in <code>web.xml</code> and the role
that is required.  In JSP/Servlet terminology, this is called <var>Declarative Security</var>.
</p>
      
<example title="Declarative Security in web.xml">
&lt;web-app xmlns="http://caucho.com/ns/resin"
        xmlns:resin="urn:java:com.caucho.resin">
        
  &lt;resin:Allow url-pattern="/professors/*">
    &lt;resin:IfUserInRole role="professor"/>
  &lt;/resin:Allow>

&lt;/web-app>
</example>
</s1>
    
<s1 title="Making a login form">
<p>A login form can be used to retrieve the username and password
from the user.  The same form or a seperate form can be used
when the login fails.</p><p>

</p><p>In this example the login form and the error form are in the
same JSP file.  If the form is being redisplayed because of an
error the <code>login_error</code> request parameter is set to '1'.</p>
      
<example title="login-config: Getting Resin to use the login form">
&lt;web-app xmlns="http://caucho.com/ns/resin"
        xmlns:resin="urn:java:com.caucho.resin">
        
  &lt;resin:FormLogin>
      &lt;login-page&gt;/login.jsp&lt;/login-page&gt; 
      &lt;error-page&gt;/login.jsp?login_error=1&lt;/error-page&gt;
  &lt;/resin:FormLogin>

    ...
&lt;/web-app&gt;
</example>

<example title="An example login form">
&lt;form action='j_security_check' method='POST'&gt;
  &lt;table&gt;
    &lt;tr&gt;&lt;td&gt;User:&lt;/td&gt;&lt;td&gt;&lt;input type='text' name='j_username'&gt;&lt;/td&gt;&lt;/tr&gt;
    &lt;tr&gt;&lt;td&gt;Password:&lt;/td&gt;&lt;td&gt;&lt;input type='password' name='j_password'&gt;&lt;/td&gt;&lt;/tr&gt;

    &lt;tr&gt;&lt;td colspan='2'&gt;&lt;input type=submit&gt;&lt;/td&gt;&lt;/tr&gt;
  &lt;/table&gt;

  &lt;!--
    -  In case the user got here without a session, redirect
    -  successful requests to the home page for authenticated
    -  users.  (This is a non-standard, but useful field.)
    --&gt;
  &lt;input type='hidden' name='j_uri' value='/home.jsp'/&gt;
&lt;/form&gt;
</example>
</s1>
    
<s1 title="Causing a login to occur">
<p>Resin will cause a login to occur when a url that points to
a secure area is used.  You do not make a url directly to the
jsp page that contains the login form.</p>

<p>In this example, <code>home.jsp</code> is in a secure area, so an
unauthenticated user trying to access it will first be
presented with the login form.</p>

<example title="Accessing a jsp in a secure area causes the login to occur">
&lt;web-app xmlns="http://caucho.com/ns/resin"
        xmlns:resin="urn:java:com.caucho.resin">

  &lt;resin:Allow url-pattern="/home.jsp">
    &lt;!-- 
      '*' for a &lt;role-name&gt; means "authenticated user with any role"
      The user must be logged in with some kind of role to access
      the home page.  
    --&gt;
    &lt;resin:IfUserInRole role="*"/>
  &lt;/resin:Allow&gt;
  
&lt;/web-app&gt;
</example>

<example title="Making a link to cause a login">
&lt;a href="&lt;c:url value='/home.jsp'/&gt;"&gt;login&lt;/a&gt;
</example>
</s1>
    
<s1 title="Determining if the user is authenticated">
      
<p>If the user has done a successfull login, we say that they
have been <var>authenticated</var>.
<code>request.getUserPrincipal()</code> returns <code>null</code> if the 
user has not been authenticated.</p><p>

</p><p>In this example it is used to determine whether a 'login' or a
'logout' link should be presented.</p>

<example title="Determining if the user is authenticated">
&lt;c:choose&gt;
  &lt;c:when test="${empty pageContext.request.userPrincipal}"&gt;
    &lt;a href="&lt;c:url value='home.jsp'/&gt;"&gt;login&lt;/a&gt;
  &lt;/c:when&gt;
  &lt;c:otherwise&gt;
    &lt;a href="&lt;c:url value='logout.jsp'/&gt;"&gt;logout&lt;/a&gt;
  &lt;/c:otherwise&gt;
&lt;/c:choose&gt;
</example>
</s1>
    
<s1 title="Getting the current username">
      
<example title="Getting the current username">
Welcome &lt;c:out value="${pageContext.request.remoteUser}"/&gt;.
</example>
</s1>
    
<s1 title="Doing different things for different roles">
<p>You can also determine if a user is in a certain role in the
body of the page using <code>request.isUserInRole("role")</code>.
In JSP/Servlet terminology, this is called
<var>Programmatic Security</var>.</p>

<p>In this example, the <code>home.jsp</code> redirects the user to a
more specific home page if the user is a <var>professor</var>,
<var>student</var>, or <var>staff</var>.</p>

<example title="Programmatic Security using Java code">
&lt;%
  /** redirect to a more specific homepage if one is available */

  String home_url = null;

  if (request.isUserInRole("professor")) {
      home_url = "professors/";
  } else if (request.isUserInRole("staff")) {
      home_url = "staff/";
  } else if (request.isUserInRole("student")) {
      home_url = "students/";
  }

  if (home_url != null) {
      home_url = response.encodeRedirectUrl(home_url);
      response.sendRedirect(home_url);
      return; // don't do any more of the page
  }
%&gt;
</example>
</s1>

<s1 title="Stop the browser from caching pages">
      
<p>Pages with information that changes depending on whether or
not there is a known user should not be cached by the
browser.</p>

<p>In this example an include file <code>inc/nobrowsercache.jspf</code>
is used to send the HTTP headers that stop the browser from
caching the page.  It is used for each page that shows the
button bar at the top, because the button bar changes
depending on whether or not the user is logged in.</p>
      
<example title="Java code to stop the browser from caching the page">
&lt;%-- stop the browser from caching the page --%&gt;

&lt;%
  response.setHeader("Cache-Control","no-cache,post-check=0,pre-check=0");
  response.setHeader("Pragma","no-cache");
  response.setHeader("Expires","Thu,01Dec199416:00:00GMT");
%&gt;
</example>

<example title="Using inc/nobrowsercache.jsp">
&lt;%@ include file="/inc/nobrowsercache.jspf" %&gt;
</example>
</s1>
    
<s1 title="Causing a logout">
<p>A user can be logged out by invalidating the session.  This
causes all of the information stored in the session to be
lost.  It is especially important to make sure that the
logout page is not cached by the browser.</p>
      
<example title="Causing a logout with session.invalidate()">
&lt;%@ include file="/inc/nobrowsercache.jspf" %&gt;

&lt;%-- invalidating the session causes a loss of all session
     information, including the identity of the user
     --%&gt;

&lt;% session.invalidate(); %&gt;
</example>
</s1>

<s1 title="Using XmlAuthenticator">
<p>Resin provides an authenticator
<a javadoc="com.caucho.http.security.XmlAuthenticator"/> which is
useful for sites which have minimal security requirements.
The developer places entries for users in the authenticator
configuration, or in an xml file, or both.</p>
      
<p>The example below uses digest passwords.  Digest passwords avoid the storage of passwords in cleartext, and are discussed under the security section of the Resin documentation.</p>

<example title="Specifying the XmlAuthenticator as the authenticator to use">
&lt;web-app xmlns="http://caucho.com/ns/resin"
         resin:xmlns="urn:java:com.caucho.resin">

  &lt;!-- Resin-specific XmlAuthenticator configuration --&gt;
  &lt;resin:XmlAuthenticator>
      &lt;!-- Optionally put user information here.  --&gt;
      &lt;user&gt;pince:Txpd1jQc/xwhISIqodEjfw==:staff,website&lt;/user&gt;
      &lt;user&gt;filch:KmZIq2RKXAHV4BaoNHfupQ==:staff&lt;/user&gt;

      &lt;!-- You can also use an external file --&gt; 
      &lt;path&gt;WEB-INF/password.xml&lt;/path&gt;
  &lt;/resin:XmlAuthenticator&gt;
  
&lt;/web-app>
</example>

<example title="An XML file with usernames, passwords, and roles">
&lt;!-- password.xml --&gt;
&lt;authenticator&gt;
  &lt;!-- professors --&gt;
  &lt;user name='snape' password='I7HdZr7CTM6hZLlSd2o+CA==' roles='professor,slytherin'/&gt;
  &lt;user name='mcgonagall' password='4slsTREVeTo0sv5hGkZWag==' roles='professor,gryffindor'/&gt;

  &lt;!-- students --&gt;
  &lt;user name='harry' password='uTOZTGaB6pooMDvqvl2Lbg==' roles='student,gryffindor'/&gt;
  &lt;user name='dmalfoy' password='yI2uN1l97Rv5E6mdRnDFwQ==' roles='student,slytherin'/&gt;

  &lt;!-- alumni --&gt;
  &lt;user name='lmalfoy' password='sj/yhtU1h4LZPw7/Uy9IVA==' roles='alumni,gryffindor'/&gt;
&lt;/authenticator&gt;
</example>
</s1>

<s1 title="Documenting Roles">
<example>
&lt;web-app xmlns="http://caucho.com/ns/resin">

&lt;security-role&gt;
  &lt;role-name&gt;professor&lt;/role-name&gt;
&lt;/security-role&gt;

&lt;/web-app>
</example>
    
</s1>
</body>
</document>
