簡體   English   中英

Spring Security XML配置找不到我的dataSource bean定義

[英]Spring Security XML configuration can not find my dataSource beans definition

我的WEB-INF/web.xml執行以下操作-

1)加載我的servlet上下文WEB-INF/spring-web-servlet.xml

2)加載我的spring安全配置文件WEB-INF/spring-security.xml

3)添加springSecurityFilterChain過濾器以攔截傳入的URL

web.xml

<!-- Load spring-web-service.xml Servlet Definition -->

<servlet>
  <servlet-name>spring-web</servlet-name>
  <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  <load-on-startup>1</load-on-startup>
  <!-- Programmatically add the property sources for the current Spring Profile -->
  <init-param>
    <param-name>contextInitializerClasses</param-name>
    <param-value>com.galapagos.context.CustomEnvironmentApplicationContextInitializer</param-value>
  </init-param>
</servlet>

<servlet-mapping>
  <servlet-name>spring-web</servlet-name>
  <url-pattern>/</url-pattern>
</servlet-mapping>

<listener>
  <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<!-- Loads Spring Security config file -->

<context-param>
  <param-name>contextConfigLocation</param-name>
  <param-value>/WEB-INF/spring-security.xml</param-value>
</context-param>

<!-- Spring Security - Intercept incoming requests for authentication -->

<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-web-servlet.xml

servlet上下文實際上定義了連接到數據庫的dataSource bean(它還會預先加載屬性)

...

<!-- Load Properties -->

<beans:bean
  class="org.springframework.context.support.PropertySourcesPlaceholderConfigurer">
</beans:bean>

<!-- Database / JDBC -->

<beans:bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource">
  <beans:property name="driverClassName" value="${jdbc.driverClassName}" />
  <beans:property name="url" value="${jdbc.url}" />
  <beans:property name="username" value="${jdbc.username}" />
  <beans:property name="password" value="${jdbc.password}" />
</beans:bean>

...

spring-security.xml

問題是web.xml調出的第二個文件(我的WEB-INF/spring-security.xml文件)也使用數據庫連接,因為它查找存儲在數據庫中的角色和憑證

<http auto-config="true" use-expressions="true">
  <intercept-url pattern="/admin**" access="hasRole('ROLE_ADMIN')" />

  <!-- access denied page -->
  <access-denied-handler error-page="/403" />

  <form-login
    login-page="/login"
    default-target-url="/"
    authentication-failure-url="/login?error"
    username-parameter="email"
    password-parameter="password" />
  <logout logout-success-url="/login?logout"  />

  <!-- enable csrf protection -->
  <csrf/>
</http>

<!-- Select users and user_roles from database -->
<authentication-manager>
  <authentication-provider>
  <jdbc-user-service
    data-source-ref="dataSource"
    users-by-username-query="SELECT email, password FROM users WHERE email=?"
    authorities-by-username-query="SELECT email, role FROM user_roles WHERE email=?" />
  </authentication-provider>
</authentication-manager>

運行時出現錯誤-

Cannot resolve reference to bean 'dataSource' while setting bean property 'dataSource'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'dataSource' is defined

我最好的猜測是,因為dataSource是在第一個XML配置( WEB-INF/spring-web-servlet.xml )中定義的,因此無法在后一個XML配置( WEB-INF/spring-security.xml )中使用。

這是正確的嗎?

如果是的話,定義dataSource bean的最佳方法是什么,以便所有單個XML文件都可以根據需要調用它? 我假設我可能會添加一些其他組件,這些組件有時也需要數據庫訪問。

謝謝!

在web.xml中嘗試更改此設置

contextConfigLocation /WEB-INF/spring-security.xml,WEB-INF / spring-web-servlet.xml這將為您提供幫助。

暫無
暫無

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

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