深入tomcat验证机制

tomcat的j_security_check验证机制 tomcat的j_security_check验证机制 是利用tomcat自身配置的角色和用户名来登录应用系统的 配置如下: tomcat-users.xml <role rolename="tomcat"/> <role rolename="tian"/> <role rolename="manager"/> <user n 阅读详情

背景

<<Head First Servlets & JSP>>安全一章介绍了tomcat内建的验证机制.

 

首先介绍术语realm.

As far as theservlet

spec is concerned, arealm is a place where authentication information is stored. When you’retesting your application in Tomcat, you can use a file called“tomcat-users.xml” (located in

tomcat’sconf/directory, NOT within webapps). That one “tomcat-users.xml” file appliesto ALL

applicationsdeployed under web-apps. It’s commonly known as the memory realm because Tomcat reads this file into memory at startuptime. While it’s great for testing, it’s not recommended for production. Forone thing you can’t modify its contents without restarting Tomcat.

 

The tomcat-users.xml file

用户名,密码以及角色存储于/conf/tomcat-users.xml文件.这里不详细介绍.

<rolerolename="Admin"/>

<rolerolename="Member"/>

<rolerolename="Guest"/>

<userusername="Annie" password="admin"roles="Admin,Member,Guest" />

<userusername="Diane" password="coder"roles="Member,Guest" />

<userusername="Ted" password="newbie" roles="Guest"/>

 

Tomcat厂商指定角色映射

<security-role><role-name>Admin</role-name></security-role>

<security-role><role-name>Member</role-name></security-role>

<security-role><role-name>Guest</role-name></security-role>

 

Enabling authentication

web.xml文件

<login-config>

<auth-method>BASIC</auth-method>

</login-config>

注意:验证方法包括BASIC,DIGEST,CLIENT-CERT以及FORM.表单验证时,action填写j_security_check,用户名name填写j_username,密码name填写j_password,即能自动使用tomcat-users文件中的数据验证.

 

Defining resource/method constraints

<security-constraint>element in the DD.

 

<security-constraint>

<web-resource-collection>

<web-resource-name>newconference</web-resource-name>

<url-pattern>/createConference</url-pattern>

<url-pattern>/createConference.do</url-pattern>

<http-method>GET</http-method>

<http-method>POST</http-method>

</web-resource-collection>

<web-resource-collection>

<web-resource-name>newschedule</web-resource-name>

<url-pattern>/newSchedule</url-pattern>                        

<http-method>GET</http-method>

<http-method>POST</http-method>

</web-resource-collection>

<web-resource-collection>

<web-resource-name>accountmanagement</web-resource-name>

<url-pattern>/editPassword</url-pattern>

<url-pattern>/editPassword.do</url-pattern>

<http-method>GET</http-method>

<http-method>POST</http-method>

</web-resource-collection>

<auth-constraint>

<role-name>Admin</role-name>                        

</auth-constraint>

<!-- 

<user-data-constraint>

<transport-guarantee>CONFIDENTIAL</transport-guarantee>

</user-data-constraint>

-->

</security-constraint>

当你访问安全约束中指定的页面时,就会弹出或跳转至登录页面,用户登录之后并满足指定角色才能访问页面.使用很简单,那么tomcat验证机制是如何实现的呢?tomcat验证机制能满足我们实际所需吗?参考Apache Tomcat文档以及Apache Tomcat源代码.

 

tomcat验证机制如何实现

 

Configuration a Realm

参考Apache Tomcat 官方文档Realm Configuration HOW-TO.

In general, you willbe adding an XML element to your conf/server.xml configuration file, that lookssomething like this:

     

      <!-- ThisRealm uses the UserDatabase configured in the global JNDI

           resources under the key"UserDatabase".  Any edits

           that are performed against thisUserDatabase are immediately

           available for use by the Realm.  -->

      <RealmclassName="org.apache.catalina.realm.UserDatabaseRealm"

            resourceName="UserDatabase"/>

这儿配置UserDatabaseRealm.接下来看看UserDatabase资源配置

 

  <!-- Global JNDI resources

       Documentation at/docs/jndi-resources-howto.html

  -->

  <GlobalNamingResources>

    <!-- Editable user database that canalso be used by

         UserDatabaseRealm to authenticateusers

    -->

    <Resource name="UserDatabase"auth="Container"

             type="org.apache.catalina.UserDatabase"

              description="User databasethat can be updated and saved"

             factory="org.apache.catalina.users.MemoryUserDatabaseFactory"

             pathname="conf/tomcat-users.xml" />

  </GlobalNamingResources>

 

配置中涉及到org.apache.catalina.realm.UserDatabaseRealm,org.apache.catalina.UserDatabase,org.apache.catalina.users.MemoryUserDatabaseFactory这三个类,应该是用于实现tomcat验证机制.

 

查阅JavaDoc相关类.

UserDatabaseRealm有个成员

     /**

     * The<code>UserDatabase</code> we will use to authenticate users

     * and identify associated roles.

     */

UserDatabase database;

 

MemoryUserDatabaseFactory

Createand return a new MemoryUserDatabase instancethat has been configured according to the properties of the specified Reference. If you instance can be created, return nullinstead.

 

MemoryUserDatabase

Concreteimplementation of UserDatabase that loads all defined users, groups, and rolesinto an in-memory data structure, and uses a specified XML file for itspersistent storage. 

 

至于相关的各种验证方法见包org.apache.catalina.authenticator,比如表单验证方法即FormAuthenticator.

 

public booleanauthenticate(Request request,

                                Responseresponse,

                                LoginConfigconfig)

 

代码块

      Realm realm =context.getRealm();

        if (characterEncoding != null) {

           request.setCharacterEncoding(characterEncoding);

        }

        String username =request.getParameter(Constants.FORM_USERNAME);

        String password =request.getParameter(Constants.FORM_PASSWORD);

        if (log.isDebugEnabled())

            log.debug("Authenticatingusername '" + username + "'");

        principal =realm.authenticate(username, password);

        if (principal == null) {

//用户名或密码错误

            forwardToErrorPage(request,response, config);

            return (false);

        }

 

 

RealmBase

 

    /**

     * Return the Principal associated with thespecified username and

     * credentials, if there is one; otherwisereturn <code>null</code>.

     *

     * @param username Username of thePrincipal to look up

     * @param credentials Password or othercredentials to use in

     * authenticating this username

     */

    public Principal authenticate(Stringusername, String credentials) {

 

        String serverCredentials =getPassword(username);

 

        boolean validated ;

        if ( serverCredentials == null ) {

            validated = false;

        } else if(hasMessageDigest()) {

            validated =serverCredentials.equalsIgnoreCase(digest(credentials));

        } else {

            validated =serverCredentials.equals(credentials);

        }

        if(! validated ) {

            if (containerLog.isTraceEnabled()){

               containerLog.trace(sm.getString("realmBase.authenticateFailure",

                                               username));

            }

            return null;

        }

        if (containerLog.isTraceEnabled()) {

           containerLog.trace(sm.getString("realmBase.authenticateSuccess",

                                           username));

        }

 

        return getPrincipal(username);

    }

 

tomcat验证机制能满足实际所需吗

 

使用数据库

以mysql为例.

 

To set up Tomcat touse JDBCRealm, you will need to follow these steps:

 

  1. If you have not yet done so, create tables and columns in your database that conform to the requirements described above.
  1. Configure a database username and password for use by Tomcat, that has at least read only access to the tables described above. (Tomcat will never attempt to write to these tables.)
  1. Place a copy of the JDBC driver you will be using inside the $CATALINA_HOME/lib directory. Note that only JAR files are recognized!
  1. Set up a <Realm> element, as described below, in your $CATALINA_BASE/conf/server.xml file.
  2. Restart Tomcat if it is already running.

 

realm配置使用JDBCRealm.

<Resourcename="jdbc/conference" auth="Container"

type="javax.sql.DataSource" username="root"password="root"

driverClassName="com.mysql.jdbc.Driver"url="jdbc:mysql://127.0.0.1:3306/conference?useUnicode=true&amp;characterEncoding=utf8"

maxTotal="8"maxIdle="4" />

<RealmclassName="org.apache.catalina.realm.JDBCRealm"

driverName="org.gjt.mm.mysql.Driver"

connectionURL="jdbc:mysql://127.0.0.1:3306/conference?user=root&amp;password=root"

userTable="users"userNameCol="user_name" userCredCol="user_pass"

userRoleTable="user_roles"roleNameCol="role_name" />

 

mysql数据库中建表

create table users (

  user_name         varchar(15) not null primary key,

  user_pass         varchar(15) not null

);

 

create tableuser_roles (

  user_name         varchar(15) not null,

  role_name         varchar(15) not null,

  primary key (user_name, role_name)

);

 

将jdbc driver for mysql放入tomcat的lib目录下

比如当前最新的文件mysql-connector-java-5.1.34-bin.jar.

 

 

A typical use of this resource reference might looklike this:

参考Apache tomcat文档JNDI Resources HOW-TO: JDBC DataSources.

Context initCtx =new InitialContext();

Context envCtx =(Context) initCtx.lookup("java:comp/env");

DataSource ds =(DataSource)

  envCtx.lookup("jdbc/conference");

 

Connection conn =ds.getConnection();

... use thisconnection to access the database ...

conn.close();

 

获取验证用户角色以及用户名

HttpServletRequest

  • getUserPrincipal
  • getRemoteUser
  • isUserInRole

 

以获取用户名为例

Principal principal= request.getUserPrincipal();

System.out.println(principal.getName());

38-反射机制(概述+应用:Tomcat)+Class+获取Class对象的三种方式+获取Class中的(构造函数+字段+方法)+AccessibleObject+类的动态加载+用反射验证集合泛型本质 一、反射机制概述 1、Java反射机制是在运行状态中,对于任意一个类(.class文件),都能够知道这个类的所有属性和方法。对于任意一个对象,都能够调用它的任意一个方法和属性。这种动态获取的信息以及动态调用对象的方法的功能称为Java语言的反射机制 即 动态获取类中信息,就是Java反射 2、如果想要对指定名称的字节码文件进行加载、获取其中的内容并调用,就使用反射技术 注:在任何情况下,想... 阅读详情

相关推荐

TensorFlow实战:5步搞定因果推断模型TARNet(附完整代码)

本文提供了一份基于TensorFlow的TARNet因果推断模型实战指南。通过五个清晰步骤,从数据预处理、模型构建到训练评估,详细讲解了如何利用深度模型解决营销评估、疗效分析等场景中的因果效应估计问题,并附有完整代码解决样本不平衡等工程挑战。

python9snake的博客 416

Tomcat中使用的组件-Digester组件

创建日期:2003.5.9基本部分:2003.5.15应用部分:2003.5.18 一、概述Digester组件用于XML文档到JAVA对象的映射处理。 对于构建可配置的软件系统和构建框架式软件平台有特别有用的帮助,这个技能目前国内比较不重视,因此这个组件对于国内有特别重要的意义。Digester的基本处理流程如下:XML文档读取-〉系统处理模块(调用Digest组

qiqijava的专栏 1994

使用Google Earth Engine下载JRC全球水体数据

通过Google Earth Engine平台和提供的源代码,研究人员和水资源管理者可以方便地获取和利用这一重要的水资源监测数据集,从而开展相关的研究和决策支持工作。要使用Google Earth Engine下载JRC全球水体数据,首先需要在Google Earth Engine平台上进行注册和登录。JRC全球水体数据是由欧洲联合研究中心开发的高分辨率全球水体分类数据集,覆盖了整个地球的陆地和水域。下载完成后,你可以使用各种遥感数据处理软件来打开和分析这些数据,以满足不同的研究和应用需求。

LrlDebug的博客 1158

Tomcat组件梳理—Digester的使用

Tomcat组件梳理—Digester的使用 再吐槽一下,本来以为可以不用再开一个篇章来梳理Digester了,但是发现在研究Service的创建时,还是对Digester的很多接口或者机制不熟悉,简直搞不懂。想想还是算了,再回头一下,把这个也给梳理了。所以此文主要做两件事情, 1.梳理Digester的设计思想和常用接口。 2.梳理Digester对server.xml文件的解析。这么看也算是...

weixin_34166472的博客 753

Python 爬虫实战:爬取今日头条文章与阅读量数据

本文详细介绍了基于Python爬取今日头条文章与阅读量数据的完整方案。通过分析今日头条API接口,采用Requests+正则表达式技术实现数据抓取,重点解决加密参数构造、请求头模拟、反爬规避等核心问题。文章包含文章列表获取、详情数据提取、互动数据采集等完整流程,并提供数据存储与基础分析方法。同时强调合规使用爬虫技术,控制爬取频率,避免干扰平台运营。该方案可为内容传播分析、用户兴趣研究提供数据支持,但需注意接口参数更新和反爬机制变化,适时调整代码实现。

2503_91057718的博客 2519

化工设备设计软件:PVELITE二次开发_(12).数据导入导出与兼容性

在数据导入之前,需要定义数据在PVELITE中的结构。可以通过编程接口定义字段名称和数据类型。# 导入必要的库 import pvelite # 创建PVELITE实例 pve = pvelite . PVELITE() # 定义CSV数据结构 csv_structure = {

kkchenjj的博客 1216

【原】HTTP 验证 Tomcat中进行基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)...

HTTP 验证HTTP 协议提供验证机制来保护资源。当一个请求要求取得受保护的资源时,网页服务器回应一个 401 Unauthorized error 错误码。这个回应包含一个指定了验证方法和领域的 WWW-Authenticate 头信息。把这个领域想像成一个存储着用户名和密码的数据库,它将被用来标识受保护资源的有效的用户。比如,你试着去访问某个网站上标识为“Personal Files”的资...

我是程序员,为了最后的阵地 672

视觉SLAM十四讲slambook2示例代码所有依赖库的编译安装教程(VTK、OpenCV3、ceres、Sophus、g2o、Eigen、Pangolin、googletest等)

这是2021年的最后一篇博客~ 新年快乐! 0 准备工作   首先需要先安装cmake编译器: sudo apt-get install cmake   以及,将十四讲的示例代码克隆下来(十四讲的源码地址点击这里): git clone --recursive https://github.com/gaoxiang12/slambook2.git   由于里面的3rdparty文件夹内所有内容都来自于其他github仓库,所以需要加上--recursive参数,不然克隆下来的内容不完整。   如果你.

zeeq的博客 7839

HTTP 验证 Tomcat中进行基本验证 (Basic Authentication) 和摘要验证 (Digest Authentication)

传送门:http://blog.csdn.net/renminzdb/article/details/42422141

王孙小蛮的自留地 965

【OS】AUTOSAR OS系统调用产生Trap的过程详解

AUTOSAR OS系统调用产生Trap的过程详解

qq_36056498的博客 2439

PCL DBSCAN密度聚类【2026最新版】

点云密度聚类的PCL代码实现,与matlab自带的密度聚类算法进行对比验证,结果一致。3万多个点能再2秒左右运行出结果,算法效率尚可。

点云侠的博客 5682

http协议之digest(摘要)认证,详细讲解并附Java SpringBoot源码

http协议之digest(摘要)认证,详细讲解并附Java SpringBoot源码

一火的专栏 8460

2025年CSP-X初赛真题及答案解析(江西)(完善程序第1题)

2025年CSP-X初赛真题及答案解析(江西)(完善程序第1题)

王老师青少年编程 714

java web安全机制_基于Tomcat的Java Web的安全验证机制

做过WEB项目的都知道,一但涉及用户,我们不得不为用户登录写一堆繁杂的验证代码。当然Spring AOP的诞生为我们的权限管理提供了不少的便利。甚至你也以用自己写的Filter(过滤器)来对你的程序进行登录时的验证。今天在这里和大家分享一种更为简便的方法,它就是Java Web的安全验证机制。这种机制是对立地WEB的容器之上的,如Tomcat,JBoss等,你只须在你应用中的web.xml文件中做...

weixin_32273747的博客 297

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

精雕软件(JDpaint) 5.5 绿色版

精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版精雕软件(JDpaint) 5.5 绿色版

基于Tomcat的Java Web的安全验证机制

做过WEB项目的都知道,一但涉及用户,我们不得不为用户登录写一堆繁杂的验证代码。当然Spring AOP的诞生为我们的权限管理提供了不少的便利。甚至你也以用自己写的Filter(过滤器)来对你的程序进行登录时的验证。今天在这里和大家分享一种更为简便的方法,它就是Java Web的安全验证机制。        这种机制是对立地WEB的容器之上的,如Tomcat,JBoss等,你只须在你应用中的we

2148

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Tomcat(69)Tomcat的集群同步机制

Tomcat的集群同步机制主要用于实现集群节点之间会话数据的复制和同步,这样即使一个节点出现故障,用户的会话信息也不会丢失。在Tomcat中,这通常通过配置组件实现。

qq_43012298的博客 1039

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

Java的类加载机制以及tomcat类加载对比

标题java类加载过程 标题加载 ->验证 ->准备 -> 解析 ->初始化 加载:将class文件解析成二进制文件进入jvm中 验证:校验文件是否符合java规范,是否会侵害jvm 准备:为类的静态变量赋值默认初始值(如:int赋0),final修饰的变量直接赋值 解析:符号引用(任何形式的字面量,可以定位到目标)解析为直接引用(指针) 初始化:为类静态变量赋正确的初始值。 类加载器:分为:启动类加载器(c++编写),扩展类加载器(加载jre的中的jar包的类),应用类加载器 双亲

one_two 399

tomcat源码阅读之安全机制

一、领域(Realm): 1、Principal接口代表角色信息,包含了三个成员:用户名、密码、role列表(以逗号分隔),对应了tomcat-users.xml文件中一行user信息: GenericPrincipal作为Principal接口的默认实现类,提供了hasRole函数,通过这个函数可以判断该角色是否支持指定的role; 2、Realm接口代表领域对象,是一个用来对用...

weixin_30877227的博客 246

Tomcat的安全性

Web应用程序的一些内容是受限的,只有授权的用户在提供了正确的用户名和密码后才能查看他们,servlet技术支持通过配置部署 描述器(web.xml)来对这些内容进行访问控制,那么web容器是怎么样支持安全限制功能的呢?servlet容器是通过一个名为“验证器阀”来支持安全限制的,当servlet容器启动时,验证器阀 会被添加到Context容器的管道中,在调用Wrapper的基础阀之前...

detscaozv622278146的博客 487

java验证 web实现basic与FORM验证

 from http://www.cnblogs.com/ansen/articles/2017360.html web实现basic与FORM验证  在web应用中,要经常对用户的身份进行验证的,但其实TOMCAT下配合SERVLET的话,也可以实现一些简单的验证,以往  可能大家都会忽略之,现再简单总结学习之。  1、BASIC验证机制       这有点象WI

qingfeng2556的博客 861

java的类加载机制

java的类加载机制1.什么是类加载机制1.1类加载过程(分为三个步骤)1)加载2)连接(又分为3个阶段)2.1)验证2.2)准备2.3)解析3)初始化2.什么是双亲委派机制 (图是扣来的) 1.什么是类加载机制 就是将经(java源码)编译器编译后的.class文件中的二进制数据读入到内存中,将其放在运行时数据区的方法区内,然后在堆区创建一个 java.lang.Class对象,用来封装类在方法区内的数据结构。 (.class文件可能来源于本地磁盘、数据库、网络传输或者jar包等。) 注意:(Class

m0_49976286的博客 165

tomcat配合实现basic与FORM验证

在web应用中,要经常对用户的身份进行验证的,但其实TOMCAT下配合SERVLET的话,也可以实现一些简单的验证,以往可能大家都会忽略之,现再简单总结学习之。1、BASIC验证机制     这有点象WINDOWS集成验证机制,就是验证时弹出一个窗口,要你输入用户名和密码。做法如下     首先建立在webapps下建立目录member,下面放一个需要假设要权限才能查看的页面test.ht

jackyrongvip的专栏 850

类加载机制

目录 1、类加载过程(懒加载方式) 2、类加载器和双亲委派机制 常用类加载器: 双亲委派机制(阅读源码) : 为什么要设计双亲委派机制? 打破双亲委派 1、类加载过程(懒加载方式) 加载 :IO读入字节码文件 验证:检查字节码正确性 准备:静态变量赋默认值、分配内存 解析:静态方法(静态链接),运行期间(动态链接) 初始化 :静态变量赋初始值 使用 卸载 2、类加载器和双亲委派机制 常用类加载器: 启动类加载器:负责加载支撑JVM运行的位于JRE的lib目...

liufang_king的博客 147

[原创]基于TOMCAT的安全认证

      做过WEB项目的都知道,一但涉及用户,我们不得不为用户登录写一堆繁杂的验证代码。当然Spring AOP的诞生为我们的权限管理提供了不少的便利。甚至你也以用自己写的Filter(过滤器)来对你的程序进行登录时的验证。今天在这里和大家分享一种更为简便的方法,它就是Java Web的安全验证机制。       这种机制是对立地WEB的容器之上的,如Tomcat,JBoss等,你只须在你应用...

zxmzxy的专栏 302

《pro Spring》学习笔记之Spring HTTP 远程方法调用集成Tomcat实现安全验证

 package ch16.SecureHTTP;import org.apache.commons.httpclient.Credentials;import org.apache.commons.httpclient.HttpClient;import org.apache.commons.httpclient.UsernamePasswordCredentials;import or

我的世界我的梦 4034

tomcat配置

将端口80的TCP连接传递给端口8080 linux的内嵌iptables特性允许各种防护墙、网络过滤和信息传递。而且可以简单的将端口80的TCP连接传递给tomcat iptables特性是linux内核的特性,通常默认为enabled(使能)状态,并可使用iptables命令行工具进行配置。 [root@TA30-53 creditease.cn]# iptables -t nat -L

娜娜的专栏 470
上一篇: Magento初探
下一篇: 番茄工作法图解笔记
soliddream66
博客等级 码龄19年 93粉丝 65原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值