Java configuration for SAAJInInterceptor and WSS4JInInterceptor beans - spring-boot

I have a web application that provides a soap web service using apache cxf and spring boot. So I have no xml file in my classpath to configure my beans, they are all configured in java configuration.I would like to set up authentication with ws-security from cxf.
So I would like to know if there are java configurations for the SAAJInInterceptor and WSS4JInInterceptor beans for example like this:
#Bean
public ServletRegistrationBean cxfServlet() {
ServletRegistrationBean servlet = new ServletRegistrationBean(new CXFServlet(), "/services/*");
servlet.setLoadOnStartup(1);
return servlet;
}

You just have to add the interceptor when you create your endpoint :
Map<String,Object> interceptorConfig = new HashMap<String,Object>();
// set the properties of you WSS4J config
// ...
WSS4JInInterceptor myInterceptor = new WSS4JInInterceptor(interceptorConfig);
myEndpoint.getInInterceptors().add(myInterceptor);
Another solution is by using annotation with #InInterceptors

Related

Defining multiple endpoints for multiple soap web services in spring boot application

I have a servlet mapping issue in spring boot application with multiple soap web services. I have two (or more) WebServices with different mappings.
Service A -> Endpoint1
Service B -> Endpoint2
Once I deploy spring boot application with two MessageDispatcherServlets , Service A and B both can be accessed with Endpoint1 only. I don't know how I can map Endpoint1 url to ServiceA and Endpoint2 to ServiceB.
Please check sample of my code for Service-A. Code is similar for Service-B.
#Bean(name = "ServiceA")
public Wsdl11Definition wsdl11DefinitionImportAgent() {
SimpleWsdl11Definition simpleWsdl11Definition = new SimpleWsdl11Definition();
simpleWsdl11Definition.setWsdl(new ClassPathResource("/wsdl/ServiceA.wsdl"));
return simpleWsdl11Definition;
}
#Bean
public ServletRegistrationBean messageDispatcherServletServiceA(ApplicationContext
applicationContext) {
MessageDispatcherServlet servlet = new MessageDispatcherServlet();
servlet.setApplicationContext(applicationContext);
servlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean(servlet, "/ws/ServiceA");
}

Spring boot MessageDispatcherServlet overriding DispatcherServlet. how to skip overriding and register both DispatcherServlet?

in a single application, I m trying to use soap as well as rest web service. and each servlet is given different URI, below is the code of config class.
The question is: only soap service URL is working fine but for the rest on getting 405 error.
#EnableWs
#Configuration
public class WebServiceConfig {
#Bean
public ServletRegistrationBean RsRegistrationBean(ApplicationContext applicationContext) {
DispatcherServlet servlet = new DispatcherServlet();
servlet.setApplicationContext(applicationContext);
return new ServletRegistrationBean(servlet,"/rest/*");
}
#Bean
public ServletRegistrationBean<MessageDispatcherServlet> messageDispatcherServlet(
ApplicationContext context) {
MessageDispatcherServlet messageDispatcherServlet = new MessageDispatcherServlet();
messageDispatcherServlet.setApplicationContext(context);
messageDispatcherServlet.setTransformWsdlLocations(true);
return new ServletRegistrationBean<>(messageDispatcherServlet,"/");
}
}
Remove your WebServiceConfig class, as both are auto-configured by Spring Boot already (as of Spring Boot 1.4). Add the following to your application.properties
spring.mvc.servlet.path=/rest
spring.webservices.path=/
Now you leverage the Spring Boot proivded infrastructure instead of fighting with it.

not able to initialize the init methof of servlet from spring boot

i have a custom servlet initialized in my spring boot app.
#Bean
public ServletRegistrationBean<CustomServlet> servletRegistrationBean()
{
log.info("going to initialise the servlet");
return new ServletRegistrationBean<>(new CustomServlet(), “/path1/*");
}
This is inside a configuration classes annotated with #Configuration, whatever i do spring does not call the init method inside the CustomServlet, although the above log is printed
This code was copied from another sample app, where exact same thing is done, but there, the servlet methods init is initialised, Any thoughts ?
spring boot starter parent version:
2.0.4.RELEASE
Try this;
#Bean
public ServletRegistrationBean exampleServletBean() {
ServletRegistrationBean bean = new ServletRegistrationBean(new CustomServlet(), "/exampleServlet/*");
bean.setLoadOnStartup(1);
return bean;
}
and check this out, it may help : https://www.baeldung.com/register-servlet

Spring + Apache CXF: SearchContextProvider not initialized

I'm integrating Spring Boot with Apache CXF. Everyhthing is OK, but I cannot get the SearchContext to work inside the controllers:
#Context
private SearchContext context;
public #ResponseBody List<Users> getAll(#Context SearchContext context, #RequestParam String search){
....
}
The SearchContext is not correctly injected, as is not created by the SearchContextProvider in the package org.apache.cxf.jaxrs.ext.search.
I'm launching the CXF Servlet with this Bean:
#Bean
public ServletRegistrationBean cxfServlet() {
CXFServlet cxf = new CXFServlet();
ServletRegistrationBean registration = new ServletRegistrationBean(cxf, "/api/*");
return registration;
}
I'm using a Java-based configuration, no XML.
In an XML based configuration, it seems that this tag is required:
<jaxrs:providers>
<bean class="org.apache.cxf.jaxrs.ext.search.SearchContextProvider"/>
</jaxrs:providers>
How can I declare the provider in a Java based configuration of the servlet?
In your main config class, add:
#ComponentScan("org.apache.cxf.jaxrs")
Since the class (SearchContextProvider) is already annotated with #Provider annotation, Spring will be able to pick it up if it is being scanned. Assuming that you are already doing component-scan on your own package, you can just add the jaxrs package like:
#ComponentScan({ "com.something", "org.apache.cxf.jaxrs" })

Spring-boot: Configure FreeMarkerConfigurationFactoryBean in web application

I'm trying to use Freemarker for e-mail templating in a web application.
I have declared a FreeMarkerConfigurationFactoryBean as follow:
#Bean
public FreeMarkerConfigurationFactoryBean freeMarkerConfigurationFactoryBean(EmailTemplateService templateService) {
FreeMarkerConfigurationFactoryBean configurationFactoryBean = new FreeMarkerConfigurationFactoryBean();
configurationFactoryBean.setPreTemplateLoaders(templateService);
return configurationFactoryBean;
}
When running my JUnit everything is working well, but when running in my webapp my bean is "overriden" by the spring boot FreeMarkerAutoConfiguration.
I have tried to:
remove the spring-boot-starter-freemarker from my gradle file
#EnableAutoConfiguration(exclude = {FreeMarkerAutoConfigurationk.class})
spring.freemarker.enabled=false
But without success. Any idea?
Thanks guys.
As your application is a Web application, it's Spring Boot's FreeMarkerWebConfiguration that you're interested in. It doesn't use a FreeMarkerConfigurationFactoryBean but a FreeMarkerConfigurer. You should create a FreeMarkerConfigurer bean and configure it as required. For example:
#Bean
public FreeMarkerConfigurer freeMarkerConfigurer(EmailTemplateService templateService) {
FreeMarkerConfigurer configurer = new FreeMarkerConfigurer();
configurer.setPreTemplateLoaders(templateService);
return configurer;
}

Resources