尽管仍然存在一些未解决的问题,但想出了如何使其工作的方法。
1)我仍然不知道为什么在
UserService实现时Spring上下文初始化失败
UserDetailsService。鉴于我没有看到它的用途,因为我使用的是custom
AuthenticationProvider,所以我删除了此实现,现在一切正常。据我所知(从我对Spring
Security参考文档的初次阅读中可以了解的内容),提供自定义
AuthenticationProvider或
UserDetailsService实现是排他性的选择。
2)正如一位响应者(@Sotirios
Delimanolis)所注意到的那样,我是
ApplicatinoAuthenticationProvider手工实例化的,由于它不是由Spring管理的,因此该实例不会
UserService自动连接到该实例。基于此,我更改
WebSecurityConfig为获得一个自动装配的实例,
ApplicationAuthenticationProvider如下所示:
@Configuration@EnableWebSecuritypublic class WebSecurityConfig extends WebSecurityConfigurerAdapter { @Autowired private ApplicationAuthenticationProvider authenticationProvider; @Override protected void configure(AuthenticationManagerBuilder authManagerBuilder) throws Exception { authManagerBuilder.authenticationProvider(authenticationProvider); }}这还不够,因为
ApplicationAuthenticationProvider没有被自动连接到中
WebSecurityConfig。基于此链接,Spring
Security 3.1.3@Autowired在使用WebApplicationInitializer时不起作用,我注意到这是因为安全配置也应具有组件扫描声明。添加
@ComponentScan(basePackages= {"com.mypackage"})来WebSecurityConfig解决问题。



