在 Tomcat 中设置 JDBCRealm

除了默认配置的 DataSourceRealm,Tomcat 还支持 JDBCRealm,它通过 JDBC 来访问记录在关系数据库里的认证信息。

JDBCRealm 的配置步骤如下:

  1. 在 $TOMCAT_HOME\conf\server.xml 配置 <Reaml/> 元素。
    <Realm  className="org.apache.catalina.realm.JDBCRealm"
        driverName="com.mysql.jdbc.Driver"
        connectionURL="jdbc:mysql://localhost/tomcat"
        connectionName="root" connectionPassword="root"
        userTable="users" userNameCol="username" userCredCol="userpass"
        userRoleTable="roles" roleNameCol="userrole" />

    <Reaml /> 元素属性说明:

    属性说明
     className Tomcat 的 JDBCRealm 实现类 
     driverName JDBC 驱动类
     connectionURL 数据库连接地址
     connectionName 数据库登录用户
     connectionPassword  数据库登录密码
     userTable 用户表的表名
     userNameCol 用户表中用户列的列名
     userCredCol 用户表中密码列的列名
     userRoleTable 角色表的表名
     roleNameCol 角色表中的角色列

    注:<Realm/> 元素可以放在 <Engine/> 元素中,这时该 Realm 会被所有应用共享。 放在 <Host/> 元素中,会被该 Host 下的应用程序共享。放在 <Context/> 元素中,则只有对应的应用程序能被访问。

  2. 将 JDBC 驱动 jar 文件放置在 $TOMCAT_HOME\lib 目录中。
  3. 在数据库中创建用户表与角色表,表名和命名要与上述的配置一致。
    CREATE TABLE `users` (
      `username` varchar(32) NOT NULL,
      `userpass` varchar(32) NOT NULL,
      PRIMARY KEY (`username`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
    
    CREATE TABLE `roles` (
      `username` varchar(32) NOT NULL,
      `userrole` varchar(32) NOT NULL,
      PRIMARY KEY (`username`,`userrole`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  4. 在表中配置用户与角色信息。
    INSERT INTO `tomcat`.`users` (`username`, `userpass`) VALUES ('admin', 'admin');
    INSERT INTO `tomcat`.`users` (`username`, `userpass`) VALUES ('huey', 'huey');
    INSERT INTO `tomcat`.`users` (`username`, `userpass`) VALUES ('suer', 'suer');
    
    INSERT INTO `tomcat`.`roles` (`username`, `role`) VALUES ('admin', 'admin');
    INSERT INTO `tomcat`.`roles` (`username`, `role`) VALUES ('admin', 'common');
    INSERT INTO `tomcat`.`roles` (`username`, `role`) VALUES ('huey', 'common');
    INSERT INTO `tomcat`.`roles` (`username`, `role`) VALUES ('suer', 'common');
    INSERT INTO `tomcat`.`roles` (`username`, `role`) VALUES ('suer', 'vip');
  5. 新建一个 Java Web 工程,编辑 web.xml 文件。
  6. 配置 <security-role/> 元素来定义角色。
    <security-role>  
        <role-name>admin</role-name>  
    </security-role>
    <security-role>  
        <role-name>common</role-name>  
    </security-role> 
    <security-role>  
        <role-name>vip</role-name>  
    </security-role> 
  7. 配置 <security-constraint/> 元素,指定角色可访问的资源集和可使用的 HTTP 方法。
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Public resources</web-resource-name>
            <url-pattern>/home/*</url-pattern>
            <http-method>HEAD</http-method>
            <http-method>GET</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>common</role-name>
        </auth-constraint>
    </security-constraint>
    
    <security-constraint>
        <web-resource-collection>
            <web-resource-name>Secret resources</web-resource-name>
            <url-pattern>/blog/*</url-pattern>
            <url-pattern>/photo/*</url-pattern>
            <http-method>HEAD</http-method>
            <http-method>GET</http-method>
            <http-method>POST</http-method>
            <http-method>PUT</http-method>
        </web-resource-collection>
        <auth-constraint>
            <role-name>admin</role-name>
            <role-name>vip</role-name>
        </auth-constraint>
    </security-constraint>
  8. 配置 <login-config/> 元素,指定认证方式为基本认证,并指定安全域。
    <login-config>
        <auth-method>BASIC</auth-method>
        <realm-name>hueyhome</realm-name>
    </login-config>
  9. 测试。
    C:\Users\huey>curl -I -u "suer:suer" http://localhost:8080/helloweb/blog/index.html
    HTTP/1.1 200 OK
    Server: Apache-Coyote/1.1
    Pragma: No-cache
    Cache-Control: no-cache
    Expires: Thu, 01 Jan 1970 08:00:00 CST
    Accept-Ranges: bytes
    ETag: W/"261-1431758220107"
    Last-Modified: Sat, 16 May 2015 06:37:00 GMT
    Content-Type: text/html
    Content-Length: 261
    Date: Tue, 19 May 2015 11:44:20 GMT

     

转载于:https://www.cnblogs.com/huey/p/4515340.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值