我对弹簧有点新意,仍然对所有配置感到困惑.我参加了几个不同的教程,似乎每个人都做了不同的事情.我有一个spring应用程序,可以使用tomcat插件通过Eclipse运行.但是当将war文件导出到tomcat本身时,tomcat无法启动和抛出
严重:ContainerBase.addChild:start org.apache.catalina.LifecycleException:启动组件失败
引起:java.lang.IllegalStateException:’springSecuirtyFilterChain’的重复过滤器注册.检查以确保过滤器仅配置一次!
查看完整堆栈跟踪的图片.
在web.xml中注释掉springSecurityFilterChain之后,无论dataSource是否自动装配都会产生一个或两个错误.
>如果dataSource是自动装配的,那么我只是得到一个错误说创建
bean securityConfig失败,找不到bean
依赖.
>如果我让dataSource没有自动装配(就像我在Eclipse中工作的代码那样),那么我得到一个IllegalArgumentException:属性’dataSource’是必需的.
另外,为了不获取多个ContextLoader定义错误,我必须在web xml中注释掉ContextLoaderListener.
从我看到的问题在于使用xml和java进行配置,但我无法确切地指出错误.
我发现了一个类似的问题,但无法解决我的问题.
Where do I define `springSecurityFilterChain` bean?
添加一个指向我的securityConfig的bean类放在spring-security.xml中没有帮助.
谢谢!
下面是在Eclipse中运行时完全正常运行的代码.
web.xml中
@H_301_35@
SecuirtyConfig.java
@H_301_35@@Configuration @EnableWebSecurity public class SecurityConfig extends WebSecurityConfigurerAdapter { DataSource dataSource; /* @Autowired public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { // auth.inMemoryAuthentication().withUser("user").password("password").roles("USER"); } */ @Autowired public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception { auth.jdbcAuthentication().dataSource(dataSource) .usersByUsernameQuery("select username,password,enabled from test_users where username=?") .authoritiesByUsernameQuery("select username,role from test_user_roles where username=?"); } @Override protected void configure(HttpSecurity http) throws Exception { http.csrf().disable() .authorizeRequests() .antMatchers("/res/**").permitAll() .anyRequest().authenticated() .and() .formLogin() .loginPage("/loginDatabase.html") .permitAll(); } }
AppConfig.java
@H_301_35@@EnableWebMvc @Configuration @ComponentScan({"security.spring"}) @Import({ SecurityConfig.class }) public class AppConfig { @Bean(name = "dataSource") public DriverManagerDataSource dataSource() { DriverManagerDataSource driverManagerDataSource = new DriverManagerDataSource(); driverManagerDataSource.setDriverClassName("com.MysqL.jdbc.Driver"); driverManagerDataSource.setUrl("****"); driverManagerDataSource.setUsername("**"); driverManagerDataSource.setPassword("**"); return driverManagerDataSource; } @Bean public InternalResourceViewResolver viewResolver() { InternalResourceViewResolver viewResolver = new InternalResourceViewResolver(); viewResolver.setViewClass(JstlView.class); viewResolver.setPrefix("/WEB-INF/pages/"); viewResolver.setSuffix(".jsp"); return viewResolver; } }
弹簧security.xml文件
@H_301_35@
web.xml中的过滤器行告诉servlet引擎(Tomcat)加载该过滤器,但该过滤器的实例是在Spring上下文中配置的.问题是Spring上下文无法启动,因为springSecurityFilterChain有两个配置.拿出一个,你将取得进步.
您在XML文件中的配置似乎更全面和细粒度,但我建议将该配置移动到Java文件并删除XML文件.
删除重复配置后,您可能仍会遇到错误,但您应该能够找到此网站上的解决方案,或者随意发布单独的问题!
注意:也可以让Spring自动为您注册过滤器链,因此您无需在web.xml中定义它.请参阅此处了解如何执行此操作:
http://www.mkyong.com/spring-security/spring-security-hello-world-annotation-example/
但是,我建议先将当前配置工作,然后将其投入混合.