在/conf/下,有个文件tomcat-users.xml,这是存放tomcat用户的名称、密码以及角色的,如果要管理权限,只要将你的role="manager"即可,如果想要多个角色,只要用逗号给开就行了

解决方案 »

  1.   

    给你一个例子。http基本验证。(参考参考)
    package com.wrox.servlets;(放置在tocmat\webapps\yourjsp\WEB-INF\classes\com\wrox\servlets下)import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.util.*;
    import java.security.*;public class ProtectedServlet extends HttpServlet {
      public void init(ServletConfig cfg) throws ServletException {
        super.init(cfg);
      }   public void doGet(HttpServletRequest req, HttpServletResponse res) 
              throws IOException, ServletException {
        res.setContentType("text/plain");
        PrintWriter out = res.getWriter();
        String authType = req.getAuthType();
        out.println("You are authorized to view this page");
        out.println("You were authenticated using: " + authType 
                    + " method of authentication");
        Principal princ = req.getUserPrincipal();
        out.println("The user is: " + princ.getName());
      } 
    }web.xml文件如下:(tomcat\webapps\yourjsp\WEB-INF\web.xml)
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd"><web-app>
      <servlet>
        <servlet-name>protected</servlet-name>
        <servlet-class>com.wrox.servlets.ProtectedServlet</servlet-class>
      </servlet>  <servlet-mapping>
        <servlet-name>protected</servlet-name>
        <url-pattern>/protected</url-pattern>
      </servlet-mapping>  <security-constraint>
        <web-resource-collection>
          <web-resource-name>Protected Area</web-resource-name>
          <url-pattern>/protected</url-pattern>
        </web-resource-collection>
        <auth-constraint>
          <role-name>begjsp</role-name>
        </auth-constraint>
      </security-constraint>  <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>Wrox Area</realm-name>
      </login-config>
    </web-app>最后在tomcat-users.xml文件增加实体,
    <user name="newuser" password="tomcat" roles="begjsp" />地址栏输入:http://localhost:8080/youjsp/protected会让你输入用户名和密码。
      

  2.   

    你指的是用BASIC验证法,修改tomcat-users.xml文件即可
      

  3.   

    精华区的帖子,连接如下:
    例如你要控制对ROOT目录下文件的访问:
    首先更改$TOMCAT_HOME/ROOT/WEB-INF/web.xml
    1。在<web-app>和</web-app>之间加入
      <security-constraint>
        <web-resource-collection>
          <web-resource-name>Entire Application</web-resource-name>
          <url-pattern>/*</url-pattern>
        </web-resource-collection>
        <auth-constraint>
           <!-- NOTE:  This role is not present in the default users file -->
           <role-name>user</role-name>
        </auth-constraint>
      </security-constraint>  <!-- Define the Login Configuration for this Application -->
      <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>TEST ACCESS CONTROL</realm-name>
      </login-config>2。然后在$TOMCAT_HOME/conf/tomcat-users.xml中加入
       <user name="user" password="password" roles="user"/> 
       roles的名字和web.xml中的相对应 在TOMCAT4.03中测试通过