java - 使用 @EnableWebSecurity 时,至少有一个非空 i..... 作为 @Bean。提示尝试扩展 WebSecurityConfigurerAdapter

标签 java spring-mvc spring-security

这是我花了几个小时尝试解决后无法弄清楚的问题。我正在研究带有java配置的spring security。我提出了以下异常,但不知道我应该做什么。

  Oct 22, 2015 6:00:57 AM org.apache.catalina.core.ApplicationContext log
SEVERE: StandardWrapper.Throwable
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'springSecurityFilterChain' defined in class org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration: Instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.servlet.Filter org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.springSecurityFilterChain() throws java.lang.Exception] threw exception; nested exception is java.lang.IllegalStateException: At least one non-null instance of WebSecurityConfigurer must be exposed as a @Bean when using @EnableWebSecurity. Hint try extending WebSecurityConfigurerAdapter

Caused by: org.springframework.beans.factory.BeanDefinitionStoreException: Factory method [public javax.servlet.Filter org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.springSecurityFilterChain() throws java.lang.Exception] threw exception; nested exception is java.lang.IllegalStateException: At least one non-null instance of WebSecurityConfigurer must be exposed as a @Bean when using @EnableWebSecurity. Hint try extending WebSecurityConfigurerAdapter
at org.springframework.beans.factory.support.SimpleInstantiationStrategy.instantiate(SimpleInstantiationStrategy.java:188)
at org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:586)
... 33 more

Caused by: java.lang.IllegalStateException: At least one non-null instance of WebSecurityConfigurer must be exposed as a @Bean when using @EnableWebSecurity. Hint try extending WebSecurityConfigurerAdapter
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration.springSecurityFilterChain(WebSecurityConfiguration.java:90)
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration$$EnhancerBySpringCGLIB$$1c09b217.CGLIB$springSecurityFilterChain$1(<generated>)
    at org.springframework.security.config.annotation.web.configuration.WebSecurityConfiguration$$EnhancerBySpringCGLIB$$1c09b217$$FastClassBySpringCGLIB$$7b0370bc.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invokeSuper(MethodProxy.java:228)

安全配置为

@Configuration
@EnableWebSecurity
public class AppSecurityConfiguration extends WebSecurityConfigurerAdapter {

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .authorizeRequests()
                .antMatchers("/allmessages*").hasAnyRole("USER", "ANONYMOUS")
                .antMatchers("/postmessages*").hasRole("USER")
                .antMatchers("/deletemessages*").hasRole("ADMIM")
            .and()
                .httpBasic();

    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication()
                .withUser("tadtdab").password("tadtab").authorities("ROLE_ADMIN","ROLE_USER")
                .and().withUser("tadi").password("tadi").authorities("ROLE_USER");
    }
}

最佳答案

我遇到了同样的问题,读完这篇文章后我找到了答案:它在堆栈跟踪中:如果你转到消息的末尾,你可以找到这个:

Hint try extending WebSecurityConfigurerAdapter

所以我刚刚找到了我的 webSecurityConfig 类并添加了

extends WebSecurityConfigurerAdapter 

我的服务器正确启动

关于java - 使用 @EnableWebSecurity 时,至少有一个非空 i..... 作为 @Bean。提示尝试扩展 WebSecurityConfigurerAdapter,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33278551/

相关文章:

java - 正则表达式查找最后一个字符是字母

java - 如何在 PIG 中转置列和行

java - Spring中不同上下文的目的和关系

grails - Grails Spring Security插件:直接从 Controller 登录

java - Spring Security + LDAP + CustomLdapAuthoritiesPopulator + RememberMe

java - 我的零钱计算器有缺陷?

java - SecureRandom 实例创建需要很长时间才能完成

java - 使用 Spring Repo 更新 OneToOne 列

java - 处理来自 @Service 类的 JSP 页面异常

java - 使用 spring-data-jpa 和 spring-mvc 过滤数据库行