gpt4 book ai didi

java - 新组件的 ReSTLet web.xml 配置

转载 作者:太空宇宙 更新时间:2023-11-04 06:30:43 29 4
gpt4 key购买 nike

我已经建立了一个 App Engine ReSTLet 项目(v 2.2.2),它为不同的用户返回 html 或 json 响应(Web 或 Android 客户端)以及来自数据库的不同数据。我已经实现了 HTTP 基本身份验证。一切都运行得很好。

我的基本设置atm(我已经简化了):

MyApplication.java

public class MyApplication extends Application {

private ChallengeAuthenticator authenticatior;
private ChallengeAuthenticator createAuthenticator() {...}
public boolean authenticate(Request request, Response response) {...}

@Override
public Restlet createInboundRoot() {
this.authenticatior = createAuthenticator();
Router router = new Router(getContext());
router.attachDefault(MyRestlet.class);
authenticatior.setNext(router);
return authenticatior;
}

MyReSTLet.java

public class MyRestlet extends ServerResource {

@Get("json")
public Representation getJSON() {
MyApplication app = (MyApplication) getApplication();
if (!app.authenticate(getRequest(), getResponse())) {
// Not authenticated
return null;
}
else {
return data;
}

@Get("html")
public String getHTML() {...}
}

web.xml

<?xml ...>
<display-name>MyName</display-name>

<context-param>
<param-name>org.restlet.application</param-name>
<param-value>x.MyApplication</param-value>
</context-param>

<servlet>
<servlet-name>MyRestlet</servlet-name>
<servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class>
</servlet>

<servlet-mapping>
<servlet-name>MyRestlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

现在我想添加加密并创建 key /证书。我的指南是这个tutorial 。我想首先尝试添加一个简单的组件,保留 HTTP 并将端口更改为 8183,如此 tutorial 中所做的那样。 。我现在已经玩了很多了,但似乎无法让我的组件工作。所以我的问题是:我应该把这个 main-Method 放在哪里(以下代码取自教程)?我应该将其插入到哪个类中,还是应该创建一个单独的服务器类,以及对 web.xml 所需的更改究竟是什么样的(我没有找到太多与此相关的内容,我怀疑这是核心问题)?如有任何帮助,我们将不胜感激!

public static void main(String[] args) throws Exception {
// Create a new Restlet component and add a HTTP server connector to it
Component component = new Component();
component.getServers().add(Protocol.HTTP, 8182);

// Then attach it to the local host
component.getDefaultHost().attach("/trace", Part05.class);

// Now, let's start the component!
// Note that the HTTP server connector is also automatically started.
component.start();
}

最佳答案

您的问题有点令人困惑,可能是因为您所采取的路径所致。

您最初的计划创建一个 RESTlet Web 应用程序,并包含 web.xml 以在容器(例如 GAE、Jetty 或 Tomcat)内运行。

您建议创建一个组件并启动一个独立服务器(它启动 Jetty 的内部实例)。这不适用于 Google App Engine。

您应该继续使用 web.xml 方法并将 GAE 配置为通过 https 提供服务。请参阅https://cloud.google.com/appengine/kb/general#httpshttps://cloud.google.com/appengine/docs/java/config/webxml#Secure_URLs获取配置说明。

此外,另一个提示:当尝试未经授权访问资源时,返回 403 状态。

关于java - 新组件的 ReSTLet web.xml 配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26144271/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com