use of org.apache.cxf.ws.policy.WSPolicyFeature in project cxf by apache.
the class MtomPolicyTest method setupServer.
public void setupServer(boolean mtomRequired, String address) throws Exception {
getStaticBus().getExtension(PolicyEngine.class).setAlternativeSelector(new FirstAlternativeSelector());
JaxWsServerFactoryBean sf = new JaxWsServerFactoryBean();
sf.setServiceBean(new EchoService());
sf.setBus(getStaticBus());
sf.setAddress(address);
WSPolicyFeature policyFeature = new WSPolicyFeature();
List<Element> policyElements = new ArrayList<>();
if (mtomRequired) {
policyElements.add(StaxUtils.read(getClass().getResourceAsStream("mtom-policy.xml")).getDocumentElement());
} else {
policyElements.add(StaxUtils.read(getClass().getResourceAsStream("mtom-policy-optional.xml")).getDocumentElement());
}
policyFeature.setPolicyElements(policyElements);
sf.getFeatures().add(policyFeature);
sf.create();
}
use of org.apache.cxf.ws.policy.WSPolicyFeature in project cxf by apache.
the class UsernameTokenTest method testPlaintextCodeFirst.
// Here we are not using the WSDL and so need to add the policy manually on the client side
@org.junit.Test
public void testPlaintextCodeFirst() throws Exception {
String address = "https://localhost:" + PORT + "/DoubleItUTPlaintext";
QName portQName = new QName(NAMESPACE, "DoubleItPlaintextPort");
WSPolicyFeature policyFeature = new WSPolicyFeature();
Element policyElement = StaxUtils.read(getClass().getResourceAsStream("plaintext-pass-timestamp-policy.xml")).getDocumentElement();
policyFeature.setPolicyElements(Collections.singletonList(policyElement));
JaxWsProxyFactoryBean clientFactoryBean = new JaxWsProxyFactoryBean();
clientFactoryBean.setFeatures(Collections.singletonList(policyFeature));
clientFactoryBean.setAddress(address);
clientFactoryBean.setServiceName(SERVICE_QNAME);
clientFactoryBean.setEndpointName(portQName);
clientFactoryBean.setServiceClass(DoubleItPortType.class);
DoubleItPortType port = (DoubleItPortType) clientFactoryBean.create();
if (test.isStreaming()) {
SecurityTestUtil.enableStreaming(port);
}
((BindingProvider) port).getRequestContext().put(SecurityConstants.USERNAME, "Alice");
((BindingProvider) port).getRequestContext().put(SecurityConstants.CALLBACK_HANDLER, "org.apache.cxf.systest.ws.common.UTPasswordCallback");
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
final KeyStore ts = KeyStore.getInstance("JKS");
try (InputStream trustStore = ClassLoaderUtils.getResourceAsStream("keys/Truststore.jks", UsernameTokenTest.class)) {
ts.load(trustStore, "password".toCharArray());
}
tmf.init(ts);
TLSClientParameters tlsParams = new TLSClientParameters();
tlsParams.setTrustManagers(tmf.getTrustManagers());
tlsParams.setDisableCNCheck(true);
Client client = ClientProxy.getClient(port);
HTTPConduit http = (HTTPConduit) client.getConduit();
http.setTlsClientParameters(tlsParams);
assertEquals(50, port.doubleIt(25));
((java.io.Closeable) port).close();
}
use of org.apache.cxf.ws.policy.WSPolicyFeature in project tesb-rt-se by Talend.
the class PolicyProviderImpl method init.
@PostConstruct
public void init() {
final EsbSecurity esbSecurity = EsbSecurity.fromString((String) serviceAutentication);
if (EsbSecurity.NO == esbSecurity)
return;
Bus currentBus = BusFactory.getThreadDefaultBus();
policyBuilder = currentBus.getExtension(PolicyBuilder.class);
List<Policy> policies = new ArrayList<Policy>();
if (EsbSecurity.TOKEN == esbSecurity) {
policies.add(getTokenPolicy());
} else if (EsbSecurity.SAML == esbSecurity) {
policies.add(getSamlPolicy());
}
Map<String, Object> endpointProps = new HashMap<String, Object>();
if (EsbSecurity.TOKEN == esbSecurity) {
JAASUsernameTokenValidator jaasUTValidator = new JAASUsernameTokenValidator();
jaasUTValidator.setContextName("karaf");
endpointProps.put(SecurityConstants.USERNAME_TOKEN_VALIDATOR, jaasUTValidator);
}
if (EsbSecurity.SAML == esbSecurity) {
endpointProps.put(SecurityConstants.SIGNATURE_PROPERTIES, getSignatureProperties());
endpointProps.put(SecurityConstants.SIGNATURE_USERNAME, getSignatureUsername());
endpointProps.put(ENDPOINT_SIGNATURE_PASSWORD, getSignaturePassword());
endpointProps.put(SecurityConstants.CALLBACK_HANDLER, new WSPasswordCallbackHandler(getSignatureUsername(), getSignaturePassword()));
}
locatorEndpoint.setProperties(endpointProps);
WSPolicyFeature policyFeature = new WSPolicyFeature();
policyFeature.setPolicies(policies);
locatorEndpoint.getFeatures().add(policyFeature);
ServerRegistry registry = currentBus.getExtension(ServerRegistry.class);
List<Server> servers = registry.getServers();
for (Server sr : servers) {
if (sr.getEndpoint().getService() == locatorEndpoint.getService())
policyFeature.initialize(sr, currentBus);
}
}
use of org.apache.cxf.ws.policy.WSPolicyFeature in project tesb-rt-se by Talend.
the class SAMClientSecurityProvider method init.
@PostConstruct
public void init() {
final EsbSecurityConstants esbSecurity = EsbSecurityConstants.fromString(authenticationType);
if (EsbSecurityConstants.NO == esbSecurity) {
return;
}
Bus bus = client.getBus();
List<Policy> policies = new ArrayList<Policy>();
WSPolicyFeature policyFeature = new WSPolicyFeature();
policyFeature.setPolicies(policies);
Map<String, Object> properties = client.getRequestContext();
if (null == properties) {
properties = new HashMap<String, Object>();
}
if (EsbSecurityConstants.BASIC == esbSecurity) {
AuthorizationPolicy authzPolicy = new AuthorizationPolicy();
authzPolicy.setUserName(username);
authzPolicy.setPassword(password);
authzPolicy.setAuthorizationType(HttpAuthHeader.AUTH_TYPE_BASIC);
HTTPConduit conduit = (HTTPConduit) client.getConduit();
conduit.setAuthorization(authzPolicy);
} else if (EsbSecurityConstants.USERNAMETOKEN == esbSecurity) {
policies.add(loadPolicy(policyUsernameToken, bus));
java.util.Map<String, Object> wssProps = new java.util.HashMap<String, Object>();
wssProps.put(ConfigurationConstants.ACTION, ConfigurationConstants.USERNAME_TOKEN);
wssProps.put(ConfigurationConstants.USER, username);
wssProps.put(ConfigurationConstants.PASSWORD_TYPE, WSS4JConstants.PW_TEXT);
wssProps.put(ConfigurationConstants.PW_CALLBACK_REF, new CallbackHandler() {
public void handle(Callback[] callbacks) throws IOException, UnsupportedCallbackException {
((WSPasswordCallback) callbacks[0]).setPassword(password);
}
});
client.getEndpoint().getOutInterceptors().add(new WSS4JOutInterceptor(wssProps));
client.getRequestContext().put("security.username", username);
client.getRequestContext().put("security.password", password);
} else if (EsbSecurityConstants.SAML == esbSecurity) {
policies.add(loadPolicy(policySaml, bus));
properties.put(SecurityConstants.SIGNATURE_PROPERTIES, processFileURI(getSignatureProperties()));
properties.put(SecurityConstants.SIGNATURE_USERNAME, getSignatureUsername());
properties.put(ENDPOINT_SIGNATURE_PASSWORD, getSignaturePassword());
properties.put(SecurityConstants.CALLBACK_HANDLER, new WSPasswordCallbackHandler(getSignatureUsername(), getSignaturePassword()));
// STS client
STSClient stsClient = new STSClient(bus);
stsClient.setWsdlLocation(stsWsdlLocation);
stsClient.setServiceQName(new QName(stsNamespace, stsServiceName));
stsClient.setEndpointQName(new QName(stsNamespace, stsEndpointName));
Map<String, Object> stsProperties = new HashMap<String, Object>();
stsProperties.put(SecurityConstants.USERNAME, username);
stsProperties.put(SecurityConstants.PASSWORD, password);
stsProperties.put(SecurityConstants.CALLBACK_HANDLER, new WSPasswordCallbackHandler(username, password));
stsProperties.put(SecurityConstants.STS_TOKEN_PROPERTIES, processFileURI(getSignatureProperties()));
stsProperties.put(SecurityConstants.STS_TOKEN_USERNAME, signatureUsername);
stsProperties.put(SecurityConstants.STS_TOKEN_USE_CERT_FOR_KEYINFO, stsTokenUsecert);
stsProperties.put(SecurityConstants.ENCRYPT_PROPERTIES, processFileURI(getSignatureProperties()));
stsProperties.put(SecurityConstants.ENCRYPT_USERNAME, encryptionUsername);
stsProperties.put(SecurityConstants.IS_BSP_COMPLIANT, isBspCompliant);
stsClient.setProperties(stsProperties);
properties.put(SecurityConstants.STS_CLIENT, stsClient);
}
client.getEndpoint().getActiveFeatures().add(policyFeature);
policyFeature.initialize(client, bus);
}
use of org.apache.cxf.ws.policy.WSPolicyFeature in project tesb-rt-se by Talend.
the class SAMServiceSecurityProvider method init.
@PostConstruct
public void init() {
final EsbSecurityConstants esbSecurity = EsbSecurityConstants.fromString(authenticationType);
if (EsbSecurityConstants.NO == esbSecurity) {
return;
}
Bus bus = serviceEndpoint.getBus();
List<Policy> policies = new ArrayList<Policy>();
WSPolicyFeature policyFeature = new WSPolicyFeature();
policyFeature.setPolicies(policies);
Map<String, Object> properties = serviceEndpoint.getProperties();
if (null == properties) {
properties = new HashMap<String, Object>();
}
if (EsbSecurityConstants.BASIC == esbSecurity) {
JAASLoginInterceptor interceptor = new JAASLoginInterceptor();
interceptor.setContextName("karaf");
serviceEndpoint.getInInterceptors().add(interceptor);
} else if (EsbSecurityConstants.USERNAMETOKEN == esbSecurity) {
policies.add(loadPolicy(policyUsernameToken, bus));
JAASUsernameTokenValidator jaasUTValidator = new JAASUsernameTokenValidator();
jaasUTValidator.setContextName("karaf");
properties.put(SecurityConstants.USERNAME_TOKEN_VALIDATOR, jaasUTValidator);
serviceEndpoint.setProperties(properties);
} else if (EsbSecurityConstants.SAML == esbSecurity) {
policies.add(loadPolicy(policySaml, bus));
properties.put(SecurityConstants.SIGNATURE_PROPERTIES, getSignatureProperties());
properties.put(SecurityConstants.SIGNATURE_USERNAME, getSignatureUsername());
properties.put(ENDPOINT_SIGNATURE_PASSWORD, getSignaturePassword());
properties.put(SecurityConstants.CALLBACK_HANDLER, new WSPasswordCallbackHandler(getSignatureUsername(), getSignaturePassword()));
serviceEndpoint.setProperties(properties);
}
serviceEndpoint.getFeatures().add(policyFeature);
ServerRegistry registry = bus.getExtension(ServerRegistry.class);
List<Server> servers = registry.getServers();
for (Server server : servers) {
if (server.getEndpoint().getService() == serviceEndpoint.getService()) {
policyFeature.initialize(server, bus);
}
}
}
Aggregations