gpt4 book ai didi

java - 对 web.xml 中的 感到困惑

转载 作者:搜寻专家 更新时间:2023-10-30 19:40:42 25 4
gpt4 key购买 nike

我明白了 <security-role><role-name>Admin</role-name></security-role>用于容器将其映射到 tomcat-users.xml( Realm )

但是如果我不使用 Realm 但我使用数据库我会感到困惑容器如何知道我的数据库中的哪个字段用于角色名称或者它在数据库字段名称中具有命名约定如“role_name”并且容器会知道它

谢谢大家的建议

最佳答案

只需使用数据库 Realm 并在 <Realm> 中配置表名和列名服务器配置文件中的元素。对于 Tomcat,这在 Realm HOWTO 中进行了描述.这是相关的摘录,来自 JDBCRealm chapter :

Quick Start

To set up Tomcat to use 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.
  2. 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.)
  3. Place a copy of the JDBC driver you will be using inside the $CATALINA_HOME/lib directory. Note that only JAR files are recognized!
  4. Set up a <Realm> element, as described below, in your $CATALINA_BASE/conf/server.xml file.
  5. Restart Tomcat 6 if it is already running.

Realm Element Attributes

To configure JDBCRealm, you will create a <Realm> element and nest it in your $CATALINA_BASE/conf/server.xml file, as described above. The attributes for the JDBCRealm are defined in the Realm configuration documentation.

Example

An example SQL script to create the needed tables might look something like this (adapt the syntax as required for your particular database):

create table users (
user_name varchar(15) not null primary key,
user_pass varchar(15) not null
);

create table user_roles (
user_name varchar(15) not null,
role_name varchar(15) not null,
primary key(user_name, role_name)
);

Example Realm elements are included (commented out) in the default $CATALINA_BASE/conf/server.xml file. Here's an example for using a MySQL database called "authority", configured with the tables described above, and accessed with username "dbuser" and password "dbpass":

<Realm className="org.apache.catalina.realm.JDBCRealm"
driverName="org.gjt.mm.mysql.Driver"
connectionURL="jdbc:mysql://localhost/authority?user=dbuser&amp;password=dbpass"
userTable="users" userNameCol="user_name" userCredCol="user_pass"
userRoleTable="user_roles" roleNameCol="role_name"/>

很清楚,不是吗?如果您已经在 Tomcat 中配置了 JDBC 数据源(用于连接池等),那么您也可以使用 DataSourceRealm相反。

tomcat-users.xml你说的是UserDatabaseRealm .

关于java - 对 web.xml 中的 <security-role> 感到困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5294252/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com