2016-12-28 89 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