我已将 Springs jsp 安全标记库添加到 freemarker 模板中,因为我使用 freemarker 而不是 jsps 来查看我的 Web 应用程序。对于任何寻找如何设置它的人,我发现添加 spring 库以使用 JSP Taglibs 以确保 Freemarker 中的安全性 https://stackoverflow.com/questions/12069602/adding-spring-library-for-usage-of-jsp-taglibs-for-security-in-freemarker这是一个非常有帮助的问题。总之,将以下内容添加到您希望使用标签的 *.ftl 文件中:
<#assign security=JspTaglibs["http://www.springframework.org/security/tags"] />
然后假设您使用 Maven,将以下内容添加到您的 pom.xml 中:
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-taglibs</artifactId>
<version>${spring.security.version}</version>
</dependency>
设置完成后,我运行了弹簧控制器单元测试,但它们都失败了。问题是他们需要 el-api.jar 和 jsp-api.jar 来解决如何呈现 Jsp 标签。它们作为 Web 应用程序运行的容器 (tomcat) 的一部分包含在内,因此应用程序的正常运行不需要它们。所以我将它们添加为测试范围内的 Maven 依赖项。
<!-- Required for spring controller junit tests, due to use of jspTagLib for security -->
<dependency>
<groupId>javax.el</groupId>
<artifactId>el-api</artifactId>
<version>2.2</version>
<scope>test</scope>
</dependency>
<!-- Required for spring controller junit tests, due to use of jspTagLib for security -->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jsp-api</artifactId>
<version>2.0</version>
<scope>test</scope>
</dependency>
修复此问题后,我的测试还引发了一个错误,即他们无法找到 .tld 文件的映射,即使在添加 spring-security-taglibs maven 依赖项时包含该文件。
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is freemarker.template.TemplateModelException: No mapping defined for http://www.springframework.org/security/tags
The failing instruction (print stack trace for 2 more):
==> #assign security = JspTaglibs["http:/... [in template "global/menu.ftl" at line 1, column 1]
为了解决这个问题,我还必须将 security.tld 添加到 WEB-INF/lib/。现在一切正常,但我需要在两个地方(META-INF 和 WEB-INF/lib 下的 spring-security-taglibs-3.1.3.RELEASE.jar)放置标签库 security.tld ,这不太好。
我的问题是,有谁知道如何避免在两个地方重复使用 security.tld 以便能够运行单元测试,我宁愿将其包含在 jar 文件中,而不是将其添加到 WEB-INF/lib ?