AdSense

網頁

2018/3/12

Spring Security No bean named 'springSecurityFilterChain' available 錯誤

如要透過xml配置檔來使用Spring Security,則第一步是在web.xml加入下面的filter

<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>

但如果只設定上面,但在Spring的設定檔中沒加入<http>就會出現
Spring Security No bean named 'springSecurityFilterChain' available 錯誤。

不過一旦設定了<http>,還必須設定其他相關的配置,否則仍然會報錯。


另外也有可能Spring根本找不到你的設定檔。Spring Security的設定檔預設是使用/WEB/INF目錄下的applicationContext.xml

若要更改配置檔的名稱或位置,則web.xml可加入ContextLoaderListener來設定contextConfigLocation

<!-- Spring Security config file location -->
<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>classpath:applicationContext.xml</param-value><!-- 將Spring預設配置檔的位置改在classpath下,檔案名稱一樣叫錯applicationContext.xml -->
</context-param>

<!-- 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>

要注意的是Spring或Spring Security的ApplicationContext與Spring MVC的DispathcerServletWebApplicationContext不同。ApplicationContext下可有多個WebApplicationContext

而且Spring Security和Spring MVC預設的設定檔名稱不同,Spring Security是applicationContext.xml,Spring MVC是DispatcherServlet[servlet名稱]-servlet.xml


參考:

沒有留言:

AdSense