SpringBusFactory.setThreadDefaultBus(bus);
URL wsdl = SamlTokenTest.class.getResource("DoubleItSaml.wsdl");
Service service = Service.create(wsdl, SERVICE_QNAME);
QName portQName = new QName(NAMESPACE, "DoubleItSaml2TransportPort");
DoubleItPortType saml2Port =
service.getPort(portQName, DoubleItPortType.class);
updateAddressPort(saml2Port, PORT2);
// Create a SAML Token with no "OneTimeUse" Condition
((BindingProvider)saml2Port).getRequestContext().put(
"ws-security.saml-callback-handler", new SamlCallbackHandler()
);
Client cxfClient = ClientProxy.getClient(saml2Port);
SecurityHeaderCacheInterceptor cacheInterceptor =
new SecurityHeaderCacheInterceptor();
cxfClient.getOutInterceptors().add(cacheInterceptor);
// Make two invocations...should succeed
saml2Port.doubleIt(25);
saml2Port.doubleIt(25);
// Now create a SAML Token with a "OneTimeUse" Condition
ConditionsBean conditions = new ConditionsBean();
conditions.setTokenPeriodMinutes(5);
conditions.setOneTimeUse(true);
SamlCallbackHandler callbackHandler = new SamlCallbackHandler();
callbackHandler.setConditions(conditions);
((BindingProvider)saml2Port).getRequestContext().put(
"ws-security.saml-callback-handler", callbackHandler
);
cxfClient.getOutInterceptors().remove(cacheInterceptor);
cacheInterceptor = new SecurityHeaderCacheInterceptor();
cxfClient.getOutInterceptors().add(cacheInterceptor);
// Make two invocations...should fail on the second one
saml2Port.doubleIt(25);
try {
saml2Port.doubleIt(25);
fail("Failure expected on a replayed SAML Assertion");
} catch (javax.xml.ws.soap.SOAPFaultException ex) {
String error = "A replay attack has been detected";
assertTrue(ex.getMessage().contains(error));
}