在/conf/下,有个文件tomcat-users.xml,这是存放tomcat用户的名称、密码以及角色的,如果要管理权限,只要将你的role="manager"即可,如果想要多个角色,只要用逗号给开就行了
解决方案 »
- 用服务启动TOMCAT
- 不能够解析html代码,直接输出代码
- jspx数据绑定如何实现
- 关于hibernate的多对多映射问题
- jsp插入,读取sql server数据库图片
- installanyshere怎么打包WEB程序?
- 接下来该怎么学习?
- 在web服务一启动时我希望一个web application 一些要初始化的工作就已经准备好了 急急急急急急!!!!!!
- 在哪里可以下载到安装JSP需要的软件?
- "extends"的应用问题。报错说:*.class找不到
- include的页面不存在不报错<jsp:include page="adf.jsp" flush="true"/>
- 关于图片链接定位的问题。紧急求助。
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会让你输入用户名和密码。
例如你要控制对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中测试通过