JAX-WS Password Type PasswordText

前端 未结 2 1111
情深已故
情深已故 2020-12-02 16:01

I\'ve got a simple command line Java JAX-WS app to test a SOAP request, but the server is expecting the Password Type to be PasswordText and I\'m stumped on how to set this.

2条回答
  •  难免孤独
    2020-12-02 16:44

    If you implement SOAPHandler interface, the method msgCtx.getMessage() will render the entire XML, and if you are working with big files you will have Out of Memory errors. I tested with UsernameToken authentication on JAX-WS client and it works:

    String SECURITY_NS = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd";
    String PASSWORD_TYPE = "http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText";
    String AUTH_PREFIX = "wss";
    
    MyService service = new MyService();
    MyServicePort port = service.getMyServicePort();
    
    try {
        SOAPFactory soapFactory = SOAPFactory.newInstance();
        SOAPElement security = soapFactory.createElement("Security", AUTH_PREFIX, SECURITY_NS);
        SOAPElement uToken = soapFactory.createElement("UsernameToken", AUTH_PREFIX, SECURITY_NS);
        SOAPElement username = soapFactory.createElement("Username", AUTH_PREFIX, SECURITY_NS);
        username.addTextNode("username");
    
        SOAPElement pass = soapFactory.createElement("Password", AUTH_PREFIX, SECURITY_NS);
        pass.addAttribute(new QName("Type"), PASSWORD_TYPE);
        pass.addTextNode("password");
    
        uToken.addChildElement(username);
        uToken.addChildElement(pass);
        security.addChildElement(uToken);
    
        Header header = Headers.create(security);
        ((WSBindingProvider) port).setOutboundHeaders(header);
    
        // now, call webservice
    
    } catch (SOAPException ex) {
        ex.printStackTrace();
    }
    

    Edit: You should add the "rt.jar" from jre to classpath.

提交回复
热议问题