[英]How to secure an included page with Spring security
假設我有一個main.jsp,其中包含另一個受保護的頁面
<%
RequestDispatcher rd = request.getRequestDispatcher("secure/protected.jsp");
rd.include(request, response);
%>
<http auto-config="true" once-per-request="true">
<intercept-url pattern="/secure/**" access="ROLE_SUPERVISOR" />
....
</http>
<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>REQUEST</dispatcher>
<dispatcher>INCLUDE</dispatcher>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
目前,我發現的是:
我已經閱讀了相關的討論: Spring Security允許未經授權的用戶從轉發訪問受限的URL
是否有可能確保包含的jsp? 如果沒有,為什么? 我猜想原因是當我們使用請求分配器時,我們仍然傳遞原始請求,因此spring安全過濾器僅知道原始請求路徑(main.jsp),而不知道目標包含路徑(protectected.jsp)。 因此,它不會阻止包含protectected.jsp
但這不起作用。 我使用Spring Security 3.1.2。
包括一個JSP基本上意味着將另一個JSP的內容內聯到當前輸出(html)文檔中。 正如sankrish所提到的,這些JSP包括不進行servlet過濾器操作。
它們的主要目的是在具有可重用的JSP時使用,例如用於導航或分頁。 通常,它們不會被servlet容器公開。 因此,他們應該住在WEB-INF
。
您沒有公開有關您的應用程序的足夠信息。 但是似乎您正在嘗試路由到一個JSP中的不同頁面。
將其視為設計缺陷。 您的視圖(JSP)應僅呈現模型數據,而您的控制器( HttpServlet
或什至更好的Spring MVC
請求處理程序)應決定是否應采用JSP A或B進行呈現。
如果我的假設有誤-很抱歉。
如果您試圖在登錄后向用戶顯示不同的內容,則應考慮實現一個定制的AuthenticationSuccessHandler
,它根據附加到principal
的角色重定向到不同的JSP。
如果只想根據用戶角色顯示或隱藏頁面內容,則應利用Spring Security's Taglib
:
在pom.xml
包含Spring Security Taglib工件(我認為Maven是理所當然的)。
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>3.1.1.RELEASE</version>
</dependency>
將taglib添加到您的JSP。
<%@ taglib prefix="security" uri="http://www.springframework.org/security/tags"%>
和環繞include
與authorize
標簽。 更好的是,您應該將authorize
移入protected.jsp
以提高可重用性。
<security:authorize ifAllGranted="ROLE_SUPERVISOR">
<jsp:include page="secure/protected.jsp" />
</security:authorize>
當您在main.jsp中include
jsp頁面時,它不會進行servlet過濾器操作。 換句話說,它不會到達服務器,而是已編譯並放置在您的主頁中。
Spring Security的概念基於ServletFilter
因為您包含的頁面不會被Spring Security攔截,它不會阻止對其的訪問。
希望這可以幫助 !!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.