Spring SAML cant read property from properties file - java

So this one stumps me. I have been using properties files for years now, but I have never seen this.
I am using Spring MVC with SAML authentication. my context xml has this in it:
<context:property-placeholder location="file:/opt/saml.properties" />
<bean id="keyManager" class="org.springframework.security.saml.key.JKSKeyManager">
<constructor-arg value="file:/opt/mySamlKeystore.jks"/>
<constructor-arg type="java.lang.String" value="${keystore.password}"/>
<constructor-arg>
<map>
<entry key="${privateKey.alias}" value="${key.password}"/>
</map>
</constructor-arg>
<constructor-arg type="java.lang.String" value="${privateKey.alias}"/>
</bean>
I am getting this error:
java.security.UnrecoverableKeyException: Cannot recover key
I so some SO research and they all say basically that I have the wrong password, which im sure I don't. So to test that it's reading the right file, I go and replace all the properties %{} and hard code them. Everything then works fine.
I am trying to figure this out, when I noticed that some of the other properties from that file are working! In fact, I can even do this:
<bean id="keyManager" class="org.springframework.security.saml.key.JKSKeyManager">
<constructor-arg value="file:/opt/myKeystore.jks"/>
<constructor-arg type="java.lang.String" value="${keystore.password}"/>
<constructor-arg>
<map>
<entry key="${privateKey.alias}" value="password"/>
</map>
</constructor-arg>
<constructor-arg type="java.lang.String" value="${privateKey.alias}"/>
</bean>
So spring is getting ${keystore.password} and ${privateKey.alias} (Along with others needed like entityID, metadataProvider, etc...) from the properties file, but not ${key.password} !!!
here is the saml.properties
#keystore stuff
keystore.password=password
key.password=password
privateKey.alias=mysaml
#SP stuff (aka, my side of things)
entity.id=mycompany:me:me:me1
entity.base.url=https://mycompany.com
#IDP stuff (aka, the SAML server)
metadata.provider=https://saml.mycompany.com/FederationMetadata/2007-06/FederationMetadata.xml
This is all working when I hard coded the key password, but not when I use the ${key.password} property. What is going on here?

you have two more slash after file
For example
<bean id="keyManager" class="org.springframework.security.saml.key.JKSKeyManager">
<constructor-arg value="file:///opt/myKeystore.jks"/>
<constructor-arg type="java.lang.String" value="${keystore.password}"/>
<constructor-arg>
<map>
<entry key="${privateKey.alias}" value="password"/>
</map>
</constructor-arg>
<constructor-arg type="java.lang.String" value="${privateKey.alias}"/>
</bean>

Related

How to convert an xml config bean to java annotation bean ( spring boot )

I am new to java AOP. I am supposed to convert the following xml config to java annotation config in my spring boot application. May I know how exactly to convert this xml config to java annotation config:
I think none of the examples that I saw in stackoverflow match the pattern I am trying to convert.
<bean id="xyzRestTemplate"
class="org.springframework.web.client.RestTemplate">
<constructor-arg ref="xyzClientHttpRequestFactory" />
<property name="messageConverters">
<list>
<bean class="org.springframework.http.converter.xml.MarshallingHttpMessageConverter">
<property name="marshaller" ref="jaxbDataMarshaller" />
<property name="unmarshaller" ref="jaxbDataMarshaller" />
</bean>
</list>
</property>
<property name="interceptors">
<list>
<bean class="com.example.XYZHeaderRequestInterceptor" />
</list>
</property>
</bean>
<bean id="jaxbDataMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="contextPaths">
<list>
<value>com.a.b.c.d.v2</value>
</list>
</property> </bean>
When people talk about converting from XML they don't mean necessarily doing the same thing exactly. What makes Spring Boot attractive isn't just that a configuration is a java class.
You should convert this to use RestTemplate https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-resttemplate.html
Then you just build the RestTemplate using the builder to have the JaxB marshaller and the interceptor you want.
Rest Template - XML Indentation
A nice testcase that passes with XML and passes with #Configuration classes will prove you got it right.

Re Read file when using int-sftp:inbound-channel-adapter

I have a int-sftp:inbound-channel-adapter which uses SftpPersistentAcceptOnceFileListFilter as part of a composite filter. Reading the documentation/ source code it should accept a file to be read again if the modified datetime has changed, but I cant get it to work, it only reads is the once. Im using redis as the store.
Any ideas what is wrong with the configuration, Im using spring integration 4.3.5
<int-sftp:inbound-channel-adapter id="sftpInboundAdapterCensus"
channel="sftpInboundCensus"
session-factory="sftpSessionFactory"
local-directory="${sftp.localdirectory}/census-local"
filter="censusCompositeFilter"
remote-file-separator="/"
remote-directory="${sftp.directory.census}">
<int:poller cron="${sftp.cron}" max-messages-per-poll="1" error-channel="pollerErrorChannel"/>
</int-sftp:inbound-channel-adapter>
<bean id="censusCompositeFilter"
class="org.springframework.integration.file.filters.CompositeFileListFilter">
<constructor-arg>
<list>
<bean class="org.springframework.integration.sftp.filters.SftpSimplePatternFileListFilter">
<constructor-arg value="*.xml" />
</bean>
<bean id="SftpPersistentAcceptOnceFileListFilter" class="org.springframework.integration.sftp.filters.SftpPersistentAcceptOnceFileListFilter">
<constructor-arg ref="metadataStore" />
<constructor-arg value="censusSampleLock_" />
</bean>
</list>
</constructor-arg>
</bean>
The SftpPersistentAcceptOnceFileListFilter only controls what we fetch from the server. You also need a FileSystemPersistentAcceptOnceFileListFilter in the local-filter (which determines which files that have been fetched end up being emitted as messages). The local filter is an AcceptOnceFileListFilter by default.

Spring Integration Kafka message-driven-channel-adapter receive message

For spring-integration-kafka version 2.1.0.RELEASE, documentation seems to be outdated
The example in the doc is incorrect as it doesn't match the constructor argument for KafkaMessageListenerContainer. Can somebody direct me how to create the bean correctly and corresponding Java code to process the message ?
<bean id="container1" class="org.springframework.kafka.listener.KafkaMessageListenerContainer">
<constructor-arg>
<bean class="org.springframework.kafka.core.DefaultKafkaConsumerFactory">
<constructor-arg>
<map>
<entry key="bootstrap.servers" value="localhost:9092" />
</map>
</constructor-arg>
</bean>
</constructor-arg>
<constructor-arg name="topics" value="foo" />
</bean>
Sorry about that; we'll fix the docs; the correct documentation is in the quick start section.

Loading application configuration properties from database in spring based application using java based configuration

Its better to store the configuration properties in a database table so that it can be managed easily for different environments. The approach to store and retrieve the configuration properties from database table in xml based configuration is like below :
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
<property name="properties">
<bean class="org.apache.commons.configuration.ConfigurationConverter" factory-method="getProperties">
<constructor-arg>
<bean class="org.apache.commons.configuration.DatabaseConfiguration">
<constructor-arg>
<ref bean="dbDataSource" />
</constructor-arg>
<constructor-arg value="DOMAIN_CONFIG" />
<!-- DB Table -->
<constructor-arg value="CONFIG_NAME" />
<!-- DB Key Column -->
<constructor-arg value="CONFIG_VALUE" />
<!-- DB Value Column -->
</bean>
</constructor-arg>
</bean>
</property>
</bean>
But the same thing i'm trying to achieve using java based configuration but no luck.
Can anyone please help me.
I found answer for my question.
Thanks to this post : https://gist.github.com/jeffsheets/8ab5f3aeb74787bdb051
This exactly suits to my problem. Thanks.!

Remove _class from mongodb document

We all know if we are trying to add a document via spring-mvc to mongodb, spring adds _class to track & facilitate future marshalling/unmarshalling of the document. In cases in becomes a overhead and one might want not to include _class in mongodb. The following simple spring config helps to avoid adding the _class in mongodb.
<bean id="defaultTypeMapper"
class="org.springframework.data.mongodb.core.convert.DefaultTypeMapper">
<property name="typeKey"><null/></property>
</bean>
<bean id="defaultMongoConverter"
class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mappingContext" ref="mappingContext" />
<property name="typeMapper" ref="defaultTypeMapper"></property>
</bean>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mongoConverter" ref="defaultMongoConverter" />
<property name="writeResultChecking" value="EXCEPTION" />
</bean>
One can easily find the config for mongodbFactory and mappingContext. So I have not added it here. Note the way I have included typeKey.
The question is: how to suppress the _class entry that the spring framework adds to MongoDB objects to support polymorphism. If you don't need polymorphism support, _class wastes spaces and introduces issues if you refactor your POJO / model classes to different packages.
The above example was close, but has a few errors (e.g. DefaultTypeMapper should be DefaultMongoTypeMapper and typeKey is a constructor-arg, not set as a property).
The following works for me...
<mongo:mongo host="hostname" port="27017">
<mongo:options
...options...
</mongo:mongo>
<mongo:db-factory dbname="blee" username="blee" password="blee" mongo-ref="mongo"/>
<bean id="mongoTypeMapper" class="org.springframework.data.mongodb.core.convert.DefaultMongoTypeMapper">
<constructor-arg name="typeKey"><null/></constructor-arg>
</bean>
<bean id="mongoMappingContext" class="org.springframework.data.mongodb.core.mapping.MongoMappingContext" />
<bean id="mongoConverter" class="org.springframework.data.mongodb.core.convert.MappingMongoConverter">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory" />
<constructor-arg name="mappingContext" ref="mongoMappingContext" />
<property name="typeMapper" ref="mongoTypeMapper"></property>
</bean>
<bean id="mongoTemplate" class="org.springframework.data.mongodb.core.MongoTemplate">
<constructor-arg name="mongoDbFactory" ref="mongoDbFactory"/>
<constructor-arg name="mongoConverter" ref="mongoConverter" />
<property name="writeResultChecking" value="EXCEPTION" />
</bean>

Categories