Leaflet中的数据存储在哪里
P粉883973481
P粉883973481 2024-02-25 20:25:54
0
1
410

我有一个使用普通 Leaflet、HTML、CSS 和 JS 启动并运行的传单应用程序。目前,它仅位于一个文件夹中,其中包含一个 index.html 文件、一个 main.js 文件和一个包含我所有数据的文件夹。数据是geojson数据。我最终希望当我将其在线时,用户无法访问我正在使用的数据。我正在寻找关于我应该如何解决这个问题的大局、大纲。

我正在考虑使用 MongoDB 之类的东西来存储我的数据,但网上没有关于如何使用 Leaflet 精确做到这一点的先例或教程。它似乎还需要对我的代码进行大量更改,并尝试将express和节点(?)添加到我的应用程序中。任何想法或示例链接将不胜感激。

P粉883973481
P粉883973481

全部回复(1)
P粉505917590

您对 MongoDB 的思考方向是错误的,因为这不会以任何额外的方式保护您的数据。

不可能提供纯 HTML 网页,而是隐藏数据 - 因为任何人都可以在 HTML 源代码中查找访问权限。

您需要的是支持 OAuth 的第三方提供商,例如:

他们会向您颁发 JWT,如下面的华为文档所示,然后在服务器端您需要验证令牌并决定是否提供数据。

即使这样,授权客户也可以获取并分发您的数据。

我知道这些东西,因为作为一名业余开发者,我编写了 2 个网页游戏,并且我正在使用这 4 个服务(还有更多)来验证用户。

这是我的服务器端 Java 代码示例,用于验证华为 Account Kit:

private void handleHuaweiAuth(HttpServletRequest httpReq, HttpServletResponse httpResp) throws ServletException, IOException {
    String error            = httpReq.getParameter("error");
    String errorDescription = httpReq.getParameter("error_description");
    String code             = httpReq.getParameter("code");
    String state            = httpReq.getParameter("state");

    // use hash of salt and current month name as CSRF protection
    String month = md5("PUT SOME SECRET HERE" + Calendar.getInstance().getDisplayName(Calendar.MONTH, Calendar.LONG, Locale.ENGLISH));

    if (error != null) {
        throw new ServletException(error + ": " + errorDescription);
        
    } else if (code != null && month.equals(state)) {
        MultiMap postParams = new MultiMap();
        postParams.put("code",          code);
        postParams.put("client_id",     HUAWEI_ID);
        postParams.put("client_secret", HUAWEI_SECRET);
        postParams.put("redirect_uri",  String.format(HUAWEI_REDIRECT_URI, mLanguage));
        postParams.put("grant_type",    "authorization_code");
        
        try {
            String tokenStr = mHttpClient.POST(HUAWEI_TOKEN_URL)
                .headers(httpFields ->
                {
                    httpFields.add(new HttpField(HttpHeader.ACCEPT, APPLICATION_JSON));
                    httpFields.add(new HttpField(HttpHeader.CONTENT_TYPE, APPLICATION_URLENCODED));
                })
                .body(new StringRequestContent(UrlEncoded.encode(postParams, StandardCharsets.UTF_8, false)))
                .send().getContentAsString();
            LOG.info("handleHuaweiAuth tokenStr = {}", tokenStr);
            Map tokenMap = (Map) new JSON().fromJSON(tokenStr);
            //String accessToken = tokenMap.get("access_token");
            //String refreshToken = tokenMap.get("refresh_token");

            // NOTE: the code is only valid for 1 usage.
            // If the user reloads this page, then the following will be returned:
            // {"sub_error":20156,"error_description":"code used twice","error":1101}
            // parsing token will result in NPE caught below and redirect to front page

            String idToken = tokenMap.get("id_token");
            Map idMap = parseJwt(idToken);
            String sid    = idMap.get("sub"); 
            String photo  = idMap.get("picture");
            String given  = idMap.get("given_name");
            String family = idMap.get("family_name");

            printGameApp(httpReq, httpResp, HUAWEI, sid, given, family, photo);
            return;
        } catch (InterruptedException | TimeoutException | ExecutionException | NullPointerException ex) {
            LOG.warn("handleHuaweiAuth", ex);
            // redirect to the front page
            httpResp.sendRedirect("/");
            return;
        }
    }
}
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!