1.2 基本自定义数据库用户表的认证配置:
如果你想基于自己定义的数据表进行认证,那你得在META-INF文件夹下创建一个名为 context.xml文件,配置如下内容:
<?xml version="1.0" encoding="UTF-8"?>
<Context path="/mycms">
<Realm className="org.apache.catalina.realm.JDBCRealm"
connectionName="root"
connectionPassword="123456"
connectionURL="jdbc:mysql://localhost:3306/mycms"
driverName="com.mysql.jdbc.Driver"
roleNameCol="rolename"
userCredCol="password"
userNameCol="username"
userRoleTable="mc_userroles"
userTable="mc_users"/>
</Context>
这里配置了数据库的连接信息以及指定了认证的用户表及角色表。
2 定义资源/方法约束
在web.xml文件中定义如元素:
<security-constraint>
<display-name>MyCMS</display-name>
<web-resource-collection>
<web-resource-name>Protected Area</web-resource-name>
<!-- Define the context-relative URL(s) to be protected -->
<url-pattern>/admin/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<!-- Anyone with one of the listed roles may access this area -->
<role-name>Admin</role-name>
</auth-constraint>
</security-constraint>
<!-- Login configuration uses form-based authentication -->
<login-config>
<auth-method>BASIC</auth-method>
<realm-name>MyCMS</realm-name>
</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>Admin</role-name>
</security-role>
至此我们基本TOMCAT的安全认证配置就完成了。至于其它三种方式,配置类似,可以参考2中定义的web.xml文件