当访问服务器中受保护的资源时,容器管理的验证方法可以控制确认用户身份的方式。Tomcat支持四种容器管理的安全防护,它们是:

  • BASIC (基本验证):通过HTTP验证,需要提供base64编码文本的用户口令
  • DIGEST (摘要验证):通过HTTP验证,需要提供摘要编码字符串的用户口令
  • FORM (表单验证):在网页的表单上要求提供密码
  • CLIENT-CERT (客户端证书验证):以客户端证书来确认用户的身份

基本验证

当web.xml文件中的auth-method元素设置为BASIC时,表明应用使用的是基本验证,每次浏览器请求受保护的Web应用资源时,Tomcat都会使用HTTP基本验证向浏览器索取用户名和密码(以页面弹窗的方式)。使用这种验证方法,所有的密码都会以base64编码的文本在网络上传输。

先看下项目结构(我用Maven管理的依赖):

其中,protect/protect.jsp是被保护的,需要授权访问。

说明:本文提到的tomcat-users.xml,server.xml等文件,如果是在 Eclipse 中启动tomcat,则这些文件在Eclipse中的Servers工程下对应的tomcat下,如图:

而本文提到的web.xml是指项目自己的web.xml,而非Servers项目下Tomcat中的web.xml。

web.xml

<security-constraint>
<web-resource-collection>
<http-method>GET</http-method>
<web-resource-name>tomcat protect page</web-resource-name>
<!-- /protect目录下的所有资源是受保护的 -->
<url-pattern>/protect/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<!-- 这里的member要与tomcat-user.xml中配置的role一致 -->
<role-name>member</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<!-- 验证方式,可选的值为: "BASIC", "DIGEST", "FORM", "CLIENT-CERT" -->
<auth-method>BASIC</auth-method>
<!-- 使用的Realm名字,注意这里不能有空格 -->
<realm-name>MyConstraints</realm-name>
</login-config>

tomcat-user.xml(注意如果是在Eclipse中启动tomcat,这个tomcat-user.xml在Eclipse中的Servers工程下)

<role rolename="member"/>
<!-- member角色下有一个叫alvis的用户,密码为pwd -->
<user username="alvis" password="pwd" roles="member"/>

重启tomcat后,访问protect目录下的资源,情况是这样的:

输入账户alvis,密码pwd后,访问成功(当然,非protect目录下的资源是可以直接访问的):

摘要验证

当web.xml文件中的auth-method元素设置为DIGEST时,表明应用使用的是摘要验证。还是上面的例子,看配置:

web.xml和基本验证一样,只是auth-method修改为DIGEST,此处不赘述。

server.xml中的UserDatabaseRealm(如果tomcat使用的是其他Realm,也一样的)里增加digest属性:

接下来,要生成tomcat可识别的MD5密码。方式有两种,正如官网描述:

To calculate the digested value of a cleartext password, two convenience techniques are supported:

  • If you are writing an application that needs to calculate digested passwords dynamically, call the static  Digest() method of the  org.apache.catalina.realm.RealmBase  class, passing the cleartext password and the digest algorithm name as arguments. This method will return the digested password.
  • If you want to execute a command line utility to calculate the digested password, simply execute

    <code>CATALINA_HOME/bin/digest.[bat|sh] -a {algorithm} {cleartext-password} </code>

    and the digested version of this cleartext password will be returned to standard output.

方式一:用代码来生成:

import org.apache.catalina.realm.RealmBase;

public class T {
public static void main(String[] args) {
//参数1:要加密的字符串;参数2:加密算法;参数3:字符串的编码
String base = RealmBase.Digest("alvis:MyConstraints:pwd", "MD5", null);
System.out.println(base);
}
}

由于RealmBase类在catalina.jar包中,如果项目中没有这个类,可在项目上右键–>Java Build Path–> Libraries–>Add Library–>选择Server Runtime–>选择Apache Tomcat V8.0(其实7.0也行),如图:

方式二:用脚本来生成:

在tomcat/bin目录下有个digest.sh(Linux系统)或digest.bat(Windows系统)脚本,运行这个脚本,传入摘要算法和参数即可,这里我在Windows系统上运行,如图:

这里的-a指定摘要算法为MD5,要 特别注意这里的参数是:{用户名}:{Realm名}:{密码明文} 。用户名就是tomcat-users.xml中配置的<user>名字(这里为alvis),Realm名是在web.xml中配置的<realm-name>(这里为MyConstraints),密码明文即该用户用于登录的密码(我这里设为pwd)。

只有这样的参数加密后的密码,在tomcat-users.xml中配置才有效,否则是登录不了的。由于我是参考《Tomcat权威指南(第二版)》 的步骤做的,之前试了很久都不知道为什么登录不了,结果在 官网 找到答案,是这么描述的:

If using digested passwords with DIGEST authentication, the cleartext used to generate the digest is different and the digest must use the MD5 algorithm. In the examples above {cleartext-password}must be replaced with {username}:{realm}:{cleartext-password} . For example, in a development environment this might take the form  testUser:Authentication required:testPassword . The value for  {realm} is taken from the  <realm-name> element of the web application’s  <login-config> . If not specified in web.xml, the default value of  Authentication required is used.

大意是说,如果使用DIGEST方式验证,用于生成摘要的明文必须被替换为这种格式。实践出真知,所以还是不能完全看书啊,动手实践才是实在的。

然后就是在tomcat-users.xml中配置生成的密码(通过下方的截图,可以比较password跟上方digest.bat脚本生成的密码是否一致):

之后重启tomcat,效果自然是跟使用基本验证的效果一样了。

表单验证

当web.xml文件中的auth-method元素设置为FORM时,表明应用使用的是表单验证。当用户请求Web应用程序受保护的资源时,表单验证会跳转至配置的登录页面。当登录失败时,还需要一个验证失败的页面,还是上面的例子,看配置:

web.xml

<security-constraint>
<web-resource-collection>
<http-method>GET</http-method>
<web-resource-name>tomcat member part</web-resource-name>
<url-pattern>/protect/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>member</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>FORM</auth-method>
<realm-name>MyConstraints</realm-name>
<form-login-config>
<form-login-page>/form/login.html</form-login-page>
<form-error-page>/form/error.html</form-error-page>
</form-login-config>
</login-config>

这里的form/login.html是用于登录的页面,而form/error.html则是验证失败后跳转到的页面(这两个页面在上方的工程结构图中已经有了)。

login.html

<html>
<body>
<h2>Login Page.</h2> <form method="post" action="j_security_check" name="loginForm">
<input type="text" name="j_username" /><br>
<input type="password" name="j_password" /><br>
<input type="submit" value="Login" />
</form> </body>
</html>

注意:这里form的action=” j_security_check “,账号的name=” j_username “和密码的name=”j_password “都是不可变的,否则配置的验证规则不起作用。

server.xml中,要 去掉 Realm中添加的“ digest=MD5 ”这个属性:

tomcat-users.xml中 使用明文 保存密码:

效果(仅在访问protect目录下的资源时才出现Login Page):

输入错误的账号和密码,跳转至form/error.html页面:

输入正确的账号和密码,跳转至受保护的页面:

客户端证书验证

待续

Demo下载:

链接: http://pan.baidu.com/s/1gfnqVdT 密码: pubw

参考页面:

https://tomcat.apache.org/tomcat-7.0-doc/realm-howto.html

Tomcat学习总结(5)——Tomcat容器管理安全的几种验证方式的更多相关文章

  1. Java学习-014-文本文件写入实例源代码(两种写入方式)

    此文源码主要为应用 Java 读取文本文件内容实例的源代码.若有不足之处,敬请大神指正,不胜感激! 第一种:文本文件写入,若文件存在则删除原文件,并重新创建文件.源代码如下所示: /** * @fun ...

  2. Docker学习第二天(Docker容器管理)

    简介 emmmm Docker 容器管理 推荐文章:容器技术概述 run里面的子选项 1.使用run命令创建容器 docker container run -it ubuntu /bin/bash / ...

  3. DOCKER学习_002:Docker的容器管理

    一 Docker的基本信息 前面已经安装了Docker,现在看一下已安装Docker的安装环境以及其他信息 1.1 系统环境 [root@docker-server3 ~]# uname -r -.e ...

  4. Spring管理Bean的三种创建方式

    1.使用类构造器实例化  (90%以上都是使用这种) <bean id=“orderService" class="cn.itcast.OrderServiceBean&qu ...

  5. Javaweb学习笔记——(六)——————xml中jaxp两种解析方式和dom4j运用

    1.xml的scheam约束 dtd的语法:<!ElEMENT 元素名称 约束> **schema符合xml的语法,xml语句 **一个xml中可以有多个schema,多个schema使用 ...

  6. STM32 HAL库学习系列第3篇 常使用的几种延时方式

    1   自带的hal_delay 函数    毫秒级延迟 void HAL_Delay(__IO uint32_t Delay) { uint32_t tickstart = HAL_GetTick( ...

  7. Storm 学习之路(六)—— Storm项目三种打包方式对比分析

    一.简介 在将Storm Topology提交到服务器集群运行时,需要先将项目进行打包.本文主要对比分析各种打包方式,并将打包过程中需要注意的事项进行说明.主要打包方式有以下三种: 第一种:不加任何插 ...

  8. Jmeter学习笔记(十五)——常用的4种参数化方式

    一.Jmeter参数化概念 当使用JMeter进行测试时,测试数据的准备是一项重要的工作.若要求每次迭代的数据不一样时,则需进行参数化,然后从参数化的文件中来读取测试数据. 参数化是自动化测试脚本的一 ...

  9. Spring IoC容器管理Action

    Spring IoC容器管理Action有两种方式:DelegatingRequestProcessor.DelegatingActionProxy 不管采用哪一种方式,都需要随应用启动时创建Appl ...

随机推荐

  1. Codeforces Round #540 (Div. 3)--1118D1 - Coffee and Coursework (Easy version)

    https://codeforces.com/contest/1118/problem/D1 能做完的天数最大不超过n,因为假如每天一杯咖啡,每杯咖啡容量大于1 首先对容量进行从大到小的排序, sor ...

  2. java基础-day7

    第07天 面向对象基础 今日内容介绍 u 面向对象概述 u 面向对象特性之封装 u 面向对象之构造方法 u 类名作为形参和返回值案例 第1章   面向对象概述 1.1      面向对象思想 1.1. ...

  3. OpenGL中的渐变颜色绘图(应力可视化)

    #include <GL/glut.h> #include <iostream> #include <cmath> using namespace std; ; ; ...

  4. 中大 9095. Islands

    9095. Islands 限制条件 时间限制: 2 秒, 内存限制: 256 兆 题目描述 Whenever it rains, Farmer John's field always ends up ...

  5. Linux (rz、sz命令行)与本地电脑 命令行上传、下载文件

    Linux 与本地电脑直接交互, 命令行上传.下载文件. 一.lrzsz命令行安装: 1.rpm安装:(链接: http://pan.baidu.com/s/1cBuTm2 密码: vijf) rpm ...

  6. 4.动态HTML处理和机器图像识别

    Selenium Selenium是一个Web的自动化测试工具,最初是为网站自动化测试而开发的,类型像我们玩游戏用的按键精灵,可以按指定的命令自动操作,不同是Selenium 可以直接运行在浏览器上, ...

  7. Linux 下建立 SSH 隧道做 Socket 代理

    背景 需要解决本地访问内部集群中各台机器上的内部web服务,但是内部集群不能直接访问,只能通过edge node节点跳转. 前提:edge node可以通过ssh方式访问,在edge node上可以访 ...

  8. FFmpeg4.0笔记:file2rtmp

    Github: https://github.com/gongluck/FFmpeg4.0-study.git #include <iostream> using namespace st ...

  9. 扩展 StackExchange.Redis 支持实体

    一.StackExchange.Redis StackExchange.Redis是由Stack Overflow开发的C#语言Redis客户端,使用广泛,本文针对 StackExchange.Red ...

  10. 使用Charles对Android App的https请求进行抓包

    本文背景 公司新项目要求抓取目前市面上一些热门App的数据,经过研究发现很多App的网络请求都使用https进行数据传输,这样问题就来了,http使用明文传输所有请求都能拦截到,而https请求无法拦 ...