简体   繁体   English

Appengine-部署隐藏文件夹

[英]Appengine - Deployment of hidden folder

To verify a SSL certificate, I need to upload a hidden folder ("/.well-known" containing some files to my application. 要验证SSL证书,我需要将一个包含一些文件的隐藏文件夹(“ /.well-known”上传到我的应用程序中)。

I am deploying java application with eclipse, but these files do not receive at the application on appengine. 我正在用Eclipse部署Java应用程序,但是appengine上的应用程序未收到这些文件。 I guess they are filtered out. 我猜他们被过滤掉了。

I tried to add the hidden folder as static file to the appengine-web.xml, but it did not help. 我试图将隐藏的文件夹作为静态文件添加到appengine-web.xml,但这没有帮助。

<!-- Configure serving/caching of GWT files -->
<static-files>
    <include path="**" />
    <include path=".**" />
    <include path="**.*" expiration="0s" />
    <include path="**.well-known" expiration="0s" />
    <include path="**.nocache.*" expiration="0s" />
    <include path="**.cache.*" expiration="365d" />
    <include path="**.css" expiration="30d"/>
    <exclude path="**.gwt.rpc" />
    <exclude path="**.html" />
</static-files>

Any ideas how I could upload these folder and the files? 有什么想法可以上传这些文件夹和文件吗?

For anyone else coming here like me after trying to serve the challenge for letsencrypt in a static manner in Google App Engine and failing, the following did it for me: (one might be able to actually do it statically, but I didn't try it as I didn't want to spend more time trying out stuff and Ian apparently tried that and could not make it work [maybe the copy command done internally on Google App Engine ignores the directories that start with a dot] ) 对于尝试在Google App Engine中以静态方式为letencrypt解决挑战而失败的其他人(例如我),以下是对我有用的:(一个人可能可以静态地进行,但是我没有尝试它是因为我不想花更多的时间来尝试一些东西,而Ian显然尝试了这一点,并且无法使它起作用[也许在Google App Engine内部完成的copy命令忽略了以点开头的目录])

Taken from http://igorartamonov.com/2015/12/lets-encrypt-ssl-google-appengine/ credits go to Igor Artamonov. 摘录自http://igorartamonov.com/2015/12/lets-encrypt-ssl-google-appengine/ ,版权归Igor Artamonov所有。

Just build a servlet like: 只需构建一个像这样的servlet:

public class LetsencryptServlet extends HttpServlet { 公共类LetsencryptServlet扩展HttpServlet {

    public static final Map<String, String> challenges = new HashMap<String, String>();

    static {
        challenges.put("RzrvZ9gd7EH3i_TsJM-B0vdEMslD4oo_lwsagGskp6c",
                "RzrvZ9gd7EH3i_TsJM-B0vdEMslD4oo_lwsagGskp6c.ONrZa3UelibSWEX270nTUiRZKPFXw096nENWbMGw0-E");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        if (!req.getRequestURI().startsWith("/.well-known/acme-challenge/")) {
            resp.sendError(404);
            return;
        }
        String id = req.getRequestURI().substring("/.well-known/acme-challenge/".length());
        if (!challenges.containsKey(id)) {
            resp.sendError(404);
            return;
        }
        resp.setContentType("text/plain");
        resp.getOutputStream().print(challenges.get(id));
    }
}

And add to web.xml somethine like: 并添加到web.xml这样的东西:

<servlet>
    <servlet-name>letsencrypt</servlet-name>
    <servlet-class>...LetsencryptServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>letsencrypt</servlet-name>
    <url-pattern>/.well-known/acme-challenge/*</url-pattern>
</servlet-mapping>

Of course, be sure that the servlet class has the full classpath for your created Servlet. 当然,请确保Servlet类具有所创建Servlet的完整类路径。

That blog post also deals with the other steps necessary to generate and install the certificate. 该博客文章还介绍了生成和安装证书所需的其他步骤。

Ian: Are you sure that you were deploying the servlet well? 伊恩:确定要很好地部署servlet吗? check the logs, make sure that you are testing the right version.. maybe you had a compilation issue.. 检查日志,确保您正在测试正确的版本..也许您遇到了编译问题..

Cheers 干杯

I ran into this problem trying to serve an assetlinks.json file. 我在尝试提供assetlinks.json文件时遇到了这个问题。 It would indeed appear that folders starting with a . 确实确实出现了以。开头的文件夹。 are not accessible within the static context in App Engine. 在App Engine的静态上下文中不可访问。 A more generic version of João Antunes workaround is as follows. JoãoAntunes解决方法的更通用版本如下。

First, create the folder without the . 首先,创建不带的文件夹。 at the start and place any required files inside it. 在开始时,将所有需要的文件放入其中。

We then need to create a servlet that will respond with the correct data when a request to the hidden folder is received. 然后,我们需要创建一个servlet,当收到对隐藏文件夹的请求时,它将使用正确的数据进行响应。

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by Will Calderwood on 17/05/2017.
 * <p>
 * It would appear to not be possible to upload hidden folders to app engine. So when files need
 * to be served from a hidden folder the URL can be bounced through this servlet
 */
public class StaticFileServer extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        // We'll remove the dots from the path
        String uri = req.getRequestURI().replace("/.", "/");

        // Do anything else that needs doing here
        if (uri.toLowerCase().contains(".json")) {
            resp.setContentType("application/json");
        }

        // Read and return the resource from the non-hidden folder
        try (InputStream in = getServletContext().getResourceAsStream(uri)) {
            if (in == null){
                resp.sendError(404);
                return;
            }
            byte[] buffer = new byte[8192];
            int count;
            while ((count = in.read(buffer)) > 0) {
                resp.getOutputStream().write(buffer, 0, count);
            }
        }
    }
}

Then add the following to your web.xml file to point the hidden folder at our servlet 然后将以下内容添加到您的web.xml文件中,以将隐藏的文件夹指向我们的servlet。

<servlet>
    <servlet-name>StaticFileServer</servlet-name>
    <servlet-class>main.com.you.StaticFileServer</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>StaticFileServer</servlet-name>
    <url-pattern>/.well-known/*</url-pattern>
</servlet-mapping>

Static files are not uploaded to the application servers and thus are not accessible by app code. 静态文件不会上载到应用程序服务器,因此无法通过应用程序代码访问。 From appengine-web.xml Reference , in the <static-files> section: appengine-web.xml参考中的<static-files>部分中:

< static-files > <静态文件>

Optional. 可选的。 The < static-files > element specifies patterns that match file paths to include and exclude from the list of static files, overriding or amending the default behavior. <static-files>元素指定与文件路径相匹配的模式,以包括或排除静态文件列表,覆盖或修改默认行为。 Static file are served from dedicated servers and caches that are separate from the application servers and are useful for serving static content such as images, CSS stylesheets or JavaScript files. 静态文件由与应用程序服务器不同的专用服务器和缓存提供,对于提供静态内容(例如图像,CSS样式表或JavaScript文件)很有用。

To have files accessible by app code they need to be specified in the <resource-files> section (same doc as above): 要使文件可通过应用程序代码访问,需要在<resource-files>部分(与上述相同的文档)中指定它们:

< resource-files > <资源文件>

Optional. 可选的。 The files that listed in the < resource-files > element are accessible by the application code using the filesystem. 应用程序代码可以使用文件系统访问<resource-files>元素中列出的文件。 These files are stored on the application servers with the app as opposed to how static files are stored and served. 这些文件与应用程序一起存储在应用程序服务器上,与静态文件的存储和提供方式相反。

I tried everything, but could not manage to upload that hidden folder. 我尝试了所有操作,但是无法上传该隐藏文件夹。 I then defined a servlet mapping for that path an the servlet writes out the verification string expected bei the SSL issuer: 然后,我为该路径定义了一个Servlet映射,该Servlet会写出SSL发行方期望的验证字符串:

<servlet-mapping>
    <servlet-name>SSLVerificationServlet</servlet-name>
    <url-pattern>/.well-known/acme-challenge/roQH_vUmRQsuKH8lZGedft9O6TK-UoZWzv_kY8MukH4F8</url-pattern>
</servlet-mapping> 

我确实找到了一种更好的方法,问题是代码存储在隐藏目录中,因此我们只需要将正确的url映射到可见目录即可。

handlers: - url: /.well-known static_dir: _well-known

An ugly, but straightforward workaround is to use JSP support to map the page. 一个丑陋但简单的解决方法是使用JSP支持来映射页面。 One example: 一个例子:

Create your json file in package/src/main/webapp/_well-known/assetlinks.json.jsp package/src/main/webapp/_well-known/assetlinks.json.jsp创建您的json文件

<%@ page contentType="application/json;charset=UTF-8" language="java"%>
[JSON CONTENT]

Map the "servlet" to the desired path in web.xml : 将“ servlet”映射到web.xml的所需路径:

<servlet>
    <servlet-name>Asset Links</servlet-name>
    <jsp-file>/_well-known/assetlinks.json.jsp</jsp-file>
</servlet>
<servlet-mapping>
    <servlet-name>Asset Links</servlet-name>
    <url-pattern>/.well-known/assetlinks.json</url-pattern>
</servlet-mapping>

Done! 做完了!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM