How to add Security provider in a Spring application using Javaconfig?

Friday, July 11, 2014

I'm trying to add BouncyCastle to my Spring application but I am not sure how to add the provider to the java.security.Security provider list using JavaConfig.


Using XML configuration, I can use the MethodInvokingFactoryBean similar the following:



<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="staticMethod" value="java.security.Security.addProvider"/>
<property name="arguments">
<list>
<bean class="org.bouncycastle.jce.provider.BouncyCastleProvider"/>
</list>
</property>
</bean>


However, I'm not sure of the right way to do this using JavaConfig. Should I still be using the MethodInvokingFactoryBean? I presumed since it is pure java, there would be a more direct approach. At the moment, I've added the directive to a @PostConstruct method in the JavaConfig object, but not too thrilled about it - it seems a little "hacky" to me:



@Configuration
public class AppConfig {
// other @Bean definitions

@PostConstruct
public void init(){
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
}
}






http://ift.tt/1y4p20Q