我认为最简单的方法是使用基本形式的身份验证。更多信息可以在这里找到http://docs.oracle.com/cd/E19798-01/821-1841/bncby/index.html。
将此添加到 web.xml
<security-constraint>
<display-name>Admin area</display-name>
<web-resource-collection>
<web-resource-name>Admin area</web-resource-name>
<description>Admin area</description>
<url-pattern>/admin/*</url-pattern>
<url-pattern>/login</url-pattern>
</web-resource-collection>
<auth-constraint>
<description/>
<role-name>admin</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<form-login-config>
<form-login-page>/login.xhtml</form-login-page>
<form-error-page>/login.xhtml</form-error-page>
</form-login-config>
</login-config>
<security-role>
<description/>
<role-name>admin</role-name>
</security-role>
创建登录页面
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<form method="POST" action="j_security_check" class="content-container admin-container">
<table>
<tr>
<td>Login:</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><input value="Login" /></td>
</tr>
</table>
</form>
</h:body>
</html>