writedby 张艳涛

第19章讲的是管理程序,当一个tomcat启动的时候,能通过远程浏览器能访问tomcat,启动web应用,关闭web应用,查看web应用

怎么实现的呢?

在webapp 文件夹下有一个manager.xml,这是一个web应用 对应的应用路径在  :: tomcat安装目录\server\webapps\manager

那么当启动tomcat 的时候,输入路径 http://localhost:8080/manager/list   会让你填写账号密码,这个值在../conf/comcat-user.xml文件下

  1. <?xml version='1.0' encoding='utf-8'?>
  2. <tomcat-users>
  3. <role rolename="tomcat"/>
  4. <role rolename="role1"/>
  5. <role rolename="manager"/>
  6. <role rolename="admin"/>
  7. <user username="tomcat" password="tomcat" roles="tomcat"/>
  8. <user username="role1" password="tomcat" roles="role1"/>
  9. <user username="both" password="tomcat" roles="tomcat,role1"/>
  10. <user username="admin" password="password" roles="admin,manager"/>
  11. </tomcat-users>

上文是tomcat那本书上的,可以看到manger 有对应的密码账号,比如 填入最后一个就行

但是直接下载的tomcat就没有配置

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

会提示正

那么输入 http://localhost:8080/manager/list 会出现

  1. OK - Listed applications for virtual host localhost
  2. /:running:0:D:\wksp_study\designbook\webapps\ROOT
  3. /tomcat-docs:running:0:D:\wksp_study\designbook\webapps\tomcat-docs
  4. /webdav:running:0:D:\wksp_study\designbook\webapps\webdav
  5. /admin:running:0:../server/webapps/admin
  6. /app1:running:0:D:\wksp_study\designbook\webapps\app1
  7. /manager:running:0:../server/webapps/manager

说明一个应用的状态,默认都启动了那么可以关闭一个应用

输入

http://localhost:8080/manager/stop?path=/admin

会有回复

  1. OK - Stopped application at context path /admin

启动一个应用

http://localhost:8080/manager/start?path=/admin

回复

  1. OK - Started application at context path /admin  

顺便说下BASIC认证,我们

一. BASIC认证概述

在HTTP协议进行通信的过程中,HTTP协议定义了基本认证过程以允许HTTP服务器对WEB浏览器进行用户身份证的方法,当一个客户端向HTTP服务 器进行数据请求时,如果客户端未被认证,则HTTP服务器将通过基本认证过程对客户端的用户名及密码进行验证,以决定用户是否合法。客户端在接收到HTTP服务器的身份认证要求后,会提示用户输入用户名及密码,然后将用户名及密码以BASE64加密,加密后的密文将附加于请求信息中, 如当用户名为anjuta,密码为:123456时,客户端将用户名和密码用“:”合并,并将合并后的字符串用BASE64加密为密文,并于每次请求数据 时,将密文附加于请求头(Request Header)中。HTTP服务器在每次收到请求包后,根据协议取得客户端附加的用户信息(BASE64加密的用户名和密码),解开请求包,对用户名及密码进行验证,如果用 户名及密码正确,则根据客户端请求,返回客户端所需要的数据;否则,返回错误代码或重新要求客户端提供用户名及密码。

二. BASIC认证的过程

1. 客户端向服务器请求数据,请求的内容可能是一个网页或者是一个其它的MIME类型,此时,假设客户端尚未被验证,则客户端提供如下请求至服务器:

Get /index.html HTTP/1.0
Host:www.google.com

2. 服务器向客户端发送验证请求代码401,服务器返回的数据大抵如下:

HTTP/1.0 401 Unauthorised
Server: SokEvo/1.0
WWW-Authenticate: Basic realm=“google.com
Content-Type: text/html
Content-Length: xxx

3. 当符合http1.0或1.1规范的客户端(如IE,FIREFOX)收到401返回值时,将自动弹出一个登录窗口,要求用户输入用户名和密码。

4. 用户输入用户名和密码后,将用户名及密码以BASE64加密方式加密,并将密文放入前一条请求信息中,则客户端发送的第一条请求信息则变成如下内容:

Get /index.html HTTP/1.0
Host:www.google.com
Authorization: Basic xxxxxxxxxxxxxxxxxxxxxxxxxxxx

注:xxxx…表示加密后的用户名及密码。

5. 服务器收到上述请求信息后,将Authorization字段后的用户信息取出、解密,将解密后的用户名及密码与用户数据库进行比较验证,如用户名及密码正确,服务器则根据请求,将所请求资源发送给客户端:

三. BASIC认证的缺点

HTTP基本认证的目标是提供简单的用户验证功能,其认证过程简单明了,适合于对安全性要求不高的系统或设备中,如大家所用路由器的配置页面的认证,几乎 都采取了这种方式。其缺点是没有灵活可靠的认证策略,如无法提供域(domain或realm)认证功能,另外,BASE64的加密强度非常低,

tomcat是怎么实现basic认证的呢?

  1. <!-- Define a Security Constraint on this Application -->
  2. <security-constraint>
  3. <web-resource-collection>
  4. <web-resource-name>HTMLManger and Manager command</web-resource-name>
  5. <url-pattern>/html/*</url-pattern>
  6. <url-pattern>/list</url-pattern>
  7. <url-pattern>/sessions</url-pattern>
  8. <url-pattern>/start</url-pattern>
  9. <url-pattern>/stop</url-pattern>
  10. <url-pattern>/install</url-pattern>
  11. <url-pattern>/remove</url-pattern>
  12. <url-pattern>/deploy</url-pattern>
  13. <url-pattern>/undeploy</url-pattern>
  14. <url-pattern>/reload</url-pattern>
  15. <url-pattern>/serverinfo</url-pattern>
  16. <url-pattern>/roles</url-pattern>
  17. <url-pattern>/resources</url-pattern>
  18. </web-resource-collection>
  19. <auth-constraint>
  20. <!-- NOTE: This role is not present in the default users file -->
  21. <role-name>manager</role-name>
  22. </auth-constraint>
  23. </security-constraint>
  24.  
  25. <!-- Define the Login Configuration for this Application -->
  26. <login-config>
  27. <auth-method>BASIC</auth-method>
  28. <realm-name>Tomcat Manager Application</realm-name>
  29. </login-config>
  30.  
  31. <!-- Security roles referenced by this web application -->
  32. <security-role>
  33. <description>
  34. The role that is required to log in to the Manager Application
  35. </description>
  36. <role-name>manager</role-name>
  37. </security-role>

这个basic认证发类为AuthenticatorBase  extends ValveBase

其中实现的invoke方法为

  1. public void invoke(Request request, Response response,
  2. ValveContext context)
  3. throws IOException, ServletException {
  4.  
  5. // If this is not an HTTP request, do nothing
  6. if (!(request instanceof HttpRequest) ||
  7. !(response instanceof HttpResponse)) {
  8. context.invokeNext(request, response);
  9. return;
  10. }
  11. if (!(request.getRequest() instanceof HttpServletRequest) ||
  12. !(response.getResponse() instanceof HttpServletResponse)) {
  13. context.invokeNext(request, response);
  14. return;
  15. }
  16. HttpRequest hrequest = (HttpRequest) request;
  17. HttpResponse hresponse = (HttpResponse) response;
  18. if (debug >= 1)
  19. log("Security checking request " +
  20. ((HttpServletRequest) request.getRequest()).getMethod() + " " +
  21. ((HttpServletRequest) request.getRequest()).getRequestURI());
  22. LoginConfig config = this.context.getLoginConfig();
  23.  
  24. // Have we got a cached authenticated Principal to record?
  25. if (cache) {
  26. Principal principal =
  27. ((HttpServletRequest) request.getRequest()).getUserPrincipal();
  28. if (principal == null) {
  29. Session session = getSession(hrequest);
  30. if (session != null) {
  31. principal = session.getPrincipal();
  32. if (principal != null) {
  33. if (debug >= 1)
  34. log("We have cached auth type " +
  35. session.getAuthType() +
  36. " for principal " +
  37. session.getPrincipal());
  38. hrequest.setAuthType(session.getAuthType());
  39. hrequest.setUserPrincipal(principal);
  40. }
  41. }
  42. }
  43. }
  44.  
  45. // Special handling for form-based logins to deal with the case
  46. // where the login form (and therefore the "j_security_check" URI
  47. // to which it submits) might be outside the secured area
  48. String contextPath = this.context.getPath();
  49. String requestURI = hrequest.getDecodedRequestURI();
  50. if (requestURI.startsWith(contextPath) &&
  51. requestURI.endsWith(Constants.FORM_ACTION)) {
  52. if (!authenticate(hrequest, hresponse, config)) {
  53. if (debug >= 1)
  54. log(" Failed authenticate() test");
  55. return;
  56. }
  57. }
  58.  
  59. // Is this request URI subject to a security constraint?
  60. SecurityConstraint constraint = findConstraint(hrequest);
  61. if ((constraint == null) /* &&
  62. (!Constants.FORM_METHOD.equals(config.getAuthMethod())) */ ) {
  63. if (debug >= 1)
  64. log(" Not subject to any constraint");
  65. context.invokeNext(request, response);
  66. return;
  67. }
  68. if ((debug >= 1) && (constraint != null))
  69. log(" Subject to constraint " + constraint);
  70.  
  71. // Make sure that constrained resources are not cached by web proxies
  72. // or browsers as caching can provide a security hole
  73. if (!(((HttpServletRequest) hrequest.getRequest()).isSecure())) {
  74. HttpServletResponse sresponse =
  75. (HttpServletResponse) response.getResponse();
  76. sresponse.setHeader("Pragma", "No-cache");
  77. sresponse.setHeader("Cache-Control", "no-cache");
  78. sresponse.setDateHeader("Expires", 1);
  79. }
  80.  
  81. // Enforce any user data constraint for this security constraint
  82. if (debug >= 1)
  83. log(" Calling checkUserData()");
  84. if (!checkUserData(hrequest, hresponse, constraint)) {
  85. if (debug >= 1)
  86. log(" Failed checkUserData() test");
  87. // ASSERT: Authenticator already set the appropriate
  88. // HTTP status code, so we do not have to do anything special
  89. return;
  90. }
  91.  
  92. // Authenticate based upon the specified login configuration
  93. if (constraint.getAuthConstraint()) {
  94. if (debug >= 1)
  95. log(" Calling authenticate()");
  96. if (!authenticate(hrequest, hresponse, config)) {
  97. if (debug >= 1)
  98. log(" Failed authenticate() test");
  99. // ASSERT: Authenticator already set the appropriate
  100. // HTTP status code, so we do not have to do anything special
  101. return;
  102. }
  103. }
  104.  
  105. // Perform access control based on the specified role(s)
  106. if (constraint.getAuthConstraint()) {
  107. if (debug >= 1)
  108. log(" Calling accessControl()");
  109. if (!accessControl(hrequest, hresponse, constraint)) {
  110. if (debug >= 1)
  111. log(" Failed accessControl() test");
  112. // ASSERT: AccessControl method has already set the appropriate
  113. // HTTP status code, so we do not have to do anything special
  114. return;
  115. }
  116. }
  117.  
  118. // Any and all specified constraints have been satisfied
  119. if (debug >= 1)
  120. log(" Successfully passed all security constraints");
  121. context.invokeNext(request, response);
  122.  
  123. }

注意这个阀门的特殊性,别的阀门都是在调用basic 阀门之后才调用 附加阀门,而认证阀门如果不满足条件,直接返回不会调用后续的阀门,也不会调用的基本阀门来调用servlet方法

深入刨析tomcat 之---第14篇 对应19章,使用manager管理 web应用的更多相关文章

  1. 深入刨析tomcat 之---第15篇 对应20章, myAdmin案例代码

    writedby 张艳涛 有没有和我一样做到第20章的?今天基本结束了本书的阅读.最后一个案例没有demo,那我写了一回,如果有需要的可以在本地试试 路径 D:\wksp_study\designbo ...

  2. 深入刨析tomcat 之---第8篇 how tomcat works 第11章 11.9应用程序,自定义Filter,及注册

    writed by 张艳涛, 标签:全网独一份, 自定义一个Filter 起因:在学习深入刨析tomcat的学习中,第11章,说了调用过滤链的原理,但没有给出实例来,自己经过分析,给出来了一个Filt ...

  3. 深入刨析tomcat 之---第2篇,解决第3章bug 页面不显示内容http://localhost:8080/servlet/ModernServlet?userName=zhangyantao&password=1234

    writedby 张艳涛7月2日, 在学习第4张的过程中,发现了前一篇文章写的是关于1,2张的bug不用设置response响应头,需要在servlet的service()方法里面写是错误想 serv ...

  4. 深入刨析tomcat 之---第11篇 how tomcat works( 第15章 ) 如何解析web.xml 文件

    writedby 张艳涛 记得当年是学习jsp的时候,写过web.xml中的标签.在之后的springmvc中也是有关于配置mvc 过滤器 和dispatchServlet的标签,之前是看不懂呢!看到 ...

  5. 深入刨析tomcat 之---第12篇 how tomcat works( 第17章 ) 解析catalina.bat 梳理启动流程

    我们如何启动tomcat呢? 答案是双击startup.bat文件,这个文件在bin目录下 @echo off    不显示批处理命令 rem Licensed to the Apache Softw ...

  6. 深入刨析tomcat 之---第23篇 聊一下web容器的filter配置和defaultservet

    writedby 张艳涛,在一个webapp应用程序内如何配置filter? <?xml version="1.0" encoding="ISO-8859-1&qu ...

  7. 深入刨析tomcat 之---第21篇 tomcat 对jsp页面支持的实现原理

    writedby 张艳涛 web技术,以前的动态网页技术多是jsp页面,比如点击一个菜单目录,直接访问了一个LRJSDetailInput.jsp页面,这个页面 有<html><bo ...

  8. 深入刨析tomcat 之---第13篇 tomcat的三种部署方法

    writedby 张艳涛 一般我们都知道将web 应用打成war包,放到tomcat的webapp目录下,就是部署了,这是部署方法1 第2种部署方法我们也知道,就是讲web应用的文件夹拷贝到webap ...

  9. 深入刨析tomcat 之---第10篇 how tomcat works 第13章,Response 发送错误信息 sendError

    writedby 张艳涛 在浏览器中发送一个错误应用url 那么tomcat是如何发送错误的呢? 基本上是发送http 的response协议,分为两部分一部分是response设置头信息, 那么先分 ...

随机推荐

  1. css--常见左右盒子高度自适应布局

    前言 前端开发工程师最基础的技能要求是根据 ui 设计稿还原网页,这就缺少不了必要的网页布局,首先看下最近小伙伴问我的一个问题,他说一个网页有左右两个部分,左右两个部分的高度都不固定,要使得右部分的宽 ...

  2. 尼恩 Java高并发三部曲 [官方]

    高并发 发烧友社群:疯狂创客圈(总入口) 奉上以下珍贵的学习资源: 疯狂创客圈 经典图书 : 极致经典 + 社群大片好评 < Java 高并发 三部曲 > 面试必备 + 大厂必备 + 涨薪 ...

  3. Hibernate 这么硬核,为什么用的人少?

    关于SQL和ORM的争论,永远都不会终止,我也一直在思考这个问题.最近温习了一遍SSH框架,发了动弹,和广大猿友进行了深刻的探讨,被喷的五体投地,感慨万千,于是就有了今天这篇文章. 声明:本文只是小编 ...

  4. 6.6考试总结(NOIP模拟4)

    前言 考试这种东西暴力拉满就对了QAQ T1 随 题解 解题思路 DP+矩阵乘(快速幂)+数论 又是一道与期望无关的期望题,显然答案是 总情况/情况数(\(n^m\)). 接下来的问题就是对于总情况的 ...

  5. 音视频点播服务基础系列(Fmpeg常用命令)

    前言 公司业务中有一些场景需要用到服务端音视频剪辑技术,最开始为了快速上线使用的是某公有云的商用解决方案,但由于费用太高所以我们团队经过一个星期的冲刺,给出了一个FFmpeg+Serverless的解 ...

  6. Linux中mail的用法

    简介:mail命令是命令行的电子邮件发送和接收工具.操作的界面不像elm或pine那么容易使用,但功能非常完整Red Hat上sendmail服务一般是自动启动的.可以通过下面的命令查看sendmai ...

  7. 第11章:Pod数据持久化

    参考文档:https://kubernetes.io/docs/concepts/storage/volumes/ Kubernetes中的Volume提供了在容器中挂载外部存储的能力 Pod需要设置 ...

  8. mysql_my.cnf文件详解

    以下是 my.cnf 配置文件参数解释:#*** client options 相关选项 ***##以下选项会被MySQL客户端应用读取.注意只有MySQL附带的客户端应用程序保证可以读取这段内容.如 ...

  9. 11、nginx+tomcat+redis_session共享

    11.1.前言: 1.多个tomcat要一起协同工作可以考虑的方案如下: (1)使用tomcat自带的cluster方式,多个tomcat间自动实时复制session信息,配置起来很简单.但这个方案的 ...

  10. 关于tinymce的一些记事

    之前能看的懂一部分英文,但是总是没有全局观,以至于我之前使用tinymce一直都有一些疑问:那就是为什么我在tinymce初始化中添加了比如字体,字体大小等设置按钮,但是为什么在前 台没有办法现实出来 ...