zoukankan      html  css  js  c++  java
  • 在 Tomcat 中设置 JDBCRealm

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

    JDBCRealm 的配置步骤如下:

    1. 在 $TOMCAT_HOMEconfserver.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_HOMElib 目录中。
    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:Usershuey>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
  • 相关阅读:
    李彦宏:创业成功五招即可
    JS无聊之作——换肤切换样式
    从3个科技公司里学到的57条经验(转载)
    早该知道的7个JavaScript技巧
    ASP.NET Cookie 概述
    曝光SEO高手藏在内心的SEO秘籍
    18种最实用的网站推广方法大全
    javascript的IE和Firefox兼容性问题
    增加反向链接的35个技巧
    常用JS片段
  • 原文地址:https://www.cnblogs.com/huey/p/4515340.html
Copyright © 2011-2022 走看看