Tomcat UserDatabaseRealm
在我们的web应用的web.xml中,如果有<security-constraint>标签,那么恭喜你,你正在使用tomcat的realm接口,通过容器来管理你的安全验证。在上一篇博文中,对于自己的tomcat admin中也使用到了UserDatabaseRealm这个Realm的实现。
具体使用如下:
server.xml中的
<GlobalNamingResources> 标签下,包含了这样一个resource:
<Resource name="UserDatabase" auth="Container" type="org.apache.catalina.UserDatabase" description="User database that can be updated and saved" factory="org.apache.catalina.users.MemoryUserDatabaseFactory" pathname="conf/tomcat-users.xml"/>
其中描述了conf/tomcat-users.xml这个文件下,有定义tomcat的MemoryUserDatabase的实例的信息。
并在Engine标签下,应用了这个resource:
<Realm className="org.apache.catalina.realm.UserDatabaseRealm" resourceName="UserDatabase"/>
讲此资源配置给UserDatabaseRealm,这样,这个Engine下的所有host都可以使用这个realm了。
回到应用的web.xml下,配置:
<security-constraint>
<display-name>Tomcat Server Configuration Security Constraint</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<!-- Define the context-relative URL(s) to be protected -->
<url-pattern>*.jsp</url-pattern>
<url-pattern>*.do</url-pattern>
<url-pattern>*.html</url-pattern>
</web-resource-collection>
<auth-constraint>
<!-- Anyone with one of the listed roles may access this area -->
<role-name>elite-tomcat-admin-gui</role-name>
<role-name>elite-tomcat-admin</role-name>
</auth-constraint>
</security-constraint>
<!-- Login configuration uses form-based authentication -->
<login-config>
<auth-method>FORM</auth-method>
<realm-name>Tomcat Server Configuration Form-Based Authentication Area</realm-name>
<form-login-config>
<form-login-page>/login.jsp</form-login-page>
<form-error-page>/error.jsp</form-error-page>
</form-login-config>
</login-config>
<!-- Security roles referenced by this web application -->
<security-role>
<description>
The role that is required to log in to the Administration Application
</description>
<role-name>elite-tomcat-admin-gui</role-name>
</security-role>
<security-role>
<description>
Deprecated role name, that provides the same access as the "admin-gui" role.
</description>
<role-name>elite-tomcat-admin</role-name>
</security-role>
此处定义了那些url-pattern需要验证,什么role是可以验证通过的。
然后定义了验证方式,使用了FORM方式的验证:
login.jsp中大致如下:
<form method="POST" action='<%= response.encodeURL("j_security_check") %>' name="loginForm">
<input type="text" name="j_username" size="16" id="username"/>
<input type="password" name="j_password" size="16" id="password"/>
</form>
在页面中又这样的form和j_username,j_password的时候,就会去使用到realm的验证了。
最后还定义了security-role,与tomcat-user.xml中的role定义相互对应。
tomcat还有许多realm的标准实现,在http://tomcat.apache.org/tomcat-5.5-doc/realm-howto.html
都可以看到相关介绍与示例,如果需要容器来管理应用的安全认证,可以以参考使用。
页:
[1]