Tomcat下的JAAS实例

创建文件login.jsp和error.jsp

login.jsp的代码如下

[c-sharp]  view plain  copy
  1. <html>  
  2.   <head>  
  3.     <meta HTTP-EQUIV="Content-Type" Content="text-html; charset=gbk">  
  4.     <title>login</title>  
  5.   </head>  
  6.   <body>  
  7.     <form method="POST" action="j_security_check">  
  8.       姓名:<input type="text" name="j_username"/><br/>  
  9.       密码:<input type="password" name="j_password"/><br/>  
  10.       <input type="submit" value="提交"/>  
  11.     </form>  
  12.   </body>  
  13. </html>  
error.jsp的代码如下

[xhtml]  view plain  copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  3. <html>  
  4.   <head>      
  5.     <title>错误页面</title>  
  6.   </head>  
  7.   <body>  
  8.     <center><h1><font color="gray">页面发生错误</font></h1></center>  
  9.   </body>  
  10. </html>  
  创建一个文件index.jsp

index.jsp代码如下

[xhtml] view plain copy
  1. <%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>  
  2. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">  
  3. <html>  
  4.   <head>     
  5.     <title>主页</title>  
  6.   </head>  
  7.   <body bgcolor="#FFFFFF">  
  8.     request.FORM_AUTH:<%=request.FORM_AUTH%><br/>  
  9.     request.getRemoteUser():<%=request.getRemoteUser()%><br/>  
  10.   </body>  
  11. </html>  
设置配置文件

web.xml的代码如下

[xhtml] view plain copy
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <web-app version="2.5"   
  3.     xmlns="http://java.sun.com/xml/ns/javaee"   
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"   
  5.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee   
  6.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  7.   <security-constraint>  
  8.     <web-resource-collection>  
  9.         <web-resource-name>protected-resource</web-resource-name>  
  10.         <url-pattern>/*</url-pattern>  
  11.         <http-method>HEAD</http-method>  
  12.         <http-method>GET</http-method>  
  13.         <http-method>POST</http-method>  
  14.         <http-method>PUT</http-method>  
  15.         <http-method>DELETE</http-method>  
  16.     </web-resource-collection>  
  17.     <auth-constraint>  
  18.         <role-name>role1</role-name>  
  19.     </auth-constraint>  
  20.     <user-data-constraint>  
  21.         <transport-guarantee>NONE</transport-guarantee>  
  22.     </user-data-constraint>  
  23.   </security-constraint>  
  24.   <login-config>  
  25.     <auth-method>FORM</auth-method>  
  26.     <form-login-config>  
  27.         <form-login-page>/login.jsp</form-login-page>  
  28.         <form-error-page>/error.jsp</form-error-page>  
  29.     </form-login-config>  
  30.   </login-config>  
  31.   <security-role>  
  32.     <description>Role1</description>  
  33.     <role-name>role1</role-name>  
  34.   </security-role>  
  35. </web-app>  
打开tomcat目录下的conf/tomcat-users.xml文件,如下内容

[c-sharp] view plain copy
  1. <?xml version='1.0' encoding='utf-8'?>  
  2. <tomcat-users>  
  3.   <role rolename="tomcat"/>  
  4.   <role rolename="role1"/>  
  5.   <user username="tomcat" password="tomcat" roles="tomcat"/>  
  6.   <user username="role1" password="tomcat" roles="role1"/>  
  7.   <user username="both" password="tomcat" roles="tomcat,role1"/>  
  8. </tomcat-users>  

启动tomcat,在浏览器中输入地址http://localhost:8080/JAASPrj/,显示的内容不是/web/index.html,而是login.jsp的内容,输入both或者role1的用户名和密码,将会看到web/index.html的内容,当然,如果输入错误,则会提示错误信息。验证通过后,我们可以看到如下内容:

request.FORM_AUTH:FORM

request.getRemoteUser():both //用户名


你可能感兴趣的:(tomcat,jaas)