<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                # 使用 JSP Taglibs 的 Spring 視圖層安全 > 原文: [https://howtodoinjava.com/spring-security/spring-security-at-view-layer-using-jsp-taglibs/](https://howtodoinjava.com/spring-security/spring-security-at-view-layer-using-jsp-taglibs/) 到目前為止,在先前的教程中,我們已經了解了如何在[登錄表單](https://howtodoinjava.com/spring/spring-security/login-form-based-spring-3-security-example/),[自定義用戶詳細信息服務](https://howtodoinjava.com/spring/spring-security/custom-userdetailsservice-example-for-spring-3-security/)甚至[方法級安全性](https://howtodoinjava.com/spring/spring-security/spring-3-method-level-security-example-using-preauthorize-and-secured/)之后保護您的應用程序的安全。 所有這些安全性實現都在 MVC 的控制器或模型層上。 是時候在視圖層中添加安全性了。 當我們要根據用戶的角色隱藏某些鏈接或按鈕以使他將無法訪問該功能時,通常需要使用該功能。 ## **Taglib 聲明** 為了確保以后查看應用程序的安全,spring Security 擁有自己的 taglib,它為訪問安全信息和在 JSP 中應用安全約束提供了基本支持。 要在 jsp 文件中使用安全功能,需要添加以下標記庫聲明: ```java <%@ taglib uri="http://www.springframework.org/security/tags" prefix="security" %> ``` 您可以選擇自己的前綴,并且需要在 jsp 文件中使用該前綴。 ## **安全標簽** Spring 基本上提供了 3 個標簽來保護視圖層信息,即 * 授權標簽 * 驗證標簽 * `accesscontrollist`標簽 讓我們一一看。 **1)授權標簽:** 該標簽用于確定是否應評估其內容。 此標記具有兩種形式,即根據用戶角色保護信息或根據用戶訪問特定 URL 的權限保護信息。 用法示例如下所示: ```java <security:authorize ifAnyGranted="ROLE_ADMIN"> <tr> <td colspan="2"> <input type="submit" value="<spring:message code="label.add"/>"/> </td> </tr> </security:authorize> OR <security:authorize url="/admin"> <tr> <td colspan="2"> <input type="submit" value="<spring:message code="label.add"/>"/> </td> </tr> </security:authorize> ``` **2)驗證標簽** 此標記允許訪問存儲在安全上下文中的當前身份驗證對象。 它直接在 JSP 中呈現對象的屬性。 因此,例如,如果`Authentication`的主體屬性是 Spring Security 的`UserDetails`對象的實例,則使用 `<sec:authentication property="principal.username"></sec:authentication>`將呈現當前用戶的名稱。 此標記不是直接出于安全目的,而是可用于訪問可用于視圖層安全性的信息。 ```java <security:authentication property="principal.username" /> ``` **3)`accesscontrollist`標記** 該標簽僅在與 Spring Security 的 ACL 模塊一起使用時才有效。 它檢查以逗號分隔的指定域對象的所需權限列表。 如果當前用戶具有這些權限中的任何一個,則將評估標簽正文。 如果他們不這樣做,它將被跳過。 ```java <sec:accesscontrollist hasPermission="1,2" domainObject="someObject"> This will be shown if the user has either of the permissions represented by the values "1" or "2" on the given object. </sec:accesscontrollist> ``` 來源:[http://static.springsource.org/spring-security/site/docs/3.0.x/reference/taglibs.html](http://static.springsource.org/spring-security/site/docs/3.0.x/reference/taglibs.html) ## **應用程序**中的使用示例 我們已經在 [**Spring Security 登錄表單教程**](https://howtodoinjava.com/spring/spring-security/login-form-based-spring-3-security-example/)的基礎上開發了一個員工管理應用程序。 在此應用程序中,經過身份驗證的用戶可以添加/刪除/列出員工。 現在,讓我們修改應用程序,以便任何沒有`ROLE_ADMIN`權限的用戶都看不到“添加”按鈕。 以下是我在應用程序中所做的代碼更改: ```java < %@ taglib uri="http://www.springframework.org/tags" prefix="spring" %> < %@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> < %@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> < %@ taglib uri="http://www.springframework.org/security/tags" prefix="security" %> <html> <head> <title>Spring 3 hibernate integration example on www.howtodoinjava.com</title> </head> <body> <h2>Employee Management Screen : <security:authentication property="principal.username"></security:authentication></h2> <h6><a href="<c:url value='j_spring_security_logout'></a>">Click here to logout</h6> <form:form method="post" action="add" commandName="employee"> <table> <tr> <td><form:label path="firstname"><spring:message code="label.firstname"></spring:message></form:label></td> <td><form:input path="firstname"></form:input></td> </tr> <tr> <td><form:label path="lastname"><spring:message code="label.lastname"></spring:message></form:label></td> <td><form:input path="lastname"></form:input></td> </tr> <tr> <td><form:label path="email"><spring:message code="label.email"></spring:message></form:label></td> <td><form:input path="email"></form:input></td> </tr> <tr> <td><form:label path="telephone"><spring:message code="label.telephone"></spring:message></form:label></td> <td><form:input path="telephone"></form:input></td> </tr> <security:authorize ifAnyGranted="ROLE_ADMIN"> <tr> <td colspan="2"> <input type="submit" value="<spring:message code="label.add"/>"/> </td> </tr> </security:authorize> </table> </form:form> <h3>Employees</h3> <c:if test="${!empty employeeList}"> <table class="data"> <tr> <th>Name</th> <th>Email</th> <th>Telephone</th> <th></th> </tr> <c:foreach items="${employeeList}" var="emp"> <tr> <td>${emp.lastname}, ${emp.firstname} </td> <td>${emp.email}</td> <td>${emp.telephone}</td> <td>delete</td> </tr> </c:foreach> </table> </c:if> </body> </html> ``` 以下是上述瀏覽器屏幕更改的結果。 ![Employee management screen](https://img.kancloud.cn/06/e9/06e9babbc586b97be3f515200825fad0_346x361.jpg) 員工管理界面 應用安全標簽后,“添加”按鈕將消失,并在屏幕上顯示登錄的用戶名“ lokesh”。 ![Spring jsp tags demo](https://img.kancloud.cn/a9/1c/a91cdcc73c4240a89e390f04a6b78119_459x333.jpg) Screen after applying security tags in JSP 讓我知道是否仍然不清楚。 **祝您學習愉快!**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看