SSLHandshakeException talking to a https Web service using Spring WebServiceTemplate - spring

I am getting the below error talking to a https webservice.
org.springframework.ws.client.WebServiceIOException: I/O error:
sun.security.validator.ValidatorException: PKIX path building failed:
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid
certification path to requested target; nested exception is
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX
path
building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to
find valid certification path to requested target
I am using spring's WebServiceTemplate and below's my xml configuration for it
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:sws="http://www.springframework.org/schema/web-services"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services
http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-1.5.xsd">
<bean id="webServiceTemplate"
class="org.springframework.ws.client.core.WebServiceTemplate"
p:marshaller-ref="jaxbMarshaller"
p:unmarshaller-ref="jaxbMarshaller"
p:defaultUri="https://XXXXXXXXXXXXXXXX"
p:messageSender-ref="messageSender">
<constructor-arg ref="messageFactory" />
</bean>
<bean id="messageSender"
class="org.springframework.ws.transport.http.CommonsHttpMessageSender" />
<!-- <bean id="messageSender"
class="org.springframework.ws.transport.http.HttpsUrlConnectionMessageSender" /> -->
<bean id="messageFactory"
class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory" />
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"
p:contextPath="com.test.schemas" />
</beans>
I am able to hit the service from soapui but not from the sample java code I wrote. Could some one please point me to why this is happening and how I can resolve this? Should we have received some security certificates from the 3'rd party wsdl folks?

According to documentation CommonsHttpMessageSender is Deprecated in favor of HttpComponentsMessageSender.
Please configure httpClient property of HttpComponentsMessageSender:
http://docs.spring.io/spring-ws/site/apidocs/org/springframework/ws/transport/http/HttpComponentsMessageSender.html#setHttpClient%28org.apache.http.client.HttpClient%29
See my other post on how to configure httpClient bean to work around self-signed certificate issues. sending https post request with post data using spring web
No need to import keys into keystore.

Check if this works:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:p="http://www.springframework.org/schema/p"
xmlns:sws="http://www.springframework.org/schema/web-services"
xmlns:oxm="http://www.springframework.org/schema/oxm"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/web-services
http://www.springframework.org/schema/web-services/web-services-2.0.xsd
http://www.springframework.org/schema/oxm
http://www.springframework.org/schema/oxm/spring-oxm-1.5.xsd">
<!-- HTTPS connection to trust self signed certificates -->
<bean id="sslSocketFactory" class="org.apache.http.conn.ssl.SSLSocketFactory">
<constructor-arg name="trustStrategy">
<bean class="org.apache.http.conn.ssl.TrustSelfSignedStrategy" />
</constructor-arg>
<constructor-arg name="hostnameVerifier">
<bean class="org.apache.http.conn.ssl.AllowAllHostnameVerifier" />
</constructor-arg>
</bean>
<bean id="httpsSchemaRegistry" class="org.apache.http.conn.scheme.SchemeRegistry">
<property name="items">
<map>
<entry key="https">
<bean class="org.apache.http.conn.scheme.Scheme">
<constructor-arg value="https" />
<constructor-arg value="443" />
<constructor-arg ref="sslSocketFactory" />
</bean>
</entry>
</map>
</property>
</bean>
<bean id="httpClient" class="org.apache.http.impl.client.DefaultHttpClient">
<constructor-arg>
<bean class="org.apache.http.impl.conn.PoolingClientConnectionManager">
<constructor-arg ref="httpsSchemaRegistry" />
</bean>
</constructor-arg>
</bean>
<!-- <bean id="apacheHttpsRequestFactory"
class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
<constructor-arg ref="httpClient" />
-->
<bean id="webServiceTemplate"
class="org.springframework.ws.client.core.WebServiceTemplate"
p:marshaller-ref="jaxbMarshaller"
p:unmarshaller-ref="jaxbMarshaller"
p:defaultUri="https://XXXXXXXXXXXXXXXX"
p:messageSender-ref="messageSender">
<constructor-arg ref="messageFactory" />
</bean>
<bean id="messageSender"
class="org.springframework.ws.transport.http.HttpComponentsMessageSender"
p:httpClient="httpClient" />
<bean id="messageFactory"
class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory" />
<bean id="jaxbMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"
p:contextPath="com.test.schemas" />
</beans>

Related

Getting error for EntityManagerFactory bean creation: com.sun.proxy.$ProxyXXX cannot be cast to javax.persistence.EntityManagerFactory

Trying to build simple spring integration app but it's failing for "emf" bean creation from following xml configuration file.
I've tried java configuration mentioned in Spring JPA - org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean cannot be cast to javax.persistence.EntityManagerFactory but still getting the same error.
Using following spring versions:
spring 5.3.18,
spring-integration 5.4.13,
hibernate 5.4.12.Final
XML Configuration:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/jee
http://www.springframework.org/schema/jee/spring-jee.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<tx:annotation-driven transaction-manager="transactionManager" />
<bean id="emf"
class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="someDataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
<property name="persistenceUnitName" value="XXX" />
<property name="persistenceUnitManager" ref="persistenceUnitManager" />
</bean>
<bean id="jpaVendorAdapter"
class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="emf" />
</bean>
<bean id="persistenceUnitManager"
class="org.springframework.orm.jpa.persistenceunit.DefaultPersistenceUnitManager">
<property name="defaultDataSource" ref="someDataSource" />
</bean>
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<constructor-arg ref="someDataSource"/>
</bean>
<jee:jndi-lookup id="someDataSource" jndi-name="Da:/someDS" />
</beans>

Cannot find class [org.springframework.http.converter.json.Jackson2ObjMapperFactoryBean]

I am working on a simple demo project and after fixing multiple erroes now i am stuck here,
bean declaration couldn't find class org.springframework.http.converter.json.Jackson2ObjMapperFactoryBean .I couldn't make head or tail of the error as i see all declared properly. May be someone experienced can point out the mistake here.
A detailed explanation would be nice.
i am using spring 5.3.13
java 11
tomcat 9
Error :
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'entityManagerFactory' defined in ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml]: Invocation of init method failed; nested exception is java.lang.IllegalStateException: No persistence units parsed from {classpath*:META-INF/persistence.xml}
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.http.converter.json.Jackson2ObjMapperFactoryBean] for bean with name 'jsonMapper' defined in ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.http.converter.json.Jackson2ObjMapperFactoryBean
Related cause: org.springframework.beans.factory.CannotLoadBeanClassException: Cannot find class [org.springframework.http.converter.json.Jackson2ObjMapperFactoryBean] for bean with name 'xmlMapper' defined in ServletContext resource [/WEB-INF/dispatcherServlet-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.springframework.http.converter.json.Jackson2ObjMapperFactoryBean
dispatcherServlet-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.3.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.3.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/sprin-jpa-1.8.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-4.3.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.3.xsd">
<!-- xmlns:repository="http://www.springframework.org/schema/data/repository" -->
<!-- http://www.springframework.org/schema/data/repository http://www.springframework.org/schema/data/repository/spring-repository.xsd -->
<context:component-scan base-package="org.charlie" />
<mvc:annotation-driven>
<mvc:message-converters>
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="jsonMapper"></property>
</bean>
<bean class="org.springframework.http.converter.xml.MappingJackson2xmlHttpMesssageConverter">
<property name="objectMapper" ref="xmlMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="jsonMapper" class="org.springframework.http.converter.json.Jackson2ObjMapperFactoryBean">
<property name="simpleDateFormat" value="yyyy-MM-dd HH:mm:ss" />
</bean>
<bean id="xmlMapper" parent="jsonMapper">
<property name="createXmlMapper" value="true"/>
</bean>
<mvc:resources mapping="/webjars/**" location="classpath:META-INF/resources/webjars/" />
<jpa:repositories base-package="org.charlie.repository" />
<jdbc:embedded-database id="dataSource" type="H2">
<jdbc:script location="classpath:META-INF/sql/schema.sql" />
<jdbc:script location="classpath:META-INF/sql/data.sql" />
</jdbc:embedded-database>
<bean id="jpaVendorAdapter" class="org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter">
<property name="showSql" value="true" />
</bean>
<bean id="entityManagerFactory" class="org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean">
<property name="dataSource" ref="dataSource" />
<property name="jpaVendorAdapter" ref="jpaVendorAdapter" />
</bean>
<bean id="transactionManager" class="org.springframework.orm.jpa.JpaTransactionManager">
<property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>
<tx:annotation-driven transaction-manager="transactionManager" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
<bean id="h2WebServer" class="org.h2.tools.Server" factory-method="createWebServer" init-method="start" destroy-method="stop">
<constructor-arg value="-web,-webAllowOthers,-webDaemon,-webPort,8082" />
</bean>
</beans>
persistence.xml
<?xml version="1.0" encoding="UTF-8"?>
<persistence xmlns="http://java.sun.com/xml/ns/persistence"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-isntance"
xsi:schemaLocation="http://java.sun.com/xml/ns/persistence http://java.sun.com/xml/ns/persistence/persistence_1_0.xsd" version="1.0">
<persistence-unit name="toDo">
<description>My Persistence Unit</description>
</persistence-unit>
</persistence>
It seems there is a typo. It should be Jackson2ObjectMapperFactoryBean instead of Jackson2ObjMapperFactoryBean.

How to add google OAuth(spring-social-google) to the existing spring project using spring-social?

I developed spring application with oauth authentication using spring social project. I'm currently able to authenticate using facebook, twitter and linkedin. I would like to also add google authentication using spring-social-google. Its really confusing for me where to add google's connection factory in the existing spring social configuration.
My current spring social application context is
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:facebook="http://www.springframework.org/schema/social/facebook"
xmlns:twitter="http://www.springframework.org/schema/social/twitter"
xmlns:social="http://www.springframework.org/schema/social"
xmlns:linkedin="http://www.springframework.org/schema/social/linkedin"
xmlns:c="http://www.springframework.org/schema/c"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/social/facebook http://www.springframework.org/schema/social/spring-social-facebook.xsd
http://www.springframework.org/schema/social/linkedin http://www.springframework.org/schema/social/spring-social-linkedin.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/social/twitter http://www.springframework.org/schema/social/spring-social-twitter.xsd
http://www.springframework.org/schema/social http://www.springframework.org/schema/social/spring-social.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">
<context:property-placeholder location="/WEB-INF/properties/application.properties" />
<facebook:config app-id="${facebook.clientId}" app-secret="${facebook.clientSecret}" app-namespace="my-app" />
<twitter:config app-id="${twitter.consumerKey}" app-secret="${twitter.consumerSecret}"/>
<linkedin:config app-id="${linkedin.consumerKey}" app-secret="${linkedin.consumerSecret}"/>
<social:jdbc-connection-repository/>
<bean id="userIdSource" class="org.springframework.social.security.AuthenticationNameUserIdSource" />
<bean id="connectController" class="org.springframework.social.connect.web.ConnectController" autowire="constructor">
<property name="connectInterceptors">
<list>
<bean class="com.prs.ony.controller.facebook.PostToWallAfterConnectInterceptor" />
<bean class="com.prs.ony.controller.twitter.TweetAfterConnectInterceptor" />
</list>
</property>
</bean>
<bean id="psc" class="org.springframework.social.connect.web.ProviderSignInController" autowire="constructor" />
<bean id="signInAdapter" class="com.prs.ony.controller.signin.SimpleSignInAdapter" autowire="constructor" />
<!-- <bean id="disconnectController" class="org.springframework.social.facebook.web.DisconnectController"
c:_0-ref="usersConnectionRepository" c:_1="${facebook.clientSecret}" /> -->
</beans>
Now im confused where to add the googles connection factory. It has to work along with the existing there oauth providers.
<bean id="connectionFactoryLocator" class="org.springframework.social.connect.support.ConnectionFactoryRegistry">
<property name="connectionFactories">
<list>
<bean class="org.springframework.social.google.connect.GoogleConnectionFactory">
<constructor-arg value="${google.consumerKey}" />
<constructor-arg value="${google.consumerSecret}" />
</bean>
</list>
</property>
</bean>
Any help would be highly appreciated.
It is possible to omit the usage of the concrete social configuration namespace and register connection factories all together:
<bean id="connectionFactoryLocator" class="org.springframework.social.connect.support.ConnectionFactoryRegistry">
<property name="connectionFactories">
<list>
<bean class="org.springframework.social.facebook.connect.FacebookConnectionFactory">
<constructor-arg name="appId" value="${facebook.clientId}" />
<constructor-arg name="appSecret" value="${facebook.clientSecret}" />
</bean>
<bean class="org.springframework.social.twitter.connect.TwitterConnectionFactory">
<constructor-arg name="consumerKey" value="${twitter.consumerKey}" />
<constructor-arg name="consumerSecret" value="${twitter.consumerSecret}" />
</bean>
<bean class="org.springframework.social.linkedin.connect.LinkedInConnectionFactory">
<constructor-arg name="consumerKey" value="${linkedin.consumerKey}" />
<constructor-arg name="consumerSecret" value="${linkedin.consumerSecret}" />
</bean>
<bean class="org.springframework.social.google.connect.GoogleConnectionFactory">
<constructor-arg name="clientId" value="${google.consumerKey}" />
<constructor-arg name="clientSecret" value="${google.consumerSecret}" />
</bean>
</list>
</property>
</bean>

Spring 3 with Thymeleaf config issue

Hello I have the following jars in my build path -
spring-beans-3.1.2.RELEASE.jar
spring-context-3.1.2.RELEASE.jar
spring-core-3.1.2.RELEASE.jar
spring-expression-3.1.2.RELEASE.jar
spring-web-3.1.2.RELEASE.jar
spring-webmvc-3.1.2.RELEASE.jar
thymeleaf-spring3-2.0.13.jar
and my servlet
<?xml version="1.0" encoding="UTF-8"?><br>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:component-scan base-package="web.controller" />
<!-- Enabling Spring MVC configuration through annotations -->
<mvc:annotation-driven />
<!-- Mapping Static Resources -->
<mvc:resources mapping="/resources/**" location="/resources/" />
<bean id="templateResolver" class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".html" />
<property name="templateMode" value="HTML5" />
</bean>
<bean id="templateEngine" class="org.thymeleaf.spring3.SpringTemplateEngine">
<property name="templateResolver" ref="templateResolver" />
</bean>
<bean class="org.thymeleaf.spring3.view.ThymeleafViewResolver">
<property name="templateEngine" ref="templateEngine" />
<property name="order" value="1" />
<property name="viewNames" value="*.html" />
</bean>
</beans>
The error I get on launching is -
Cannot find class [org.thymeleaf.templateresolver.ServletContextTemplateResolver] for bean with name 'templateResolver' defined in ServletContext resource [/WEB-INF/springMVC-servlet.xml]; nested exception is java.lang.ClassNotFoundException: org.thymeleaf.templateresolver.ServletContextTemplateResolver
Am I missing any other library here? Any help is much appreciated.
You are missing the actual Thymeleaf jar. You included the Spring jar that provides the integration but you missed the actual implementation of it.
Download the jar from here
thymeleaf download site

spring multiple property file

I have a spring project. There are two property files. one property file is config in dbConfig.xml and I cannot change it. I have my own appConfig.xml
I have the following in it
<util:properties id="configProps" location="classpath:spring/config.properties" />
<bean id="createDummyDataTask" class="com.merc.spring.CreateDummyData" scope="step">
<property name="srcFolder" value="${configProps.srcDir}"/>
</bean>
using either srcDir or configProps.srcDir does not seem to work.
${} works only for property files loaded by the context:propertyplaceholder. If you are using spring 3.0 you can use #{} which is processed as a Spel (spring expression language). The following should work.
<util:properties id="configProps" location="classpath:spring/config.properties" />
<bean id="createDummyDataTask" class="com.merc.spring.CreateDummyData" scope="step">
<property name="srcFolder" value="#{configProps.srcDir}"/>
</bean>
Here how we resolved it
Context.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<import resource="Settings.xml"/>
<import resource="Database.xml"/>
</beans>
Settings.xml
In the list many property file could be added
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>db.properties</value>
</list>
</property>
</bean>
</beans>
Sample bean for processing db.properties file
Database.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd">
<!-- the transaction manager -->
<bean id="dstxnManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="appDataSource"/>
</bean>
<!-- the DataSource (parameterized for configuration via a PropertyPlaceHolderConfigurer) -->
<bean id="appDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource" lazy-init="false">
<property name="driverClass" value="${driverClass}" />
<property name="jdbcUrl" value="${jdbcUrl}" />
<property name="user" value="${username}" />
<property name="password" value="${password}" />
</bean>
<!-- DB connection factory -->
<bean id="storageDaoFactory" class="com.dao.StorageDAOFactory">
<constructor-arg><ref bean="dstxnManager"/></constructor-arg>
</bean>
</beans>

Resources