gpt4 book ai didi

java - Tomcat 7 受限 (ContainerServlet) 错误

转载 作者:搜寻专家 更新时间:2023-10-31 08:12:32 26 4
gpt4 key购买 nike

我已经在这个问题上停留了一段时间。我正在尝试访问我使用 HttpClient 和 Tomcat 7 实现的 ContainerServlet。我不断收到错误消息“Restricted(ContainerServlet)”。我已经添加了基本身份验证,并尝试像内置的主机管理器应用程序一样设置它,但没有成功。

我的 HostManagerServlet 开始于:

public class HostManagerServlet
extends HttpServlet implements ContainerServlet {...

我的 web.xml 包含:

<servlet>
<servlet-name>virtualHostCreator</servlet-name>
<servlet-class>com.eatmyfish.servlets.HostManagerServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>virtualHostCreator</servlet-name>
<url-pattern>/virtualhostcreator/*</url-pattern>
</servlet-mapping>
<security-constraint>
<web-resource-collection>
<web-resource-name>HostManager commands</web-resource-name>
<url-pattern>/virtualhostcreator/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<!-- NOTE: This role is not present in the default users file -->
<role-name>admin-script</role-name>
</auth-constraint>
</security-constraint>
<security-constraint>
<web-resource-collection>
<web-resource-name>HTMLHostManager commands</web-resource-name>
<url-pattern>/virtualhostcreator/*</url-pattern>
</web-resource-collection>
<auth-constraint>
<!-- NOTE: This role is not present in the default users file -->
<role-name>admin-gui</role-name>
</auth-constraint>
</security-constraint>
<login-config>
<auth-method>BASIC</auth-method>
</login-config>
<security-role>
<description>
The role that is required to log in to the Host Manager Application HTML
interface
</description>
<role-name>admin-gui</role-name>
</security-role>
<security-role>
<description>
The role that is required to log in to the Host Manager Application text
interface
</description>
<role-name>admin-script</role-name>
</security-role>

我的 tomcat-users.xml 包含:

.
.
.
<role rolename="admin-gui"/>
<role rolename="admin-script"/>
<role rolename="manager-gui"/>
<role rolename="tomcat"/>
<role rolename="role1"/>
<user username="tomcat" password="tomcat" roles="tomcat,admin-gui,admin-script,manager-gui"/>
.
.
.

调用 HttpClient 的代码(我尝试了几种不同的方法):

方法一-

String url = environment.getProperty("baseurl");
url += "virtualhostcreator/createVirualHost?name="+group.getGroupName();
HttpClient httpclient = new DefaultHttpClient();

HttpGet httpget = new HttpGet(url);
httpget.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials("tomcat", "tomcat"),
"UTF-8", false));
HttpResponse response = httpclient.execute(httpget);

方法二 -

String url = environment.getProperty("baseurl");
url += "virtualhostcreator/createVirualHost?name="+group.getGroupName();
DefaultHttpClient httpclient = new DefaultHttpClient();
httpclient.getCredentialsProvider().setCredentials(
new AuthScope("localhost", 8080),
new UsernamePasswordCredentials("tomcat", "tomcat"));
// Create AuthCache instance
AuthCache authCache = new BasicAuthCache();
// Generate BASIC scheme object and add it to the local
// auth cache
HttpHost targetHost = new HttpHost("localhost", 8080, "http");
BasicScheme basicAuth = new BasicScheme();
authCache.put(targetHost, basicAuth);

// Add AuthCache to the execution context
BasicHttpContext localcontext = new BasicHttpContext();
localcontext.setAttribute("http.auth.auth-cache", authCache);

HttpGet httpget = new HttpGet(url);

HttpResponse response = httpclient.execute(targetHost, httpget,localcontext);

错误的堆栈跟踪:

SEVERE: Allocate exception for servlet virtualHostCreator
java.lang.SecurityException: Restricted (ContainerServlet) class com.eatmyfish.servlets.HostManagerServlet
at org.apache.catalina.core.DefaultInstanceManager.checkAccess(DefaultInstanceManager.java:536)
at org.apache.catalina.core.DefaultInstanceManager.loadClassMaybePrivileged(DefaultInstanceManager.java:509)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:124)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1136)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:857)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:581)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:98)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:927)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:407)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:987)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:579)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:307)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
at java.lang.Thread.run(Thread.java:722)

知道我做错了什么吗?我无法克服 Restricted(ContainerServlet) 错误。

最佳答案

我在 Tomcat 7 中遇到了类似的问题,并且能够通过编辑 ./conf/context.xml 中的顶级元素来解决它

<Context privileged="true">

我不明白的是 Tomcat 自己的管理器 servlet 是如何在没有这种配置的情况下加载的。有人有任何线索吗?

关于java - Tomcat 7 受限 (ContainerServlet) 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10770773/

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