Question

I have created web service client using Apache axis 1.4. The wcf service i am accessing is STS service which required AppliesTo Parameter and its return SAML token. I have go through various blog and website while searching and some how i came to this kanbancoding Part 3 So i go through it and do the changes in my code but now i am getting org.apache.cxf.ws.policy.PolicyException: None of the policy alternatives can be satisfied. My

Was it helpful?

Solution

Well after lot of try and error i figure out what exactly i need to call WCF STS Service. While goggling i found a document on Docs.Oasis OOasis WS Trust 1.3 which explain in details what data need to be send in a soap Message and yes with the help of kanbancoding this is my method to call the STS

private static void getSecurityToken() {
        try {
            // Use the empty constructor – no need to specify wsdl
            SecurityTokenService src = new SecurityTokenService();

            // Pull the class used to negotiate WS Trust directly from the
            // SecurityTokenService
            IWSTrust13Sync trust = src.getBasicHttpBindingIWSTrust13Sync();

            JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
            factory.setServiceClass(IWSTrust13Sync.class);
            factory.setAddress(ServiceURL);
            IWSTrust13Sync service = (IWSTrust13Sync) factory.create();

            // Obtain a reference to the CXF endpoint using the ClientProxy helper:
            Client client = ClientProxy.getClient(service);

            // Set up logging if desired
            client.getOutInterceptors().add(new LoggingOutInterceptor());
            client.getInInterceptors().add(new LoggingInInterceptor());
            client.getRequestContext().put("com.sun.xml.ws.connect.timeout", 1 * 60 * 1000);
            client.getRequestContext().put("com.sun.xml.ws.request.timeout", 5 * 60 * 1000);

            // Specify the user we want to authenticate
            client.getRequestContext().put("ws-security.username", UserName);
            client.getRequestContext().put("ws-security.password", Password);

            HTTPConduit http = (HTTPConduit) client.getConduit();
            http.getAuthorization().setUserName(UserName);
            http.getAuthorization().setPassword(Password);
            http.getClient().setConnectionTimeout(36000);
            http.getClient().setAllowChunking(false);

            RequestSecurityTokenType token = new RequestSecurityTokenType();

            Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            Element tokenType = document.createElementNS("http://docs.oasis-open.org/ws-sx/ws-trust/200512",
                    "TokenType");
            tokenType.setTextContent("http://docs.oasis-open.org/wss/oasis-wss-saml-token-profile-1.1#SAMLV2.0");

            token.getAny().add(tokenType);

            Element requestType = document.createElementNS("http://docs.oasis-open.org/ws-sx/ws-trust/200512",
                    "RequestType");
            requestType.setTextContent("http://docs.oasis-open.org/ws-sx/ws-trust/200512/Issue");
            token.getAny().add(requestType);

            Document appliesTodoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
            Element appliesTo = appliesTodoc.createElementNS("http://schemas.xmlsoap.org/ws/2004/09/policy",
                    "AppliesTo");

            Element endPoint = appliesTodoc.createElementNS("http://schemas.xmlsoap.org/ws/2004/08/addressing",
                    "EndpointReference");

            Element address = appliesTodoc.createElementNS("http://schemas.xmlsoap.org/ws/2004/08/addressing",
                    "Address");
            address.setTextContent("http://localhost");

            endPoint.appendChild(address);
            appliesTo.appendChild(endPoint);

            token.getAny().add(appliesTo);

            //Now specify what claims we want back.
            Document claimsDoc = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();

            Element claims = claimsDoc.createElementNS("http://docs.oasis-open.org/ws-sx/ws-trust/200512", "Claims");
            claims.setAttribute("Dialect", "http://schemas.microsoft.com/ws/2008/06/identity/securitytokenservice");

            // Add claims to token request
            //token.getAny().add(claims);

            RequestSecurityTokenResponseCollectionType result = service.trust13Issue(token);

            //parseResponse(result);
            List<RequestSecurityTokenResponseType> response = result.getRequestSecurityTokenResponse();

            Iterator<RequestSecurityTokenResponseType> itr = response.iterator();

            while (itr.hasNext()) {
                RequestSecurityTokenResponseType obj = itr.next();
                List<Object> responseObject = obj.getAny();
                Iterator<Object> ObjItr = responseObject.iterator();
                while (ObjItr.hasNext()) {
                    System.out.println("Result " + ObjItr.next());
                }

            }

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

But not fully succeeded in what i want but yes something i am getting in response In Apache CFX Log not in result object.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top