Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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());
    }
}
like image 521
Eric B. Avatar asked Nov 01 '22 20:11

Eric B.


1 Answers

MethodInvokingBean will be the de facto choice for add BouncyCastleProvider to java.security.Security since you won't need any exposure to your application context.

like image 66
Nick Avatar answered Nov 12 '22 13:11

Nick