安全域是tomcat内置的功能,在org.apache.catalina.realm接口中声明了把一组用户名,口令及所关联的角色集成到tomcat的方法中,tomcat5提供了4个实现这一接口的类,它们代表了4种安全域类型。
下面我一一介绍其相关配置,及其应用方法。
运行环境:windows2000,tomcat5.0.28,jdk1.5,jdbc3.0,sqlserver2000,
测试目录:%tomcat%/webapps/area目录。
1. 内存域:类名,memoryrealm;在初始化阶段,从xml文件中读取安全验证信息,并把它们以一组对象的形式放在内存中。
对于资源访问它有三种方式:basic、digest、form。使用basic authentication通过被认为是不安全的,因为它没有强健的加密方法,除非在客户端和服务器端都使用https或者其他密码加密码方式(比如,在一个虚拟私人网络中)。
配置文件%tomcat%/conf/catalina/localhost/area.xml为:
<?xml version='1.0' encoding='utf-8'?>
<context docbase="d:/jakarta-tomcat-5.0.28/webapps/area" path="/area" reloadable="true"
workdir="work/catalina/localhost/area">
<realm classname="org.apache.catalina.realm.memoryrealm"/>
</context>
web应用所在的web-inf/web.xml为:
<?xml version="1.0" encoding="gb2312"?>
<!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>
<security-constraint>
<display-name>sessiontest secruity constraint</display-name>
<web-resource-collection>
<web-resource-name>protected area</web-resource-name>
<url-pattern>/test/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<role-name>cool</role-name>
</auth-constraint>
</security-constraint>
<!--以下是基于basic验证-->
<login-config>
<auth-method>basic</auth-method>
<realm-name>sessiontest realm</realm-name>
</login-config>
<!--以下是基于digest验证-->
<!-- <login-config>
<auth-method>digest</auth-method>
<realm-name>sessiontest realm</realm-name>
</login-config>
-->
<!--以下是基于form验证-->
<!-- <login-config>
<auth-method>form</auth-method>
<realm-name>sessiontest realm</realm-name>
<form-login-config>
<form-login-page>/usercheck.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
若采用form验证,usercheck.jsp,主要参数(不要去更改它)设置如下:
<form method="post" action="j_security_check">
<input type="text" name="j_username"><br>
<br>
<input type="password" name="j_password"><br>
<br>
<input type="submit" value="login">
<input type="reset" value="reset">
</form>
-->
<security-role>
<description> session jdbctest </description>
<role-name>cool</role-name>
</security-role>
</web-app>
基中<url-pattern>/test/*</url-pattern>表示受保护的资源为:http://localhost:8080/area/test/下的所有资源。role角色cool在%tomcat%conf中的tomcat-users.xml中添加:
<role rolename="cool"/>
<user username="zxj1" password="zxj2" roles="cool"/>
2. jdbc域:类名,jdbcrealm,通过jdbc驱动程序访问存在数据库中的安全验证。
3. 数据源域:类名,datasourcerealm,通过jndi数据源访问存在数据库中的安全验证信息。
4. jndi域:类名,jndirealm,通过jndiproveider访问存放在基于ldap的目录服务器中的安全验证信息。
闽公网安备 35060202000074号