Google App Engine FreeMarker模板java.security.AccessControlException

时间:2016-05-31 07:09:00

标签: java google-app-engine freemarker

我正在使用FreeMarker和Google App-Engine Java。

web.xml

<servlet>
        <servlet-name>WebBookEditor</servlet-name>
        <servlet-class>com.business.cloud.servlets.WebBookEditor</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>WebBookEditor</servlet-name>
        <url-pattern>/editor/*</url-pattern>
    </servlet-mapping>

模板路径:/webapp/WEB-INF/templates/book_editor.ftlh

加载模板的代码

Template temp = cfg.getTemplate("/book_editor.ftlh”);

配置方法

private static void instantiateConfiguration() throws IOException {
    if (null == cfg) {
        cfg = new Configuration();
        cfg.setDirectoryForTemplateLoading(new File("/templates"));
        cfg.setDefaultEncoding("UTF-8");
        cfg.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
    }
}

有人请帮助我确定我遗失或做错了什么吗?我收到以下异常

Uncaught exception from servlet
java.security.AccessControlException: access denied ("java.io.FilePermission" "/templates" "read")
    at com.google.appengine.runtime.Request.process-047aec732318c54c(Request.java)
    at java.security.AccessControlContext.checkPermission(AccessControlContext.java:484)
    at java.security.AccessController.checkPermission(AccessController.java:698)
    at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
    at java.lang.SecurityManager.checkRead(SecurityManager.java:888)
    at java.io.File.exists(File.java:877)
    at freemarker.cache.FileTemplateLoader$1.run(FileTemplateLoader.java:124)
    at java.security.AccessController.doPrivileged(AccessController.java:62)
    at freemarker.cache.FileTemplateLoader.<init>(FileTemplateLoader.java:122)
    at freemarker.cache.FileTemplateLoader.<init>(FileTemplateLoader.java:108)
    at freemarker.template.Configuration.setDirectoryForTemplateLoading(Configuration.java:314)
    at com.business.cloud.servlets.WebBookEditor.instantiateConfiguration(WebBookEditor.java:62)
    at com.business.cloud.servlets.WebBookEditor.doGet(WebBookEditor.java:33)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1166)
    at com.googlecode.objectify.ObjectifyFilter.doFilter(ObjectifyFilter.java:48)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1157)
    at org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:388)
    at org.mortbay.jetty.security.SecurityHandler.handle(SecurityHandler.java:216)
    at org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)
    at org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)
    at org.mortbay.jetty.webapp.WebAppContext.handle(WebAppContext.java:418)
    at org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)
    at org.mortbay.jetty.Server.handle(Server.java:326)
    at org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)
    at org.mortbay.jetty.HttpConnection$RequestHandler.headerComplete(HttpConnection.java:923)
    at org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)
    at com.google.tracing.TraceContext$TraceContextRunnable.runInContext(TraceContext.java:439)
    at com.google.tracing.TraceContext$TraceContextRunnable$1.run(TraceContext.java:446)
    at com.google.tracing.CurrentContext.runInContext(CurrentContext.java:256)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContextNoUnref(TraceContext.java:310)
    at com.google.tracing.TraceContext$AbstractTraceContextCallback.runInInheritedContext(TraceContext.java:302)
    at com.google.tracing.TraceContext$TraceContextRunnable.run(TraceContext.java:443)
    at java.lang.Thread.run(Thread.java:745)

3 个答案:

答案 0 :(得分:0)

您可以使用cfg.setClassForTemplateLoading(classpath,&#39; /&#39;)进行尝试。 请尝试以下链接Setting freemarker template from classpath。如果要将模板文件放在jar文件中,这可能会有效。

答案 1 :(得分:0)

该例外表示您没有/templates的读取权限。

实际上,/templates下的模板文件不是,它们存在于getClass().getResource("/").getPath() + "templates"中。所以代码应该是:

cfg.setDirectoryForTemplateLoading(new File(getClass().getResource("/").getPath() + "templates"));

但我建议您使用Configuration提供的另一种方法:

cfg.setClassForTemplateLoading(App.class, "/templates");

答案 2 :(得分:0)

实际上我在https://code.google.com/p/googleappengine/issues/detail?id=11766找到了问题的解决方案。显然这是一个已知的错误,谷歌正在努力解决它。我希望它能解决我的问题。