实现权限控制

使用Tomcat实现页面访问控制主要有三种方式:

引用

 

  • 使用JDBCRealm利用数据库控制
  • 使用JNDIRealm利用远程访问控制
  • 使用MemoryRealm利用tomcat-users.xml文件控制

 


一、使用JDBCRealm利用数据库控制
1.The meaning of the attributes is as follow:

attribute

Meaning

driverName

The name of the driver needed to connect to the database

connectionURL

The connection URL used to connect to the database

userTable

The user's tables

userNameCol

The column in the user's table that contains the name

userCredCol

The column in the user's table that contains the password

userRoleTable

The user's roles table

roleNameCol

The column in the user's table that contains a role given to a user

connectionName

The name to use when connecting to the database. (Optional)

connectionPassword

The password to use when connecting to the database. (Optional)

digest

The algorithm used for digest passwords or "No" for plain passwords, the values can be "MD5", "MD2", "SHA", etc... (Optional)


2.server.xml
文件配制

Java代码

  1. <Realm className="org.apache.catalina.realm.JDBCRealm"
  2. driverName="com.mysql.jdbc.Driver"
  3. connectionURL="jdbc:mysql://localhost:3306/globalapp"
  4. connectionName="root" connectionPassword="x"
  5. roleNameCol="role_name" userCredCol="user_pass"
  6. userNameCol="user_name" userRoleTable="user_roles"
  7. userTable="users" debug="50"/>

<Realm className="org.apache.catalina.realm.JDBCRealm"

       driverName="com.mysql.jdbc.Driver"

       connectionURL="jdbc:mysql://localhost:3306/globalapp"

       connectionName="root" connectionPassword="x"

       roleNameCol="role_name" userCredCol="user_pass"

       userNameCol="user_name" userRoleTable="user_roles"

       userTable="users" debug="50"/>

 

引用


connectionURL
connectionNameconnectionPassword不能拼在一起使用一个符串,虽然Tomcat doc上使用的是这种方法,但试了很多次都没能成功。具体原因不详。如果有高手解决还望说明一下。


3.web.xml
文件配制

Xml代码

  1. <security-constraint>
  2. <web-resource-collection>
  3. <web-resource-name>Global App</web-resource-name>
  4. <url-pattern>/*</url-pattern>
  5. </web-resource-collection>
  6. <auth-constraint>
  7. <role-name>admin</role-name>
  8. </auth-constraint>
  9. </security-constraint>
  10.  
  11. <!-- 这是FORM验证 -->
  12. <login-config>
  13. <auth-method>FORM</auth-method>
  14. <realm-name>Global App DIGEST</realm-name>
  15.  
  16. <form-login-config>
  17. <form-login-page>/accessConsole/login.jsp</form-login-page>
  18. <form-error-page>/accessConsole/error.jsp</form-error-page>
  19. </form-login-config>
  20.  
  21. </login-config>
  22.  
  23. <security-role>
  24. <description>
  25. The role that is required to log into the Global application
  26. </description>
  27. <role-name>admin</role-name>
  28. </security-role>

    <security-constraint>

       <web-resource-collection>

            <web-resource-name>Global App</web-resource-name>

            <url-pattern>/*</url-pattern>

        </web-resource-collection>

        <auth-constraint>                           

            <role-name>admin</role-name>

        </auth-constraint>

    </security-constraint>

 

    <!-- 这是FORM验证 -->

    <login-config>

        <auth-method>FORM</auth-method>

        <realm-name>Global App DIGEST</realm-name>

       

        <form-login-config>

          <form-login-page>/accessConsole/login.jsp</form-login-page>

          <form-error-page>/accessConsole/error.jsp</form-error-page>

        </form-login-config>

        

    </login-config>

 

    <security-role>

        <description>

          The role that is required to log into the Global application

        </description>

        <role-name>admin</role-name>

    </security-role>


4.
数据库表建设

Sql代码

  1. create table users (
  2. user_name varchar(15) not null primary key,
  3. user_pass varchar(15) not null
  4. );
  5.  
  6. create table user_roles (
  7. user_name varchar(15) not null,
  8. role_name varchar(15) not null,
  9. primary key (user_name, role_name)
  10. );
  11.  
  12. insert into users values("xwood", "xwood");
  13. insert into user_roles values("xwood", "admin");

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)

);

 

insert into users values("xwood", "xwood");

insert into user_roles values("xwood", "admin");



这里除了采用FORM验证外还可以采用BASIC验证,别外还有一种客户端证书验证方法没有用过。采用BASIC验证方法只需要将<form-login-config>标签去掉即可。
二、使用JNDIRealm利用远程访问控制

引用


还未具体研究


三、使用MemoryRealm利用tomcat-users.xml文件控制
1.server.xml
配置

Xml代码

  1. <Resource name="UserDatabase" auth="Container"
  2. type="org.apache.catalina.UserDatabase"
  3. description="User database that can be updated and saved"
  4. factory="org.apache.catalina.users.MemoryUserDatabaseFactory"
  5. pathname="conf/tomcat-users.xml" />
  6. <Realm className="org.apache.catalina.realm.MemoryRealm"
  7. resourceName="UserDatabase"/>

<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" />

<Realm className="org.apache.catalina.realm.MemoryRealm"

       resourceName="UserDatabase"/>


其中,UserDatabaseserver.xml中已经完成配置
2.tomcat-users.xml
配置

Xml代码

  1. <role rolename="admin"/>
  2. <role rolename="user"/>
  3.  
  4. <user username="xwood" password="xwood" roles="admin"/>

  <role rolename="admin"/>

  <role rolename="user"/>

 

  <user username="xwood" password="xwood" roles="admin"/>


3.web.xml
配置

引用


JDBCRealm相同

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值