2016-12-28 88 views
1

我在現有spring mvc項目中實現spring security。我曾使用XML來配置彈簧安全。我已經使用這個教程實施春季安全 http://www.mkyong.com/spring-security/spring-security-form-login-using-database/在現有Spring項目中配置用於集成Spring Security的數據源

在我的項目我有資源文件夾不到主要爲DB-源文件(MySQL_Datasource.xml)(Web應用程序之外)。在教程中實現spring安全的方式,數據源需要位於webapp文件夾下。我面臨着這個整合問題。

下面是我的項目結構和右側配置的捕捉。代碼web.xml,我已經評論了圖像中我必須定義數據源位置的行。

web.xml

這是春天的安全的代碼,其中數據源將用於

<beans:beans xmlns="http://www.springframework.org/schema/security" 
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/security 
    http://www.springframework.org/schema/security/spring-security-3.2.xsd"> 

    <!-- enable use-expressions --> 
    <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="/welcome" 
      authentication-failure-url="/login?error" 
      username-parameter="usr" 
      password-parameter="pwd" /> 
     <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 username,password, enabled from users where username=?" 
       authorities-by-username-query= 
        "select username, role from user_roles where username =? " /> 
     </authentication-provider> 
    </authentication-manager> 

</beans:beans> 

我這樣做的第一次。我需要幫助,以便我能完成這件事。

UPDATE:

MYSQL_DataSource.xml代碼:

<bean id="dataSource" class= "org.springframework.jdbc.datasource.DriverManagerDataSource">  
     <property name="driverClassName" value="com.mysql.jdbc.Driver"></property> 
     <property name="url" value="${jdbc.url}"></property> 
     <property name="username" value="${jdbc.username}"></property> 
     <property name="password" value="${jdbc.password}"></property> 
    </bean> 

    <bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
     <value>db.properties</value> 
    </property> 
    </bean> 

以下是db.properties值:

jdbc.url  = jdbc:mysql://localhost/bhaiyag_prod_grocery 
jdbc.username = newuser 
jdbc.password = kmsg 
+0

你的錯誤是什麼 – bmarkham

+0

感謝您的時間。您可以在圖像中的第23行的web.xml代碼中看到。我必須在下給出dataSource的路徑,以便dataSource可以在我的spring-security.xml中使用。我面臨着給路徑的問題。所以要麼我沒有正確地給路徑,要麼我錯誤地執行了 – RishiPandey

+0

對,但是你遇到了什麼問題使用你的方法 – bmarkham

回答

2

如果您的項目配置正確,src/main/resources文件夾將被打包在WEB-INF/classes下的項目建設期間。

所以,如果在項目/屬性Maven配置或部署組裝部分是好的,你應該在你的web.xml文件使用的路徑是這樣的:

<context-param> 
<param-name>contextConfigLocation</param-name> 
<param-value> 
    /WEB-INF/groceryapp-servlet.xml 
    /WEB-INF/spring-security.xml 
    /WEB-INF/classes/MySQL_DataSource.xml 
</param-value>  
</context-param> 

應該以這種方式工作。

一旦它的工作,看看這個問題和答案spring-scheduler-is-executing-twice和這個也web-application-context-root-application-context-and-transaction-manager-setup。在Mkyong的許多教程中,應用程序上下文都加載了兩次,而且我很確定它一旦開始工作就會發生與您的項目相同的情況。

當你groceryapp-servlet.xml已經被Spring MVC的調度的servlet加載,你可以嘗試只從contextConfigLocation的設置刪除它,只是這樣:

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

屬性加載問題

正確加載db.properties,請在DB config xml中嘗試此配置:

<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> 
    <property name="location"> 
     <value>classpath:/db.properties</value> 
    </property> 
    </bean> 
+0

感謝您的答覆。我更新了我的代碼,現在我的dataSouce爲db.properties文件提供了fileNotFoundException。當我硬編碼數據庫條目並從dataSource中刪除了db.properties條目時,編譯器沒有顯示錯誤。但我的ajax服務給405:post方法不支持 – RishiPandey

+0

好吧,我解決了這個post方法不支持的問題。但我確實需要幫助,爲什麼我的MySQL_DataSource.xml不能訪問db.properties – RishiPandey

+0

您應該至少發佈MySQL_DataSource.xml以查看發生了什麼 – jlumietu

0

您還可以指定相對於當前類路徑的上下文位置。確保資源文件夾位於你的類路徑中,如果是的話。然後你可以加載你的資源文件夾中的配置文件,如

<context-param> 
    <param-value>classpath:MySQL_DataSource.xml</param-value> 
</context-param> 
+0

不錯的選擇這個作品太多,但同樣的問題,因爲我評論上面jlumietu的答案 – RishiPandey