📄 [转贴加修改]j2ee安全策略:为tomcat页面设置访问权限--rosonsandy.htm
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0055)http://rosonsandy.blogdriver.com/rosonsandy/987063.html -->
<HTML><HEAD><TITLE>[转贴加修改]J2EE安全策略:为tomcat页面设置访问权限--rosonsandy</TITLE>
<META http-equiv=Content-Type content="text/html; charset=GBK">
<META http-equiv=Pragma content=no-cache>
<META http-equiv=Cache-Control content=no-cache>
<META http-equiv=Expires content=0>
<META
content="[转贴]论J2EE开发Web应用程序中的安全认证机制4[转贴加修改]J2EE安全策略:为tomcat页面设置访问权限seed 君は僿に似てい 博客 博客动力 blog blogdriver blogger 中国"
name=description>
<META
content="rosonsandy [转贴]论J2EE开发Web应用程序中的安全认证机制4[转贴加修改]J2EE安全策略:为tomcat页面设置访问权限seed 君は僿に似てい 博客 博客动力 blog blogdriver blogger 中国"
name=keywords><LINK
href="[转贴加修改]J2EE安全策略:为tomcat页面设置访问权限--rosonsandy.files/diary.css" type=text/css
rel=stylesheet>
<SCRIPT language=JavaScript
src="[转贴加修改]J2EE安全策略:为tomcat页面设置访问权限--rosonsandy.files/UBB.js"></SCRIPT>
<SCRIPT src="[转贴加修改]J2EE安全策略:为tomcat页面设置访问权限--rosonsandy.files/blog.js"
type=text/javascript></SCRIPT>
<META content="MSHTML 6.00.2900.3199" name=GENERATOR></HEAD>
<BODY>
<DIV id=container>
<DIV id=header>
<H1 class=title><A
href="http://rosonsandy.blogdriver.com/rosonsandy/index.html">rosonsandy</A></H1></DIV>
<DIV id=category><A title=上一篇
href="http://rosonsandy.blogdriver.com/rosonsandy/987047.html">[转贴]论J2EE开发Web应用程序中的安全认证机制4</A>-
-| <A href="http://rosonsandy.blogdriver.com/rosonsandy/index.html">回首页</A> | <A
href="http://rosonsandy.blogdriver.com/rosonsandy/catalog_2005.html">2005年索引</A>
| - -<A title=下一篇
href="http://rosonsandy.blogdriver.com/rosonsandy/990178.html">seed
君は僿に似てい</A></DIV>
<DIV class=entity>
<H2 class=diaryTitle>[转贴加修改]J2EE安全策略:为tomcat页面设置访问权限- -</H2>
<P>
<P><FONT face=Arial>在<SPAN
lang=EN-US>web</SPAN>应用中,对页面的访问控制通常通过程序来控制,流程为:</FONT><SPAN
lang=EN-US><BR></SPAN><FONT face=Arial>登录<SPAN lang=EN-US> -> </SPAN>设置<SPAN
lang=EN-US>session -> </SPAN>访问受限页面时检查<SPAN
lang=EN-US>session</SPAN>是否存在,如果不存在,禁止访问<SPAN lang=EN-US>
<P></SPAN></FONT>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 9pt; WORD-BREAK: break-all; LINE-HEIGHT: 150%; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><SPAN
style="LINE-HEIGHT: 150%; FONT-FAMILY: 新宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><FONT
face=Arial>对于较小型的<SPAN lang=EN-US>web</SPAN>应用,可以通过<SPAN
lang=EN-US>tomcat</SPAN>内置的访问控制机制来实现权限控制。采用这种机制的好处是,程序中无需进行权限控制,完全通过对<SPAN
lang=EN-US>tomcat</SPAN>的配置即可完成访问控制。<SPAN lang=EN-US>
<P></SPAN></FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 9pt; WORD-BREAK: break-all; LINE-HEIGHT: 150%; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><SPAN
style="LINE-HEIGHT: 150%; FONT-FAMILY: 新宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><FONT
face=Arial>为了在<SPAN lang=EN-US>tomcat</SPAN>页面设置访问权限控制,在项目的<SPAN
lang=EN-US>WEB-INFO/web.xml</SPAN>文件中,进行如下设置:</FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 9pt; WORD-BREAK: break-all; LINE-HEIGHT: 150%; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><SPAN
style="LINE-HEIGHT: 150%; FONT-FAMILY: 新宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><FONT
face=Arial>web.xml</FONT></SPAN><SPAN
style="LINE-HEIGHT: 150%; FONT-FAMILY: 新宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"></SPAN></P>
<CENTER>
<TABLE cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400
align=center borderColorLight=#000000 border=1>
<TBODY>
<TR>
<TD class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6><PRE><FONT face=Arial><?xml version="1.0" encoding="ISO-8859-1"?></FONT></PRE><PRE><FONT face=Arial><!DOCTYPE web-app<BR> PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"<BR> "</FONT><A href="http://java.sun.com/dtd/web-app_2_3.dtd"><FONT face=Arial>http://java.sun.com/dtd/web-app_2_3.dtd</FONT></A><FONT face=Arial>"></FONT></PRE><PRE><FONT face=Arial><web-app><BR><security-constraint><BR> <web-resource-collection><BR> <web-resource-name>Protected Area</web-resource-name><BR> <url-pattern>/*</url-pattern><BR> <http-method>GET</http-method><BR> <http-method>POST</http-method><BR> </web-resource-collection><BR> <auth-constraint><BR> <role-name>student</role-name><BR> <role-name>tomcat</role-name><BR> </auth-constraint><BR></security-constraint></FONT></PRE><PRE><FONT face=Arial><login-config><BR> <auth-method>FORM</auth-method><BR> <realm-name>Default</realm-name><BR> <form-login-config><BR> <form-login-page>/logon.jsp</form-login-page><BR> <form-error-page>/error.jsp</form-error-page><BR> </form-login-config><BR></login-config><BR><security-role><BR> <role-name>student</role-name><BR></security-role><BR></web-app><CCID_CODE /></CCID_CODE /></FONT></PRE></TD></TR></TBODY></TABLE></CCID_NOBR
/></CENTER>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 9pt; WORD-BREAK: break-all; LINE-HEIGHT: 150%; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><SPAN
style="LINE-HEIGHT: 150%; FONT-FAMILY: 新宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><FONT
face=Arial>其中,<SPAN lang=EN-US><url-pattern></SPAN>中指定受限的<SPAN
lang=EN-US>url</SPAN>,可以使用通配符<SPAN
lang=EN-US>*</SPAN>,通常对整个目录进行访问权限控制。</FONT><SPAN lang=EN-US><BR><FONT
face=Arial><auth-constraint></FONT></SPAN><FONT
face=Arial>中指定哪些角色可以访问<SPAN lang=EN-US><url-pattern></SPAN>指定的<SPAN
lang=EN-US>url</SPAN>,在<SPAN
lang=EN-US><role-name></SPAN>中可以设置一个或多个角色名。</FONT><SPAN
lang=EN-US><BR></SPAN></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 9pt; WORD-BREAK: break-all; LINE-HEIGHT: 150%; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><SPAN
style="LINE-HEIGHT: 150%; FONT-FAMILY: 新宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><FONT
face=Arial>使用的角色名来自<SPAN lang=EN-US>tomcat</SPAN>的配置文件<SPAN
lang=EN-US>${CATALINA_HOME}/conf/tomcat-users.xml</SPAN>。<SPAN lang=EN-US>
<P></SPAN></FONT></SPAN></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 9pt; WORD-BREAK: break-all; LINE-HEIGHT: 150%; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><FONT face=Arial><SPAN lang=EN-US
style="LINE-HEIGHT: 150%; FONT-FAMILY: 新宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><login-config></SPAN><SPAN
style="LINE-HEIGHT: 150%; FONT-FAMILY: 新宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt">中设置登录方式,<SPAN
lang=EN-US><auth-method></SPAN>的取值为<SPAN lang=EN-US>BASIC</SPAN>或<SPAN
lang=EN-US>FORM</SPAN>。如果为<SPAN
lang=EN-US>BASIC</SPAN>,浏览器在需要登录时弹出一个登录窗口。如果为<SPAN
lang=EN-US>FORM</SPAN>方式,需要指定登录页面和登录失败时的提示信息显示页面。</SPAN></FONT></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 9pt; WORD-BREAK: break-all; LINE-HEIGHT: 150%; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><SPAN
style="LINE-HEIGHT: 150%; FONT-FAMILY: 新宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"><FONT
face=Arial>logon.jsp</FONT></SPAN><SPAN
style="LINE-HEIGHT: 150%; FONT-FAMILY: 新宋体; mso-bidi-font-size: 10.5pt; mso-bidi-font-family: Arial; mso-font-kerning: 0pt"></SPAN><BR><FONT
face=Arial>error.jsp</FONT></P>
<CENTER><CCID_NOBR />
<TABLE cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400
align=center borderColorLight=#000000 border=1>
<TBODY>
<TR>
<TD class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6><PRE><CCID_CODE /><FONT face=Arial><%@ page import="java.util.*" %><BR><center><BR><form action="j_security_check" method=post><BR><table><BR><tr><BR> <td align="center" ><BR> <table border="0"><BR> <tr><BR> <td><b>USERNAME:</b></td><BR> <td><BR> <input type="text" size="15" name="j_username"> <BR> </td><BR> </tr><BR> <tr><BR> <td><b>Password:</b></td><BR> <td> <BR> <input type="password" size="15" name="j_password"><BR> </td><BR> </tr><BR> <tr><BR> <td></td><BR> <td align="right"> <BR> <input type="submit" value="Submit"><BR> </td><BR> </tr><BR> <tr><BR> <td><br></td><BR> </tr><BR></table><BR></td><BR></tr><BR></table><BR></form><BR></center></FONT></PRE></TD></TR></TBODY></TABLE></CCID_NOBR
/></CENTER>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 9pt; WORD-BREAK: break-all; LINE-HEIGHT: 150%; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><BR></P>
<CENTER><CCID_NOBR />
<TABLE cellSpacing=0 borderColorDark=#ffffff cellPadding=2 width=400
align=center borderColorLight=#000000 border=1>
<TBODY>
<TR>
<TD class=code style="FONT-SIZE: 9pt" bgColor=#e6e6e6><PRE><CCID_CODE /><FONT face=Arial> <%@ page import="java.util.*" %><BR><h2>Login Error</h2></FONT></PRE></TD></TR></TBODY></TABLE></CCID_NOBR
/></CENTER><FONT face=Arial>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 9pt; WORD-BREAK: break-all; LINE-HEIGHT: 150%; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left><BR>有了web.xml,logon.jsp和error.jsp这三个文件后,就可以看看有什么效果了。</P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 9pt; WORD-BREAK: break-all; LINE-HEIGHT: 150%; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=left>可以在tomcat下新建一个application:</P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 9pt; WORD-BREAK: break-all; LINE-HEIGHT: 150%; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=justify>security<BR>|<BR>-logon.jsp<BR>-error.jsp<BR>-WEB-INF<BR>
|<BR> -web.xml</P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 9pt; WORD-BREAK: break-all; LINE-HEIGHT: 150%; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=justify>在浏览器上输入地址:<A
href="http://localhost:8080/security">http://localhost:8080/security</A></P>
<P class=MsoNormal
style="MARGIN: 0cm 0cm 9pt; WORD-BREAK: break-all; LINE-HEIGHT: 150%; TEXT-ALIGN: left; mso-pagination: widow-orphan"
align=justify>就可来到登陆界面。输入用户名:tomcat,密码:tomcat即可登陆啦。<BR></P></FONT>
<P></P>
<P class=diaryFoot>- 作者: <A
onclick="window.open('http://publishblog.blogdriver.com/blog/postMessage.b?receiver=517836','发送短消息','width=520, height=455')"
href="javascript:void(0);">rosonsandy</A> 2005年09月29日, 星期四 22:35 </P></DIV>
<DIV class=operation><A name=trackback>
<H3>Trackback</H3></A>
<P class=trackback>你可以使用这个链接引用该篇日志
http://publishblog.blogdriver.com/blog/tb.b?diaryID=987063 </P></DIV>
<DIV class=operation><A name=comment>
<H3>回复</H3></A></DIV>
<DIV class=operation>
<TABLE class=comment cellSpacing=0 cellPadding=0 width=700 border=0>
<FORM id=replyForm method=post><INPUT type=hidden value=356891 name=blogID>
<INPUT type=hidden value=987063 name=diaryID> <INPUT type=hidden
value=rosonsandy name=blogDomino>
<SCRIPT>
if(getCookie('userID') == null){
document.write('<tr><td width="70">发布人:</td>');
document.write('<td width="150"> <input name="remark.authorNameFUI" type="text" size="20" class="inputStyle" maxlength="20"></td>');
document.write('<td width="70">邮箱:</td>');
document.write('<td width="435"> <input name="remark.authorEmail" type="text" size="20" class="inputStyle" maxlength="40"></td>');
document.write('</tr><tr><td>主 页:</td>');
document.write('<td colspan="3"> <input name="remark.authorURL" type="text" class="inputStyle" value="HTTP://" size="63" maxlength="40"></td></tr>');
}else{
document.write('<input type="hidden" name="remark.authorNameFUI" value="Blogdriver">');
}
</SCRIPT>
<TBODY>
<TR align=left>
<TD colSpan=4>评论内容:<BR><TEXTAREA class=textStyle id=remark name=remark.remarkFUI rows=8 cols=60> </TEXTAREA>
</TD></TR>
<TR align=left>
<TD colSpan=4> <INPUT onclick=reply() type=button value=提交>
<INPUT type=reset value=重置> </TD></TR></FORM></TBODY></TABLE></DIV></DIV>
<SCRIPT src="" type=text/javascript></SCRIPT>
<DIV id=footer><A href="http://www.blogdriver.com/jsp/reg/register.jsp"><IMG
class=logo src="[转贴加修改]J2EE安全策略:为tomcat页面设置访问权限--rosonsandy.files/logo3.gif"
border=0></A><A href="http://www.blogdriver.com/">2003-2004 BLOGDRIVER.COM All
rights reserved</A></DIV></BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -