tomcat form authority

来源:互联网 发布:淘宝网香奈尔女装 编辑:程序博客网 时间:2024/05/16 00:38
refered url:  http://www.jaxmao.org/tomcat-docs/realm-howto.html

定义:
域(Realm)是一个存储用户名,密码以及和用户名相联系的角色的”数据库”,用户名,密码用来验证用户对一个或多个web应用程序的有效性。访问应用程序中特定资源的权限是被授予了拥有特殊角色的用户,而不是相关的用户名。通过用户名相关联,一个用户可以有任意数量的角色。

种类:
JDBCRealm
DataSourceRealm
JNDIRealm
MemoryRealm
JAASRealm
UserDatabaseRealm

用法:
一:MemoryRealm
1 setup context (%tomcat%/conf/Catalina/localhost/RealmMemory.xml) and point out the realm type
    <Context path="/RealmMemory" docBase="E:/projects/RealmMemory"
             debug="0" privileged="true">
             # "
className" point out realm's type
             # "pathname" point out the file store information about account,pwd,role
             <Realm className="org.apache.catalina.realm.MemoryRealm"
                    pathname="conf/MemoryRealm-users.xml" />
     </Context>

2 create the file
%tomcat%/conf/MemoryRealm-users.xml
   <tomcat-users>
      <user name="guang"  password="" roles="test" />
   </tomcat-users>


3 config the project's WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
    <security-constraint>
        <web-resource-collection>
               <web-resource-name>Protected Area</web-resource-name>
             <!--  # point which links will be protected-->
               <url-pattern>/*</url-pattern>
               <http-method>GET</http-method>
               <http-method>POST</http-method>
               <http-method>PUT</http-method>
               <http-method>DELETE</http-method>
        </web-resource-collection>
        <auth-constraint>
                <!--# point out which role will be permitte to access the links-->
                <role-name>test</role-name>
        </auth-constraint>
    </security-constraint>                                                                       

    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>Memory Realm</realm-name>
        <form-login-config>
               <!-- # default page before login-->
                 <form-login-page>/login.jsp</form-login-page>
                 <form-error-page>/error.jsp</form-error-page>
        </form-login-config>
    </login-config>

    <security-role>
                <role-name>test</role-name>
    </security-role>
 
    <welcome-file-list>
        <!--# default welcome page-->
        <welcome-file>/success.jsp</welcome-file>
    </welcome-file-list>
</web-app>

3 login.jsp

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>MemoryRealm Example</title>
   </head>
   <body>
      <form method="post" action="j_security_check">
         <input type="text" name="j_username">
         <input type="password" name="j_password">
         <input type="submit" value="default-begin">
      </form>
   </body>
</html>



二:
JDBCRealm
1  create context file
   <Context path="/RealmJDBC" docBase="E:/projects/RealmJDBC" debug="0" privileged="true">
      <Realm className="org.apache.catalina.realm.JDBCRealm" debug="99"
             driverName="com.mysql.jdbc.Driver"
             connectionURL="jdbc:mysql://localhost/authority"
             connectionName="user" connectionPassword="1234567"
             userTable="users" userNameCol="user_name" userCredCol="pwd"
             userRoleTable="roles" roleNameCol="role" />
   </Context>

2 create refere database and tables
   CREATE DATABASE `authority`;
   CREATE TABLE `users` (

     `id` int(11) NOT NULL auto_increment,

     `user_name` char(20) default NULL,
     `pwd` char(20) default NULL,
      PRIMARY KEY  (`id`));
   CREATE TABLE `roles` (

     `id` int(11) NOT NULL auto_increment,

     `user_name` char(20) default NULL,
     `role` char(20) default NULL,
     PRIMARY KEY  (`id`);
  use authority;
  insert into users values("guang","123",3);
  insert into roles values("guang","test",3);

3 add mysql's driver into folder %tomcat%/common/lib/mysql-connector-java-3.1.6-bin.jar

4 config the project's WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
    <security-constraint>
        <web-resource-collection>
               <web-resource-name>Protected Area</web-resource-name>
             <!--  # point which links will be protected-->
               <url-pattern>/*</url-pattern>
               <http-method>GET</http-method>
               <http-method>POST</http-method>
               <http-method>PUT</http-method>
               <http-method>DELETE</http-method>
        </web-resource-collection>
        <auth-constraint>
                <!--# point out which role will be permitte to access the links-->
                <role-name>test</role-name>
        </auth-constraint>
    </security-constraint>                                                                       

    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>JDBC Realm</realm-name>
        <form-login-config>
               <!-- # default page before login-->
                 <form-login-page>/login.jsp</form-login-page>
                 <form-error-page>/error.jsp</form-error-page>
        </form-login-config>
    </login-config>

    <security-role>
                <role-name>test</role-name>
    </security-role>
 
    <welcome-file-list>
        <!--# default welcome page-->
        <welcome-file>/success.jsp</welcome-file>
    </welcome-file-list>
</web-app>

5 login.jsp

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>MemoryRealm Example</title>
   </head>
   <body>
      <form method="post" action="j_security_check">
         <input type="text" name="j_username">
         <input type="password" name="j_password">
         <input type="submit" value="default-begin">
      </form>
   </body>
</html>

三 DataSourceRealm
refered url: http://blog.csdn.net/xuliang_net/archive/2004/07/01/31201.aspx

1. create context file
<Context path="/RealmDataSource" docBase="E:/projects/RealmDataSource" debug="0" privileged="true">

    <Resource    name="jdbc/RealmDatasource"  auth="Container"  type="javax.sql.DataSource" />     
    <ResourceParams name="jdbc/RealmDatasource">
        <parameter>
            <name>factory</name>
            <value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
        </parameter>
        <!-- Maximum number of dB connections in pool. Make sure you
             configure your mysqld max_connections large enough to handle
             all of your db connections. Set to 0 for no limit.
         -->
        <parameter>
            <name>maxActive</name>
            <value>100</value>
        </parameter>
        <!-- Maximum number of idle dB connections to retain in pool.
             Set to 0 for no limit.
         -->
        <parameter>
            <name>maxIdle</name>
            <value>30</value>
        </parameter>
        <!-- Maximum time to wait for a dB connection to become available
             in ms, in this example 10 seconds. An Exception is thrown if
             this timeout is exceeded.  Set to -1 to wait indefinitely.
        -->
        <parameter>
            <name>maxWait</name>
            <value>10000</value>
        </parameter>
        <parameter>
            <name>driverClassName</name>
            <value>com.mysql.jdbc.Driver</value>
        </parameter>
        <parameter>
            <name>url</name>
            <value>jdbc:mysql://localhost/authority</value>
        </parameter>
        <parameter>
            <name>username</name>
            <value>user</value>
        </parameter>
        <parameter>
            <name>password</name>
            <value>1234567</value>
        </parameter>
    </ResourceParams>

    <Realm    className="org.apache.catalina.realm.DataSourceRealm"
            debug="99"
            dataSourceName="jdbc/RealmDatasource"
            localDataSource="true"
            userTable="users"
            userNameCol="user_name"
            userCredCol="pwd"
            userRoleTable="roles"
            roleNameCol="role"    />

</Context>

2 create refere database and tables

3 add datasource dirver to %tomcat%/common/lib/commons-dbcp.jar

4 config the project's WEB-INF/web.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app>
    <security-constraint>
        <web-resource-collection>
               <web-resource-name>Protected Area</web-resource-name>
             <!--  # point which links will be protected-->
               <url-pattern>/*</url-pattern>
               <http-method>GET</http-method>
               <http-method>POST</http-method>
               <http-method>PUT</http-method>
               <http-method>DELETE</http-method>
        </web-resource-collection>
        <auth-constraint>
                <!--# point out which role will be permitte to access the links-->
                <role-name>test</role-name>
        </auth-constraint>
    </security-constraint>                                                                       

    <login-config>
        <auth-method>FORM</auth-method>
        <realm-name>DataSource Realm</realm-name>
        <form-login-config>
               <!-- # default page before login-->
                 <form-login-page>/login.jsp</form-login-page>
                 <form-error-page>/error.jsp</form-error-page>
        </form-login-config>
    </login-config>

    <security-role>
                <role-name>test</role-name>
    </security-role>
 
    <welcome-file-list>
        <!--# default welcome page-->
        <welcome-file>/success.jsp</welcome-file>
    </welcome-file-list>
</web-app>


四 jaasRealm
http://www.winu.cn/viewthread.php?tid=70665

http://360doc.com/showWeb/0/0/715158.aspx
http://hi.baidu.com/kekemao1/blog/item/7db13209df3b5dac2eddd414.html
http://blog.163.com/among_1985/blog/static/27500523200810107320674/