| ||||||||||||||||||||||||||||||||
Resin 3.1 Documentation Examples Changes Quercus Database Amber EJB SOA/ESB IoC JMS Servlet JMX Hessian Security |
This tutorial covers the basics of JSP and Servlet security and the use of Resin's XmlAuthenticator. Files in this example
Specifying rolesEach user belongs to one or more In this example, a user is either a , a , or a . They can also optionally have an additional role of , , , or , indicating which house they belong to (or none at all).<security-role> <role-name>professor</role-name> </security-role> Specifying secure areasYou can limit areas of the website to users in a certain
<security-constraint> <web-resource-collection> <web-resource-name>Professors</web-resource-name> <url-pattern>/professors/*</url-pattern> </web-resource-collection> <auth-constraint> <role-name>professor</role-name> </auth-constraint> </security-constraint> Making a login formA 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. 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 <login-config> <auth-method>form</auth-method> <form-login-config> <form-login-page>/login.jsp</form-login-page> <form-error-page>/login.jsp?login_error=1</form-error-page> </form-login-config> ... </login-config> <form action='j_security_check' method='POST'> <table> <tr><td>User:</td><td><input type='text' name='j_username'></td></tr> <tr><td>Password:</td><td><input type='password' name='j_password'></td></tr> <tr><td colspan='2'><input type=submit></td></tr> </table> <!-- - 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.) --> <input type='hidden' name='j_uri' value='/home.jsp'/> </form> Causing a login to occurResin 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. In this example, <security-constraint> <web-resource-collection> <web-resource-name>Home</web-resource-name> <url-pattern>/home.jsp</url-pattern> </web-resource-collection> <auth-constraint> <!-- '*' for a <role-name> means "authenticated user with any role" The user must be logged in with some kind of role to access the home page. --> <role-name>*</role-name> </auth-constraint> </security-constraint> <a href="<c:url value='/home.jsp'/>">login</a> Determining if the user is authenticatedIf the user has done a successfull login, we say that they
have been In this example it is used to determine whether a 'login' or a 'logout' link should be presented. <c:choose> <c:when test="${'${'}empty pageContext.request.userPrincipal}"> <a href="<c:url value='home.jsp'/>">login</a> </c:when> <c:otherwise> <a href="<c:url value='logout.jsp'/>">logout</a> </c:otherwise> </c:choose> Getting the current usernameWelcome <c:out value="${'${'}pageContext.request.remoteUser}"/>. Doing different things for different rolesYou can also determine if a user is in a certain role in the
body of the page using In this example, the <% /** 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 } %> Stop the browser from caching pagesPages with information that changes depending on whether or not there is a known user should not be cached by the browser. In this example an include file <%-- stop the browser from caching the page --%> <% response.setHeader("Cache-Control","no-cache,post-check=0,pre-check=0"); response.setHeader("Pragma","no-cache"); response.setHeader("Expires","Thu,01Dec199416:00:00GMT"); %> <%@ include file="/inc/nobrowsercache.jspf" %> Causing a logoutA 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. <%@ include file="/inc/nobrowsercache.jspf" %> <%-- invalidating the session causes a loss of all session information, including the identity of the user --%> <% session.invalidate(); %> Using XmlAuthenticatorResin provides an authenticator 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. 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. <!-- Resin-specific XmlAuthenticator configuration --> <authenticator> <type>com.caucho.server.security.XmlAuthenticator</type> <init> <!-- Optionally put user information here. --> <user>pince:Txpd1jQc/xwhISIqodEjfw==:staff,website</user> <user>filch:KmZIq2RKXAHV4BaoNHfupQ==:staff</user> <!-- You can also use an external file --> <path>WEB-INF/password.xml</path> </init> </authenticator> <!-- password.xml --> <authenticator> <!-- professors --> <user name='snape' password='I7HdZr7CTM6hZLlSd2o+CA==' roles='professor,slytherin'/> <user name='mcgonagall' password='4slsTREVeTo0sv5hGkZWag==' roles='professor,gryffindor'/> <!-- students --> <user name='harry' password='uTOZTGaB6pooMDvqvl2Lbg==' roles='student,gryffindor'/> <user name='dmalfoy' password='yI2uN1l97Rv5E6mdRnDFwQ==' roles='student,slytherin'/> <!-- alumni --> <user name='lmalfoy' password='sj/yhtU1h4LZPw7/Uy9IVA==' roles='alumni,gryffindor'/> </authenticator>
|