Pretty simple scenario: I’m using Spring and Spring Security, and I’m trying to inject a property into the AuthenticationProvider used by my AuthenticationManager. I just had the thought that I might be able to wire it up in the applicationContext, but not sure if that would work.
applicationContext.xml:
<http use-expressions="true">
<intercept-url pattern="/" access="permitAll" />
<intercept-url pattern="/**" access="hasRole('USER')"/>
<form-login login-page="/index" />
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider ref="twitterAuthenticationProvider" />
</authentication-manager>
<beans:bean id="twitterAuthenticationProvider" class="com.bottlingday.model.auth.TwitterAuthenticationProvider" />
TwitterAuthenticationProvider:
public class TwitterAuthenticationProvider implements AuthenticationProvider
{
@Autowired
private AppEngineUserStore userStore;
static Log log = LogFactory.getLog(TwitterAuthenticationProvider.class.getName());
public void Test()
{
//userStore is always null here
log.info("test");
}
}
In my controller this fails because userStore is a null ref in TwitterAuthenticationProvider:
Authentication authentication = this.authenticationManager.authenticate(authToken);
Here’s the exact error:
java.lang.NullPointerException
at com.bottlingday.model.auth.TwitterAuthenticationProvider.authenticate(TwitterAuthenticationProvider.java:33)
at org.springframework.security.authentication.ProviderManager.authenticate(ProviderManager.java:156)
at com.bottlingday.auth.TwitterAuthController.OauthCallback(TwitterAuthController.java:129)
Edit: I am using autowiring, and it works fine for other things. If I autowire a AppEngineUserStore in a MVC controller, it will be there every time.
This would work. You can specify the wiring of your properties in your application context.
However, this is not the same as autowiring (Which is what you have in your
AuthenticationProvider.If you wish to autowire your AppEngineUserStore you will need to register it in your application context and turn on autowiring. This can be done as follows:
Spring can also scan for beans to autowire if you use:
This will automatically turn on autowiring