Advanced JAX-WS Security with Wildfly Client

Here too you start by consuming the published WSDL contract using the wsconsume tool(Eclipse or Maven. See our tutorial about how to use them). Then you simply invoke the the endpoint as a standard JAX-WS one:

BASInscriptionWSIntegrationTest

import java.math.BigInteger;
import java.net.URL;
import java.util.Map;

import javax.xml.ws.BindingProvider;

import org.apache.cxf.ws.security.SecurityConstants;
import org.junit.Assert;
import org.junit.Test;

import client.BASInscriptionWSService;
import client.EcoleApplicationInContext;
import client.Etudiant;
import client.GetEtudiantRequest;
import client.IBASinscriptionWS;

public class BASInscriptionWSIntegrationTest {

	@Test
	public void testGetEtudiant() throws Exception {
		BASInscriptionWSService service = new BASInscriptionWSService(
				new URL(
						"http://localhost:8080/BASInscriptionWS-1/BASInscriptionWSPort?wsdl"));
		IBASinscriptionWS port = service.getBASInscriptionWSPort();
		setupWSSecurity(port, "kermit");
		
		Etudiant etudiant = port.getEtudiant(prepareGetEtudiantRequest("32")).getEtudiant();  
		Assert.assertEquals("451247",etudiant.getMatricule());
		Assert.assertEquals("Festado", etudiant.getPrenom());

		setupWSSecurity(port, "alice");
		try {
			port.getEtudiant(prepareGetEtudiantRequest("32")).getEtudiant();  
			Assert.fail("Authorization exception expected!");
		} catch (Exception e) {
			Assert.assertTrue("Authorization error message expected, but got: "
					+ e.getMessage(), e.getMessage().contains("Unauthorised"));
		}
	}

	private GetEtudiantRequest prepareGetEtudiantRequest(String value) {
		GetEtudiantRequest request = new GetEtudiantRequest();
		request.setEtudiantId(new BigInteger(value));
		request.setActif(true);
		return request;
	}

	private void setupWSSecurity(IBASinscriptionWS port, String value) {
		Map<String, Object> ctx = ((BindingProvider) port).getRequestContext();
		ctx.put(SecurityConstants.CALLBACK_HANDLER,
				new ClientPasswordCallback());
		ctx.put(SecurityConstants.USERNAME, value);

	}
}

 ClientPasswordCallback 
The UsernamePasswordCallback class is shown below and is responsible for setting the passwords on client side just before performing the invocations:

import java.util.HashMap;
import java.util.Map;

import org.jboss.wsf.stack.cxf.extensions.security.PasswordCallbackHandler;

public class ClientPasswordCallback extends PasswordCallbackHandler
{
   public ClientPasswordCallback()
   {
      super(getInitMap());
   }
   
   private static Map<String, String> getInitMap() {
      Map<String, String> passwords = new HashMap<String, String>();
      passwords.put("alice", "password");
      passwords.put("bob", "password");
      passwords.put("kermit", "thefrog");
      return passwords;
   }
}

Support?

  • Do you need any kind of  support?
  • Do you need professional coaching?
  • Do you need samples code to start?
  • Do you have any question about our SOAP & REST Architecture and how to implement it?
  • Do you have any question about our API Infrastructure and how to install it?

    We are based in Germany and speak French, German and English

Then feel free to contact us today support@festado.com  


Thanks. Festado Team

Enregistrez vous aujoud'hui, achetez et vendre en ligne gratuitement Clickez ici