HTTP代理Servlet错误

时间:2015-05-20 19:54:25

标签: java maven tomcat servlets proxy

首先我要说这是在昨天工作,所以我完全不知道为什么它突然停止工作。而Java /插件并不是我的专长。

我正在开发一个应用程序,它对另一个域上的服务器进行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>

的web.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)

注意完整堆栈跟踪Apache Tomcat / 7.0.39日志中提供了根本原因。

Apache Tomcat / 7.0.39

所以简短的问题是,为什么发送到HttpProxy插件的用户名param没有被截获并正确替换?

很抱歉,如果这可以提供很多信息。希望熟悉这些技术的人能够快速识别问题或指出正确的调试路径。

谢天谢地!

修改

的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中的模板

再次感谢!

2 个答案:

答案 0 :(得分:0)

主要是servlet映射存在问题。 根据{{​​3}},我没有看到类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;
...

所以我正在为URITemplateProxyServlet阅读documentation,我认为可能期待一对一的args匹配。换句话说,对于targetUri中的每个参数,查询字符串中必须有匹配的参数(即ajax生成的URL)。不幸的是我无法测试,但你应该能够迅速告诉我这是否是事实。

试试这个:

  • 为了避免混淆,请将网址格式设置为第二个“/”<url-pattern>/userProfile/*</url-pattern>,就像the source一样。
  • 更改ajax生成的网址以匹配此模式 AND 具有第二个参数:"userProfile/subpath?_username=" + username + "&_username2=" + username;
  • 根据需要更改目标uri:<param-value>${authserver}/aaenroll/rest/userProfile/userName/{_username}.json?user={_username2}</param-value>

答案 1 :(得分:0)

好吧,我终于找到了问题所在。由于我试图使用相同的_username参数填充我的targetUri中的两个点,其中一个迷路了。基本上你不能使用这个插件将相同的参数两次应用到targetUri。

解决方案: 的web.xml

<servlet>
    <servlet-name>user_profile</servlet-name>
    <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={_username2}</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>

ajax call

$.ajax({
    type: "GET",
    beforeSend: function(request)
    {
        request.setRequestHeader("Authorization", authToken);
    },
    url: "userProfile?_username=" + username + "&_username2=" + username;, //Url intercepted by servlet plugin. Full path mapping is available in web.xml
    success: function(msg, success) {
    },
    error: function(xhr, ajaxOptions, thrownError) {
    }
});