java – 在eclipse之外的tomcat上部署时重复注册springSecurityFilterChain

前端之家收集整理的这篇文章主要介绍了java – 在eclipse之外的tomcat上部署时重复注册springSecurityFilterChain前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我对弹簧有点新意,仍然对所有配置感到困惑.我参加了几个不同的教程,似乎每个人都做了不同的事情.我有一个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中没有帮助.

谢谢!

picture of full stack trace

下面是在Eclipse中运行时完全正常运行的代码.

web.xml中

SecuirtyConfig.java

@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

@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文件

301_4@

但是,我建议先将当前配置工作,然后将其投入混合.

猜你在找的Spring相关文章