【问题标题】:java web service jax-ws client authentication Tomcat not workingjava web服务jax-ws客户端身份验证Tomcat不工作
【发布时间】:2017-03-02 01:50:58
【问题描述】:

我有以下客户端使用在 tomcat(本地主机)上运行的 Web 服务

客户

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.xml.ws.BindingProvider;
import javax.xml.ws.handler.MessageContext;

public class NewClass {
    static {
        //for localhost testing only
        javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
        new javax.net.ssl.HostnameVerifier(){
            public boolean verify(String hostname,
                    javax.net.ssl.SSLSession sslSession) {
                if (hostname.equals("localhost")) {
                    return true;
                }
                return false;
            }
        });
    }
     public static void main(String[] args) {

         System.out.println(receive(5));

    }
    private static java.util.List<java.lang.Object> receive(int resNumber) {

       System.setProperty("javax.net.ssl.trustStore","c:\\mylocalhost.jks"); //path to jks
       System.setProperty("javax.net.ssl.trustStorePassword","****"); //path to jks
       System.setProperty("javax.net.ssl.keyStore","c:\\myclient.jks"); //path to jks
       System.setProperty("javax.net.ssl.keyStorePassword","*****"); //path to jks
       System.setProperty("javax.net.debug","all");

       client.NewWebService_Service service = new client.NewWebService_Service();
       client.NewWebService port = service.getNewWebServicePort();
       java.util.Map<String,Object> rmap = ((BindingProvider)port).getRequestContext();
       java.util.Map<String,List<String>> header= new HashMap();
       header.put("Username", Collections.singletonList("1"));
       header.put("Password", Collections.singletonList("1"));

       rmap.put(MessageContext.HTTP_REQUEST_HEADERS , header);
       rmap.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY,"https://localhost:8443/WSTest1/NewWebService");
      return port.receiveData(resNumber);

    }

}

现在在 tomcat 上我在 server.xml 中有以下内容

 <Connector port="8443" protocol="org.apache.coyote.http11.Http11NioProtocol"
               maxThreads="150" SSLEnabled="true" 

              keystoreFile="conf/mytomcatkeystore.jks" clientAuth="false"  scheme="https" secure="true" 
               />

我正在使用以下选项来启动 tomcat

set JAVA_OPTS="-Djavax.net.ssl.trustStore=file:///c:/mylocalhost.jks" "-Djavax.net.ssl.trustStorePassword=****" "-Djavax.net.debug=all"

现在,当clientAuth="false" 时,通过 ssl 的 Web 服务调用可以正常工作,但是如果 我在 server.xml 中设置了clientAuth="true",出现以下错误。

com.sun.xml.ws.client.ClientTransportException: HTTP transport error: javax.net.ssl.SSLException: Unrecognized SSL message, plaintext connection?

尝试在许多网站上搜索解决方案 - 没有成功。 如果有人可以提供帮助 - 将不胜感激。

【问题讨论】:

    标签: java web-services tomcat ssl


    【解决方案1】:

    好的。经过许多令人沮丧的小时后,我终于找到了解决方案,感谢this SO 帖子。

    所以解决方案是注释掉server.xml中的以下行

    &lt;Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" /&gt;

    【讨论】:

      【解决方案2】:

      我认为您的错误与 https 通信有关。客户端没有指定它想通过 https 进行通信。看看这个Unrecognized SSL message, plaintext connection? Exception

      【讨论】:

      • 但是,我正在使用 -"https://localhost:8443/WSTest1/NewWebService",否则有人会如何指定通信是通过 https 进行的?此外,ssl 在没有客户端身份验证的情况下也能正常工作......
      • 好的。从 tomcat 文档中,属性 clientAuth 的含义:如果您希望 SSL 堆栈在接受连接之前需要来自客户端的有效证书链,请设置为 true。等等...tomcat.apache.org/tomcat-5.5-doc/config/http.html。所以这不是关于用户/密码的有效组合......
      • 这正是我想要实现的 - 使用它的(自签名)证书对客户端进行身份验证....虽然到目前为止还没有运气:-(
      • 还有,对了,别被表头里面的用户名和密码搞糊涂了,和我遇到的问题没有关系……
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多