2016-05-31 149 views
0

我正在使用FreeMarker和Google App-Engine Java。Google App Engine FreeMarker模板java.security.AccessControlException

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); 
    } 
} 

威爾小號omeone請幫我確定我失蹤或做錯了什麼?我收到以下例外

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) 
+0

我還沒有收到谷歌的回信,我讀了http://stackoverflow.com/questions/14209175/java-security-accesscontrolexception-access-denied-java-io-filepermission。所以我嘗試了下面的建議,但沒有運氣。我的'web.xml'或'webapp'路徑中是否仍然有可能丟失? –

回答

0

例外意味着您沒有對/templates的讀取權限。

其實你的模板文件是而不是/templates下,它們存在於getClass().getResource("/").getPath() + "templates"。因此,代碼應該是:

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

但我會建議你使用的配置提供了另一種方法:

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