简体   繁体   English

配置数据源以将Spring Security集成到现有的Spring项目中

[英]Configure datasource for integrating Spring Security in existing Spring project

I am implementing spring security in an existing spring mvc project. 我正在现有的spring mvc项目中实现spring安全性。 I had used xml to configure the spring security. 我曾使用xml来配置Spring安全性。 I have used this tutorial for implementing spring security http://www.mkyong.com/spring-security/spring-security-form-login-using-database/ 我已经使用本教程来实现Spring Security http://www.mkyong.com/spring-security/spring-security-form-login-using-database/

In my project I have a db-source file( MySQL_Datasource.xml ) in resources folder just under main (outside of webapp). 在我的项目中,位于主文件夹(位于webapp外部)下的resources文件夹中,有一个db-source文件( MySQL_Datasource.xml )。 And the way spring security is implemented in tutorial, the datasource needs to be under webapp folder. 在教程中实现Spring Security的方式中,数据源必须位于webapp文件夹下。 I am facing this problem of integration. 我面临着这个整合问题。

Below is the snap of my project structure and on the right side config. 下面是我的项目结构和右侧配置的快照。 code of web.xml , I have commented on the line in image where i have to define my dataSource location. web.xml代码,我已经在图像中的行上注释了,我必须在其中定义dataSource的位置。

web.xml

This is code of spring security where dataSource will be used 这是Spring Security的代码,其中将使用dataSource

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

I am doing this first time. 我第一次这样做。 I need help so that I can get this done. 我需要帮助,以便可以完成此任务。

UPDATE: 更新:

MYSQL_DataSource.xml code: 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>

and below is the db.properties values: 下面是db.properties值:

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

If your project is correctly configured, src/main/resources folder will be packaged during project build under WEB-INF/classes . 如果您的项目配置正确,则在项目构建过程中src/main/resources文件夹将打包在WEB-INF/classes

So, if maven configuration or deployment-assembly section in project/properties is Ok, the path that you should use in your web.xml is like this: 因此,如果“项目/属性”中的“ 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>

It should work this way. 它应该以这种方式工作。

Once it works, have a look at this question and answers spring-scheduler-is-executing-twice and this one too web-application-context-root-application-context-and-transaction-manager-setup . 一旦工作,请看一下这个问题并两次回答spring-scheduler-is-executing两次 ,这也是一个web-application-context-root-application-context-and-transaction-manager-setup In many of the Mkyong's tutorials the application context is loading twice, and I'm pretty sure it would happen the same with your project once it starts working. 在Mkyong的许多教程中,应用程序上下文都加载了两次,而且我敢肯定,一旦项目开始工作,它将与您的项目发生相同的情况。

As your groceryapp-servlet.xml is already loaded by Spring MVC's dispatcher servlet, you could try just removing it from contextConfigLocation setting, just this way: 由于Spring MVC的调度程序servlet已经加载了groceryapp-servlet.xml ,因此您可以尝试通过以下方式将其从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>

Properties loading problem : 属性加载问题

To load correctly the db.properties, try this config in DB config xml: 要正确加载db.properties,请在DB config xml中尝试以下配置:

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

You can also specify context location relatively to current classpath. 您还可以相对于当前类路径指定上下文位置。 Make sure the resources folder is on your classpath and if it is. 确保资源文件夹位于类路径上(如果存在)。 Then you can load the configuration file in your resources folder like, 然后,您可以将配置文件加载到资源文件夹中,例如,

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM