---
title: "通过tomcat本身的用户管理进行页面权限设置"
author: "zybing"
date: "2010-11-18T11:30:40Z"
tags: ["Security", "Tomcat", "Web", "配置管理", "项目管理"]
categories: ["编程语言"]
slug: "tomcat"
draft: false
lastmod: "2010-11-18T11:30:40Z"
id: 814836
url: "https://zybing.iteye.com/blog/814836"
---

来源： [http://www.qqread.com/java/2008/09/y428747.html](http://www.qqread.com/java/2008/09/y428747.html)

 

 

# 为tomcat页面设置访问权限

[巧巧读书](http://www.qqread.com/) 2008-09-04 [中国](http://www.qqread.com/tksys/pclick.php?kwcode=2019&loc=http%3A%2F%2Fwww.qqread.com%2Fjava%2F2008%2F09%2Fy428747.html&ref=http%3A%2F%2Fwww.google.com.hk%2Fsearch%3Fsource%3Dig%26hl%3Dzh-CN%26rlz%3D1G1GGLQ_ZH-CNCN304%26q%3Dtomcat%20form%25E7%2599%25BB%25E9%2599%2586%26btnG%3DGoogle%20%25E6%2590%259C%25E7%25B4%25A2%26aq%3D%26oq%3D)IT实验室 佚名 [技术论坛](http://group.qqread.com/)

[大](http://www.qqread.com/java/2008/09/y428747.html) [中](http://www.qqread.com/java/2008/09/y428747.html) [小](http://www.qqread.com/java/2008/09/y428747.html)

    在[web应用](http://www.qqread.com/tag/2920/index.html)中，对页面的[访问控制](http://www.qqread.com/z/network/acl/index.html)通常通过程序来控制，流程为：登录 -&gt; 设置session -&gt; 访问受限页面时检查session是否存在，如果不存在，禁止访问

    对于较小型的web应用，可以通过tomcat内置的访问控制机制来实现[权限控制](http://www.qqread.com/tag/3597/index.html)。采用这种机制的好处是，程序中无需进行权限控制，完全通过对tomcat的[配置](http://www.qqread.com/z/sys/safe-seting/index.html)即可完成访问控制。

    为了在tomcat页面设置访问权限控制，在项目的WEB-INFO/web.xml文件中，进行如下设置：

```
<!--servlet等其他配置-->

<security-constraint>
  <web-resource-collection>
      <display-name>Example Security Constraint</display-name>
      <web-resource-name>My Test</web-resource-name>
   
      <url-pattern>/ddly/admin/*</url-pattern>
  </web-resource-collection>
  
<auth-constraint>
    <role-name>role1</role-name>
    <role-name>tomcat</role-name>
</auth-constraint>

</security-constraint>

<login-config>
    <auth-method>BASIC</auth-method>
    <realm-name>My Test</realm-name>
</login-config>

</web-app>
```

 

 

    其中，&lt;url-pattern&gt;中指定受限的url，可以使用通配符\*，通常对整个目录进行访问权限控制。

    &lt;auth-constraint&gt;中指定哪些角色可以访问&lt;url-pattern&gt;指定的url，在&lt;role-name&gt;中可以设置一个或多个角色名。

    使用的角色名来自tomcat的配置文件${CATALINA\_HOME}/conf/tomcat-users.xml.

    &lt;login-config&gt;中设置登录方式，&lt;auth-method&gt;的取值为BASIC或FORM.如果为BASIC，浏览器在需要登录时弹出一个登录窗口。如果为FORM方式，需要指定登录页面和登录失败时的提示信息显示页面。

    使用FORM方式的配置样例如下：

```
<login-config>
   <auth-method>FORM</auth-method>
   <realm-name>Example Form-Based Authentication Area</realm-name>
   <form-login-config>
      <form-login-page>/login.jsp</form-login-page>
      <form-error-page>/error.jsp</form-error-page>
   </form-login-config>
</login-config>
```

 

    其中的&lt;form-login-page&gt;指定登录页面url，&lt;form-error-page&gt;指定登录失败时的提示页面url.登录页面中，form的action，以及其中的用户名和密码两个参数的名称，都应取固定的值。登录的后台处理程序为j\_security\_check；用户名和密码的参数名称分别为：j\_username和j\_password.如下是登录页面（如：login.jsp）的一段示例代码：

```
<form method="POST" action='<%= response.encodeURL("j_security_check") %>' >
  <table border="0" cellspacing="5">
    <tr>
      <th align="right">Username:</th>
      <td align="left"><input type="text" name="j_username"></td>
    </tr>
    <tr>
      <th align="right">Password:</th>
      <td align="left"><input type="password" name="j_password"></td>
    </tr>
    <tr>
      <td align="right"><input type="submit" value="Log In"></td>
      <td align="left"><input type="reset"></td>
    </tr>
  </table>
</form>
```