我的应用程序部署在一个共享的 Tomcat 托管环境中,我的 appBase 位于容器之外,并且严格维护了安全策略。我现在正在尝试引入 Freemarker 模板,但我得到了这个:
Could not create a file template loader for current directory
java.security.AccessControlException: access denied (java.io.FilePermission C:\apache-tomcat\bin read)
at java.security.AccessControlContext.checkPermission(AccessControlContext.java:374)
at java.security.AccessController.checkPermission(AccessController.java:546)
at java.lang.SecurityManager.checkPermission(SecurityManager.java:532)
at java.lang.SecurityManager.checkRead(SecurityManager.java:871)
at java.io.File.exists(File.java:731)
at freemarker.cache.FileTemplateLoader$1.run(FileTemplateLoader.java:124)
at java.security.AccessController.doPrivileged(Native Method)
at freemarker.cache.FileTemplateLoader.<init>(FileTemplateLoader.java:122)
at freemarker.cache.FileTemplateLoader.<init>(FileTemplateLoader.java:108)
at freemarker.cache.FileTemplateLoader.<init>(FileTemplateLoader.java:94)
at freemarker.cache.TemplateCache.createDefaultTemplateLoader(TemplateCache.java:120)
at freemarker.cache.TemplateCache.<init>(TemplateCache.java:115)
at freemarker.template.Configuration.<init>(Configuration.java:110)
at freemarker.template.Configuration.<clinit>(Configuration.java:96)
at org.springframework.ui.freemarker.FreeMarkerConfigurationFactory.newConfiguration(FreeMarkerConfigurationFactory.java:333)
at org.springframework.ui.freemarker.FreeMarkerConfigurationFactory.createConfiguration(FreeMarkerConfigurationFactory.java:266)
at org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean.afterPropertiesSet(FreeMarkerConfigurationFactoryBean.java:6
0)
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory$6.run(AbstractAutowireCapableBeanFactory.java:1504)
尽管我相信 Freemarker 的安全策略建议已被遵循。
使用 Spring 注入配置:
<bean id="freemarkerMailConfiguration"
class="org.springframework.ui.freemarker.FreeMarkerConfigurationFactoryBean">
<property name="templateLoaderPath" value="file:/C:/apache-tomcat-files/mailtemplates/"/>
</bean>
或手动创建它
Configuration freemarkerMailConfiguration = new Configuration();
File dir = new File("C:/apache-tomcat-files/mailtemplates");
freemarkerMailConfiguration.setDirectoryForTemplateLoading(dir);
该异常似乎源于 Configuration 的无参数构造函数,它可能会在您更改它之前尝试从默认位置加载 - 使用 setDirectoryForTemplateLoading() 为时已晚。
谁能告诉我当默认值超出范围时我是否可以成功设置目录?
非常感谢,尼尔