0

非常简单的场景:我正在使用 Spring 和 Spring Security,并且我试图将一个属性注入到我的 AuthenticationManager 使用的 AuthenticationProvider 中。我只是想到我可以将它连接到 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");
}
}

在我的控制器中,这失败了,因为 userStore 在 TwitterAuthenticationProvider 中是一个空引用:

Authentication authentication = this.authenticationManager.authenticate(authToken);

这是确切的错误:

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)

编辑:我正在使用自动装配,它适用于其他事情。如果我在 MVC 控制器中自动装配 AppEngineUserStore,它每次都会在那里。

4

2 回答 2

1
I just had the thought that I might be able to wire it up in the applicationContext, but not sure if that would work.

这会奏效。您可以在应用程序上下文中指定属性的连接。但是,这与自动装配不同(这是您的AuthenticationProvider.

如果您希望自动装配 AppEngineUserStore,您需要在您的应用程序上下文中注册它并打开自动装配。这可以按如下方式完成:

<context:annotation-config />

如果您使用,Spring 还可以扫描 bean 以自动装配:

<context:component-scan base-package="com.my.package" />

这将自动打开自动装配

于 2012-10-19T19:24:27.527 回答
0

如果您使用的是 autowired,您会说您在 spring-context 中使用注解。

<context:annotation-config />

否则你需要做基于 xml 的 DI。

于 2012-10-19T19:24:52.297 回答