【问题标题】:Configure datasource for integrating Spring Security in existing Spring project配置数据源以在现有 Spring 项目中集成 Spring Security
【发布时间】:2016-12-29 17:56:27
【问题描述】:

我正在现有的 spring mvc 项目中实现 spring security。我曾使用 xml 来配置 spring 安全性。我已经使用本教程来实现 spring 安全性 http://www.mkyong.com/spring-security/spring-security-form-login-using-database/

在我的项目中,我在 main 下(webapp 外部)的资源文件夹中有一个 db-source 文件(MySQL_Datasource.xml)。教程中实现spring security的方式,数据源需要在webapp文件夹下。我正面临这个集成问题。

下面是我的项目结构和右侧配置的快照。 web.xml 的代码,我已经评论了图像中我必须定义我的数据源位置的行。

这是使用dataSource的spring security代码

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

我是第一次这样做。我需要帮助才能完成这项工作。

更新:

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

【问题讨论】:

  • 你的错误是什么
  • 感谢您的宝贵时间。您可以在图像中第 23 行的 web.xml 代码中看到。我必须在 下提供我的数据源的路径,以便可以在我的 spring-security.xml 中使用数据源。我面临给出路径的问题。所以要么我没有正确地给出路径,要么我的实施方式错误
  • 对,但是你用你的方法遇到了什么问题
  • 找不到我的数据源(因为我没有给出正确的路径)。当我在 webapp Spring 安全工作下移动我的数据源时,但我不能这样做,因为我所有的 bean 都在数据源中定义,我必须在应用程序上下文中使用它们
  • 你能发布你的堆栈跟踪吗

标签: java spring-mvc spring-security


【解决方案1】:

如果您的项目配置正确,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>

【讨论】:

  • 感谢您的回答。我已经更新了我的代码,现在我的 dataSouce 正在为 db.properties 文件提供 fileNotFoundException。当我硬编码 db 条目并从 dataSource 中删除 db.properties 条目时,编译器没有显示错误。但我的 ajax 服务给出 405: post method not supported
  • 好的,我解决了这个帖子方法不支持的问题。但我确实需要帮助,为什么我的 MySQL_DataSource.xml 没有访问 db.properties
  • 你至少应该发布你的 MySQL_DataSource.xml 看看发生了什么
  • 查看相关更新部分。我已经添加了dataSource和db.properties的相关代码
  • @RishiPandey,看看我在“属性加载问题”中的回答
【解决方案2】:

您还可以指定相对于当前类路径的上下文位置。确保资源文件夹在你的类路径上,如果是的话。然后您可以在资源文件夹中加载配置文件,例如,

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

【讨论】:

  • 不错的选项,这也有效,但问题与我上面对 jlumietu 的回答所评论的问题相同
猜你喜欢
  • 1970-01-01
  • 2022-06-12
  • 2012-10-28
  • 2023-01-30
  • 2014-10-31
  • 2018-02-23
  • 2012-01-23
  • 2011-01-08
  • 2014-01-24
相关资源
最近更新 更多