java web服务jax-ws客户端身份验证Tomcat无法正常工作

时间:2016-10-20 07:27:31

标签: java web-services tomcat ssl

我有以下客户端使用在tomcat(本地主机)

上运行的Web服务

客户端

// read from database
DataAccess.User user = db.Users.SingleOrDefault(...);

if (user == null)
{
    // show error page telling that the POSTed email address is not known
}

// update from POST
user.FirstName = manageviewmodel.FirstName;
user.LastName = manageviewmodel.LastName;
// ...

// persist to database
db.SaveChanges();

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

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

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

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

现在,通过ssl进行的Web服务调用在set JAVA_OPTS="-Djavax.net.ssl.trustStore=file:///c:/mylocalhost.jks" "-Djavax.net.ssl.trustStorePassword=****" "-Djavax.net.debug=all" 时可以正常工作 我在server.xml中设置clientAuth="false"我收到以下错误。

clientAuth="true"

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

2 个答案:

答案 0 :(得分:1)

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

所以解决方案是注释 server.xml

中的以下行

<Listener className="org.apache.catalina.core.AprLifecycleListener" SSLEngine="on" />

答案 1 :(得分:0)

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