posts - 193,  comments - 520,  trackbacks - 0

          在數(shù)據(jù)庫里新建兩個表
          A、用戶表
          create table users (user_name varchar(20) not null,
                                         user_pass varchar(20) not null,
                                         PRIMARY KEY (user_name)) ;
          B、用戶與角色關(guān)聯(lián)表
          create table user_roles (user_name varchar(20) not null,
                                                  role_name varchar(20) not null,
                                                  PRIMARY KEY (user_name)) ;
          C、插入數(shù)據(jù)
          insert into users values('user1', 'password');
          insert into user_roles values('user1', 'manager');

          在tomcat的server.xml里加入描述
                <Realm  className="org.apache.catalina.realm.JDBCRealm"
                        driverName="org.gjt.mm.mysql.Driver"
                        connectionURL="jdbc:mysql://localhost/databaseName"
                        connectionName=yourname connectionPassword=yourpassword
                        userTable="users" userNameCol="user_name" userCredCol="user_pass"
                        userRoleTable="user_roles" roleNameCol="role_name" />

          在自己應(yīng)用程序的web.xml里加入描述(基于表單)
          <security-constraint>
            <web-resource-collection>
             <web-resource-name>My Test</web-resource-name>
             <url-pattern>/get.jsp</url-pattern>
            </web-resource-collection>
            <auth-constraint>
              <role-name>manager</role-name>    //能夠訪問的角色,可以多個
            </auth-constraint>
          </security-constraint>

          <login-config>
            <auth-method>FORM</auth-method>
              <realm-name>My Test</realm-name>
            <form-login-config>
            <form-login-page>/login.jsp</form-login-page>
            <form-error-page>/fail_login.html</form-error-page>  //認(rèn)證失敗后跳轉(zhuǎn)的頁面
            </form-login-config>
          </login-config>

          登錄表單必須包含輸入用戶姓名和口令的字段,它們必須被分別命名為j_username和j_password,表單將這二個值發(fā)送給j_security_check邏輯名字。
            下面是一個該表單如何在HTML網(wǎng)頁中實現(xiàn)的例子:
          <form method="POST" action="j_security_check">
            <input  type="text" name="j_username">
            <input  type="password" name="j_password">
              <input  type="submit" value="確定"/>
          </form>

          這樣當(dāng)對get.jsp進行訪問時,tomcat就會自動轉(zhuǎn)到login.jsp頁面實現(xiàn)認(rèn)證。對于簡單的認(rèn)證,小型系統(tǒng),采用Tomcat實現(xiàn)容器內(nèi)認(rèn)證是方便的。



          http://www.aygfsteel.com/ronghao 榮浩原創(chuàng),轉(zhuǎn)載請注明出處:)
          posted on 2005-12-28 14:00 ronghao 閱讀(1416) 評論(3)  編輯  收藏 所屬分類: 表現(xiàn)層相關(guān)

          FeedBack:
          # re: 用Tomcat實現(xiàn)容器內(nèi)認(rèn)證
          2006-11-21 14:17 | 沒弄明白
          沒實現(xiàn)啊
          能把具體的代碼發(fā)給我嗎 我想看看 form的那個啊

          謝謝  回復(fù)  更多評論
            
          # re: 用Tomcat實現(xiàn)容器內(nèi)認(rèn)證
          2007-06-07 10:41 | jayboy
          請問一個問題:
          驗證成功后將返回到哪個頁面,是當(dāng)前頁面么?   回復(fù)  更多評論
            
          # re: 用Tomcat實現(xiàn)容器內(nèi)認(rèn)證
          2007-06-08 17:37 | ronghao
          @jayboy
          應(yīng)該是這樣  回復(fù)  更多評論
            
          <2007年6月>
          272829303112
          3456789
          10111213141516
          17181920212223
          24252627282930
          1234567

          關(guān)注工作流和企業(yè)業(yè)務(wù)流程改進。現(xiàn)就職于ThoughtWorks。新浪微博:http://weibo.com/ronghao100

          常用鏈接

          留言簿(38)

          隨筆分類

          隨筆檔案

          文章分類

          文章檔案

          常去的網(wǎng)站

          搜索

          •  

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 鸡西市| 武平县| 嘉定区| 宽甸| 苗栗县| 桑日县| 景德镇市| 历史| 青神县| 山东| 伊川县| 两当县| 监利县| 泸定县| 哈巴河县| 德庆县| 泽普县| 尚志市| 临洮县| 拉孜县| 灵川县| 湄潭县| 宾川县| 浦北县| 固镇县| 高阳县| 永靖县| 郸城县| 多伦县| 专栏| 南部县| 应城市| 遵化市| 成都市| 集安市| 宜州市| 榆树市| 景德镇市| 惠东县| 阜康市| 清苑县|