捕風之巢

          統計

          留言簿(3)

          java友情鏈接

          閱讀排行榜

          評論排行榜

          創建安全的web應用程序

          為了保證web應用程序的安全,應該對登錄的用戶進行身份驗證。在WebLogic Server中進行web應用程序的身份驗證有兩種方式:
          1.基本驗證方式(Basic Authentication)
          2.表單驗證方式(Form Authentication)
          基本驗證方式比較簡單,而表單驗證方式可以提供自定義的登錄頁面和出錯處理頁面。
          1.基本驗證方式(Basic Authentication)
          ?采用這種驗證方式的web應用程序,用戶訪問時首先會彈出一個登錄界面要求用戶輸入用戶名和密碼,然后查看此用戶是否在web應用中定義的安全角色中。
          ?開發一個以基本驗證方式進行身份驗證的web應用程序的基本步驟如下。
          ?(1)在web應用程序的描述符web.xml中做如下聲明:

          ? < security-role >
            
          < role-name > webuser </ role-name >
           
          </ security-role >


          指定驗證方式為基本驗證方式:用<login-config>定義。例如:

          < login-config >
            
          < auth-method > BASIC </ auth-method >
            
          < realm-name > default </ realm-name >
           
          </ login-config >

          定義被保護的資源,例如下面這段聲明指明了只有角色webuser才能訪問被保護的資源:

          < security-constraint >
           
          < web-resource-collection >
            
          < web-resource-name > Success </ web-resource-name >
            
          < url-pattern > /welcome.jsp </ url-pattern >
             
          < http-method > GET </ http-method >
             
          < http-method > POST </ http-method >
           
          </ web-resource-collection >
           
          < auth-constraint >
            
          < role-name > webuser </ role-name >
           
          </ auth-constraint >
           
          </ security-constraint >


          (2)在weblogic.xml文件中定義安全角色到weblogic server中用戶或用戶組的映射。即指定weblogic server中的哪些實體屬于安全角色。例如下面這段聲明將weblogic server中的實體myGroup映射到安全角色webUser:

          < weblogic-web-app >
           
          < security-role-assignment >
            
          < role-name > webuser </ role-name >
            
          < principal-name > myGroup </ principal-name >
           
          </ security-role-assignment >
          </ weblogic-web-app >


          ?下面時完整的相關頁面:
          ?web.xml文件
          ?
          ?

          < web-app >
          ?
          < welcome-file-list >
          ??
          < welcome-file > welcome.jsp </ welcome >
          ?
          </ welcome-file-list >
           
          < security-constraint >
           
          < web-resource-collection >
            
          < web-resource-name > Success </ web-resource-name >
            
          < url-pattern > /welcome.jsp </ url-pattern >
             
          < http-method > GET </ http-method >
             
          < http-method > POST </ http-method >
           
          </ web-resource-collection >
           
          < auth-constraint >
            
          < role-name > webuser </ role-name >
           
          </ auth-constraint >
           
          </ security-constraint >
           
          < login-config >
            
          < auth-method > BASIC </ auth-method >
            
          < realm-name > default </ realm-name >
           
          </ login-config >
           
          < security-role >
            
          < role-name > webuser </ role-name >
           
          </ security-role >
          </ web-app >

          weblogic.xml文件

          < weblogic-web-app >
           
          < security-role-assignment >
            
          < role-name > webuser </ role-name >
            
          < principal-name > myGroup </ principal-name >
           
          </ security-role-assignment >
          </ weblogic-web-app >

          ?

          welcome.jsp文件

          < html >
          ?
          < head >
          ??
          < title > Browser?Based?Authentication?Example?Welcome?Page </ title >
          ?
          </ head >
          ?
          < body >
          ?
          < h1 > Browser?Based?Authentication?Example?Welcome?Page </ h1 >
          ?
          < p > Welcome <% = request.getRemoteUser() %>
          ?
          </ body >
          </ html >

          2.表單驗證方式
          使用表單驗證方式進行web應用程序的身份驗證,需要開發者定義一個登錄頁面和登錄失敗的錯誤處理頁面。登錄頁面可以時html、jsp或servlet。使用這種驗證方式的好處時可以對程序有更進一步的控制。登錄頁面應該讓用戶輸入用戶名和密碼,錯誤處理頁面應該將驗證失敗的信息反饋給用戶。開發使用表單驗證方式的web應用程序的基本步驟如下。
          (1)編寫登錄頁面和錯誤處理頁面,在web應用程序的歡迎頁面中加入到登錄頁面的超級鏈接,提示用戶首先進行登錄。登錄頁面舉例:


          < form? method ="POST" ?action ="j_security_check" >
          ???
          < input? type ="text" ?name ="j_username" >
          ???
          < input? type ="text" ?name ="j_password" >
          ???
          < input? type ="submit" ?value ="Log?in" >
          </ form >

          (2)配置web.xml

          < web-app >
          < welcome-file-list >
          ??
          < welcome-file > welcome.jsp </ welcome >
          ?
          </ welcome-file-list >
          < login-config >
          ???
          < auth-method > FORM </ auth-method >
          ???
          < realm-name > Web?Demo </ realm-name >
          ???
          < form-login-config >
          ??????
          < form-login-page > /admin/login.jsp </ form-login-page >
          ??????
          < form-error-page > /admin/error.jsp </ form-error-page >
          ???
          </ form-login-config >
          </ login-config >
          < security-role >
            
          < role-name > webuser </ role-name >
          </ security-role >
          < security-constraint >
           
          < web-resource-collection >
            
          < web-resource-name > Success </ web-resource-name >
            
          < url-pattern > /welcome.jsp </ url-pattern >
             
          < http-method > GET </ http-method >
             
          < http-method > POST </ http-method >
           
          </ web-resource-collection >
           
          < auth-constraint >
            
          < role-name > webuser </ role-name >
           
          </ auth-constraint >
          </ security-constraint >
          < security-constraint >
           
          < web-resource-collection >
            
          < web-resource-name > login </ web-resource-name >
            
          < url-pattern > /login2.jsp </ url-pattern >
             
          < http-method > GET </ http-method >
             
          < http-method > POST </ http-method >
           
          </ web-resource-collection >
           
          < auth-constraint >
            
          < role-name > webuser </ role-name >
           
          </ auth-constraint >
          </ security-constraint >
          </ web-app >

          (3)在weblogic.xml文件中定義安全角色到weblogic server中實體(用戶或用戶組)的映射。例如:

          < weblogic-web-app >
           
          < security-role-assignment >
            
          < role-name > webuser </ role-name >
            
          < principal-name > myGroup </ principal-name >
           
          </ security-role-assignment >
          </ weblogic-web-app >

          posted on 2006-10-11 10:50 捕風 閱讀(485) 評論(0)  編輯  收藏 所屬分類: web開發

          主站蜘蛛池模板: 泰兴市| 阳原县| 阿图什市| 星子县| 桃园市| 永和县| 柳林县| 宁阳县| 定陶县| 潜山县| 建昌县| 扎兰屯市| 亳州市| 突泉县| 龙州县| 高青县| 桑日县| 乳山市| 都匀市| 汕尾市| 新民市| 新野县| 巴青县| 阿城市| 常宁市| 微博| 麻城市| 林芝县| 时尚| 兴城市| 镇赉县| 长泰县| 田林县| 水富县| 定州市| 新和县| 方山县| 昌都县| 宁夏| 台中市| 德昌县|