簡體   English   中英

org.springframework.beans.factory.NoSuchBeanDefinitionException:未定義名為“ springSecurityFilterChain”的bean-基於Java的配置

[英]org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined - Java Based Configuration

嘗試解決org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined在基於Java的配置中org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined

web.xml

<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xmlns="http://java.sun.com/xml/ns/javaee"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0">

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/spring/root-context.xml</param-value>
    </context-param>
    <!-- Creates the Spring Container shared by all Servlets and Filters -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>appServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </init-param>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>com.examples.config.WebAppConfig</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>appServlet</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

    <!-- Spring Security -->
    <filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/views/error/404.jsp</location>
    </error-page>


</web-app>

基於Java的WebAppCinfig:

@EnableWebMvc
@Configuration
@Import(value = SecurityConfig.class)
@ComponentScan(basePackages = {"com.examples"})
@ImportResource({ "classpath:spring-security-config.xml" })
public class WebAppConfig extends WebMvcConfigurerAdapter {

    public WebAppConfig() {
        super();
    }

    @Override
    public void addViewControllers(final ViewControllerRegistry registry) {
        super.addViewControllers(registry);

        registry.addViewController("/anonymous.html");

        registry.addViewController("/login.html");
        registry.addViewController("/homepage.html");
    }

    @Bean
    public ViewResolver viewResolver() {
        final InternalResourceViewResolver bean = new InternalResourceViewResolver();

        bean.setViewClass(JstlView.class);
        bean.setPrefix("/WEB-INF/views/");
        bean.setSuffix(".jsp");

        return bean;
    }
}

用於安全性的SecurityConfig.java文件:

@Configuration
@ImportResource({ "classpath:spring-security-config.xml" })
public class SecurityConfig {

    public SecurityConfig() {
        super();
    }

}

最后是WebAppConfig.java類:

<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xmlns:beans="http://www.springframework.org/schema/beans"
             xmlns="http://www.springframework.org/schema/security"
             xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security.xsd">

    <http auto-config="true">
        <intercept-url pattern="/anonymous*" access="isAnonymous()"/>
        <intercept-url pattern="/login*" access="permitAll"/>
        <intercept-url pattern="/**" access="isAuthenticated()"/>

        <form-login login-page='/login.html' login-processing-url="/perform_login"
                    default-target-url="/homepage.html" authentication-failure-url="/login.html?error=true"
                    always-use-default-target="true"/>

        <logout logout-url="/perform_logout" delete-cookies="JSESSIONID"
                success-handler-ref="customLogoutSuccessHandler"/>

    </http>

    <beans:bean name="customLogoutSuccessHandler"
                class="com.examples.config.CustomLogoutSuccessHandler"/>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="user1" password="pass1" authorities="ROLE_USER"/>
                <user name="user2" password="pass2" authorities="ROLE_USER"/>
            </user-service>
        </authentication-provider>
    </authentication-manager>
</beans:beans>

現在,我得到org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined在我正在研究的Java基礎Spring Security項目中org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'springSecurityFilterChain' is defined 事情在注釋配置上運行良好,並且可以很好地進行測試。 由於某些原因,Spring在嘗試完成Spring Security Form Login示例時會拋出這樣的錯誤。

另外,在spring-security-config.xml中,嘗試將<http use-expressions="true">更改為<http auto-config="true">但仍然無法解決沒有名為'springSecurityFilterChain' Bean的異常。

在這方面的任何幫助將不勝感激。 謝謝,

Spring容器正在嘗試在應用程序上下文而不是Web應用程序上下文中查找springSecurityFilterChain bean。 將spring安全配置更改為包含在根上下文中。

請提供最少的彈簧安全性配置

http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-安全3.0.3xsd “>

<http auto-config='true'>
    <intercept-url pattern="/**" access="ROLE_USER" />
</http>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM