gpt4 book ai didi

java - HTTP 代理 Servlet 错误

转载 作者:行者123 更新时间:2023-11-28 22:02:18 24 4
gpt4 key购买 nike

首先让我说这是昨天的工作,所以我完全不明白为什么它突然停止工作。 Java/Plugins 真的不是我的专长。

我正在开发一个应用程序,它可以对另一个域中的服务器进行 POST 和 GET 调用。这是一个 Maven 项目。为了避免跨源问题,我有一个 httpproxy.ProxyServlet 插件。

我将在这里列出所有的工作部分,但基本上问题和错误都总结在这篇文章的底部。

pom.xml

<dependencies>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>servlet-api</artifactId>
<version>2.5</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet.jsp</groupId>
<artifactId>jsp-api</artifactId>
<version>2.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.mitre.dsmiley.httpproxy</groupId>
<artifactId>smiley-http-proxy-servlet</artifactId>
<version>1.6</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.0.2</version>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
<!-- War plugin below is required for above plugin to build project without errors. http://stackoverflow.com/questions/7539970/cannot-construct-org-apache-maven-plugin-war-util-webappstructure-as-it-does-not-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-war-plugin</artifactId>
<version>2.1.1</version>
</plugin>
</plugins>

网络.xml

第二个 servlet 部分是有问题的部分。我只发布第一个,因为第一个工作正常。供引用。

<servlet>
<servlet-name>ad_auth</servlet-name>
<servlet-class>org.mitre.dsmiley.httpproxy.ProxyServlet</servlet-class>
<init-param>
<param-name>targetUri</param-name>
<param-value>${authserver}/aaenroll/rest/authContext</param-value>
</init-param>
<init-param>
<param-name>log</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>ad_auth</servlet-name>
<url-pattern>/adauth</url-pattern>
</servlet-mapping>


<servlet>
<servlet-name>user_profile</servlet-name>
<servlet-class>org.mitre.dsmiley.httpproxy.ProxyServlet</servlet-class>
<init-param>
<param-name>targetUri</param-name>
<param-value>${authserver}/aaenroll/rest/userProfile/userName/{_username}.json?user={_username}</param-value>
<!--<param-value>${authserver}/aaenroll/rest/userProfile/userName/segotac.json?user=segotac</param-value>-->
</init-param>
<init-param>
<param-name>log</param-name>
<param-value>true</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>user_profile</servlet-name>
<url-pattern>/userProfile</url-pattern>
</servlet-mapping>

Javascript Ajax 调用

function checkEnrollment() {
var username = Cookies.get('username');
var authToken = Cookies.get('authToken');
$.ajax({
type: "GET",
beforeSend: function(request)
{
request.setRequestHeader("Authorization", authToken);
},
url: "userProfile?_username=" + username, //**PROBLEM HERE** -> Url intercepted by servlet plugin. Full path mapping is available in web.xml. As per the Plugin syntax, param _username will replace all "{_username}" in web.xml userProfile targetUri param-values.
success: function(msg, success) {
window.location.replace("enroll.jsp");
},
error: function(xhr, ajaxOptions, thrownError) {
console.log('false Enrollment');
}
});

上面传递的拦截 URL 的语法位于此链接的底部。哪个是插件。这又是……昨天还在工作……https://github.com/mitre/HTTP-Proxy-Servlet

最后。错误:

<html><head><title>Apache Tomcat/7.0.39 - Error report</title><style><!--H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}HR {color : #525D76;}--></style> </head><body><h1>HTTP Status 500 - Trying to process targetUri init parameter: java.net.URISyntaxException: Illegal character in path at index 71: http://cslxintwebdev3.csmc.edu:8087/aaenroll/rest/userProfile/userName/{_username}.json?user={_username}</h1><HR size="1" noshade="noshade"><p><b>type</b> Exception report</p><p><b>message</b> <u>Trying to process targetUri init parameter: java.net.URISyntaxException: Illegal character in path at index 71: http://cslxintwebdev3.csmc.edu:8087/aaenroll/rest/userProfile/userName/{_username}.json?user={_username}</u></p><p><b>description</b> <u>The server encountered an internal error that prevented it from fulfilling this request.</u></p><p><b>exception</b> <pre>javax.servlet.ServletException: Trying to process targetUri init parameter: java.net.URISyntaxException: Illegal character in path at index 71: http://cslxintwebdev3.csmc.edu:8087/aaenroll/rest/userProfile/userName/{_username}.json?user={_username}
org.mitre.dsmiley.httpproxy.ProxyServlet.initTarget(ProxyServlet.java:156)
org.mitre.dsmiley.httpproxy.ProxyServlet.init(ProxyServlet.java:140)
javax.servlet.GenericServlet.init(GenericServlet.java:160)
org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)
org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)
org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)
org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)
org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)
org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
java.lang.Thread.run(Thread.java:722)

根本原因

java.net.URISyntaxException: Illegal character in path at index 71: http://example.com:8087/aaenroll/rest/userProfile/userName/{_username}.json?user={_username}    java.net.URI$Parser.fail(URI.java:2829)    java.net.URI$Parser.checkChars(URI.java:3002)    java.net.URI$Parser.parseHierarchical(URI.java:3086)    java.net.URI$Parser.parse(URI.java:3034)    java.net.URI.<init>(URI.java:595)    org.mitre.dsmiley.httpproxy.ProxyServlet.initTarget(ProxyServlet.java:154)    org.mitre.dsmiley.httpproxy.ProxyServlet.init(ProxyServlet.java:140)    javax.servlet.GenericServlet.init(GenericServlet.java:160)    org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)    org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:99)    org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:947)    org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:408)    org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1009)    org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:589)    org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:312)    java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)    java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)    java.lang.Thread.run(Thread.java:722)

note The full stack trace of the root cause is available in the Apache Tomcat/7.0.39 logs.

Apache Tomcat/7.0.39

So the short question is, why is the username param being sent to the HttpProxy plugin not being intercepted and replaced correctly?

Sorry if this is way to much info to throw at this. Hopefully someone who is familiar with these technologies will be able to quickly identify the issue or point me on the correct debugging path.

THANK ALOOOT!

Edit

web.xml

<servlet-class>org.mitre.dsmiley.httpproxy.URITemplateProxyServlet</servlet-class>
<url-pattern>/userProfile/*</url-pattern>

js

url: "userProfile?_username=" + username,

错误:

HTTP Status 500 - Missing HTTP parameter _username to fill the template

我知道用户名在 javascript 调用中有一个值。变量未正确应用于 web.xml 中的模板

再次感谢!

最佳答案

主要是你的servlet映射有问题。根据 documentation ,我没有看到任何迹象表明 class ProxyServlet允许参数化(尽管可能会)。也许你应该使用类 URITemplateProxyServlet反而。您可以这样更改您的 servlet 定义:

<servlet-class>org.mitre.dsmiley.httpproxy.URITemplateProxyServlet</servlet-class>
<init-param>
<param-name>targetUri</param-name>
<param-value>{authserver}/aaenroll/rest/userProfile/userName/{_username}.json?user={_username}</param-value>
...

和这样的 servlet 映射(注意 '*'):

<servlet-mapping>
<servlet-name>user_profile</servlet-name>
<url-pattern>/userProfile/*</url-pattern>
</servlet-mapping>

然后在你的ajax中:

...
url: "userProfile/?authserver=" + authserver + "&_username=" + username;
...

所以我在读the source对于 URITemplateProxyServlet我认为它可能期待一对一的参数匹配。换句话说,对于 targetUri 中的每个参数,查询字符串中必须有一个匹配的参数(即您的 ajax 生成的 URL)。遗憾的是我无法测试,但您应该能够快速判断这是否是事实。

试试这个:

  • 为了避免混淆,将 url-pattern 设置为第二个 '/' <url-pattern>/userProfile/*</url-pattern>正如他们的 documentation
  • 更改 ajax 生成的 URL 以匹配此模式并且有第二个参数:"userProfile/subpath?_username=" + username + "&_username2=" + username;
  • 根据需要更改目标 uri:<param-value>${authserver}/aaenroll/rest/userProfile/userName/{_username}.json?user={_username2}</param-value>

关于java - HTTP 代理 Servlet 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30359192/

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