javascript
使用Java EE安全性和JBoss AS 7.x保护JSF应用程序的安全
企業(yè)應(yīng)用程序的常見要求是在登錄頁面后面保護(hù)所有JSF頁面。 有時(shí),您甚至希望在應(yīng)用程序內(nèi)部具有保護(hù)區(qū),只有擁有特定角色的用戶才能訪問這些保護(hù)區(qū)。 Java EE標(biāo)準(zhǔn)附帶實(shí)現(xiàn)受某些安全性約束保護(hù)的Web應(yīng)用程序所需的所有方法。 在此博客文章中,我們希望開發(fā)一個(gè)簡單的應(yīng)用程序,以演示這些方法的用法,并展示如何為兩個(gè)不同的角色構(gòu)建完整的JSF應(yīng)用程序。 由于該解決方案乍看之下可能會(huì)直截了當(dāng),因此有一些陷阱需要注意。
我們要關(guān)心的第一點(diǎn)是應(yīng)用程序的文件夾布局。 我們有三種不同的頁面:
- 所有用戶都應(yīng)該可以訪問登錄頁面和錯(cuò)誤頁面。
- 我們有一個(gè)主頁,只有經(jīng)過身份驗(yàn)證的用戶才能訪問。
- 我們有一個(gè)受保護(hù)的頁面,該頁面僅對(duì)角色roleed-role的用戶可見。
因此,將這三種類型的頁面放在三個(gè)不同的文件夾中:根文件夾src / main / webapp,文件夾src / main / webapp / pages和受保護(hù)的頁面位于src / main / webapp / pages / protected:
web.xml文件是定義我們要使用的角色以及如何將某些URL模式的可訪問性映射到這些角色的位置:
<security-constraint><web-resource-collection><web-resource-name>pages</web-resource-name><url-pattern>/pages/*</url-pattern><http-method>PUT</http-method><http-method>DELETE</http-method><http-method>GET</http-method><http-method>POST</http-method></web-resource-collection><auth-constraint><role-name>security-role</role-name><role-name>protected-role</role-name></auth-constraint> </security-constraint> <security-constraint><web-resource-collection><web-resource-name>protected</web-resource-name><url-pattern>/pages/protected/*</url-pattern><http-method>PUT</http-method><http-method>DELETE</http-method><http-method>GET</http-method><http-method>POST</http-method></web-resource-collection><auth-constraint><role-name>protected-role</role-name></auth-constraint> </security-constraint><security-role><role-name>security-role</role-name> </security-role> <security-role><role-name>protected-role</role-name> </security-role>如您所見,我們定義了兩個(gè)角色:security-role和protected-role。 只有擁有security-role和protected-role角色的用戶才能訪問與模式/ pages / *匹配的URL,而/ pages / protected / *下的頁面僅限于具有protected-role角色的用戶。
您可能會(huì)偶然發(fā)現(xiàn)的另一點(diǎn)是歡迎頁面。 乍一看,您希望將登錄頁面指定為歡迎頁面。 但這是行不通的,因?yàn)镾ervlet容器的登錄模塊會(huì)自動(dòng)將所有未經(jīng)授權(quán)的訪問重定向到登錄頁面。 因此,我們將應(yīng)用程序的主頁指定為歡迎頁面。 這已經(jīng)是一個(gè)受保護(hù)的頁面,但是當(dāng)用戶直接調(diào)用其URL時(shí),它將自動(dòng)重定向到登錄頁面。
<welcome-file-list><welcome-file>pages/home.xhtml</welcome-file> </welcome-file-list>現(xiàn)在我們幾乎完成了web.xml頁面。 我們要做的就是定義身份驗(yàn)證方法以及登錄頁面和錯(cuò)誤頁面,以防用戶輸入無效憑據(jù)時(shí)顯示。 必須引起注意的是,這兩個(gè)頁面均不包含受保護(hù)的URL(例如CSS或JavaScript文件),否則,甚至禁止訪問這兩個(gè)頁面,并且用戶會(huì)看到Application Server特定的錯(cuò)誤頁面。
<login-config><auth-method>FORM</auth-method><form-login-config><form-login-page>/login.xhtml</form-login-page><form-error-page>/error.xhtml</form-error-page></form-login-config> </login-config>在將應(yīng)用程序部署到JBoss Application Server時(shí),我們提供了一個(gè)名為jboss-web.xml的文件,該文件將我們的應(yīng)用程序連接到安全域:
<?xml version="1.0" encoding="UTF-8"?> <jboss-web><security-domain>java:/jaas/other</security-domain> </jboss-web>在standalone.xml中配置了“其他”安全域。 默認(rèn)配置要求用戶傳遞“ RealmUsersRoles”登錄模塊,該模塊從配置文件夾中的兩個(gè)文件application-users.properties和application-roles.properties獲取其用戶和角色定義。 您可以使用提供的添加用戶腳本將新用戶添加到該領(lǐng)域:
What type of user do you wish to add?a) Management User (mgmt-users.properties)b) Application User (application-users.properties) (a): bEnter the details of the new user to add. Realm (ApplicationRealm) : Username : bart Password : Re-enter Password : What roles do you want this user to belong to? (Please enter a comma separated list, or leave blank for none) : security-role,protected-role About to add user 'bart' for realm 'ApplicationRealm' Is this correct yes/no? yes在這里選擇正確的領(lǐng)域(ApplicationRealm)很重要,因?yàn)槟J(rèn)情況下,在standalone.xml中為“其他”登錄模塊配置了該領(lǐng)域。 在這里,您還可以用逗號(hào)分隔列表的形式提供用戶所擁有的角色。
<form method="POST" action="j_security_check" id=""><h:panelGrid id="panel" columns="2" border="1" cellpadding="4" cellspacing="4"><h:outputLabel for="j_username" value="Username:" /><input type="text" name="j_username"/><h:outputLabel for="j_password" value="Password:" /><input type="password" name="j_password"/><h:panelGroup><input type="submit" value="Login"/></h:panelGroup></h:panelGrid> </form>下一步是實(shí)現(xiàn)一個(gè)簡單的登錄表單,該表單將其數(shù)據(jù)提交到登錄模塊。 請(qǐng)注意輸入字段的ID以及表單的操作。 通過這種方式,表單被發(fā)布到登錄模塊,該模塊從請(qǐng)求中提取輸入的用戶名和密碼。 JSF開發(fā)人員可能想知道為什么我們使用標(biāo)準(zhǔn)HTML表單而不是元素。 原因是JSF表單元素跨越了自己的名稱空間,因此輸入字段的ID帶有表單ID的前綴(并且此ID不能為空)。
如果用戶已通過登錄表單,我們將向他顯示一個(gè)主頁。 但是,只有擁有protected-role角色的用戶才能訪問到受保護(hù)頁面的鏈接。 這可以通過以下呈現(xiàn)條件來完成:
<h:link value="Protected page" outcome="protected/protected" rendered="#{facesContext.externalContext.isUserInRole('protected-role')}"/>最后但并非最不重要的一點(diǎn)是,我們需要注銷功能。 對(duì)于這種情況,我們實(shí)現(xiàn)了一個(gè)簡單的后備bean,類似于以下實(shí)例,它使用戶的會(huì)話無效并將其重定向回登錄頁面:
@Named(value = "login") public class Login {public String logout() {FacesContext.getCurrentInstance().getExternalContext().invalidateSession();return "/login";} }- 像往常一樣,完整的源代碼可以在github上找到。
翻譯自: https://www.javacodegeeks.com/2014/01/securing-a-jsf-application-with-java-ee-security-and-jboss-as-7-x.html
總結(jié)
以上是生活随笔為你收集整理的使用Java EE安全性和JBoss AS 7.x保护JSF应用程序的安全的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 镜片怎么取下来 去镜片的方法
- 下一篇: 项目学生:Spring数据的持久性