gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 02:42:06 25 4
gpt4 key购买 nike

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

在我的项目中,我在 main 下的资源文件夹中(webapp 之外)有一个数据库源文件(MySQL_Datasource.xml)。而教程中Spring Security的实现方式,数据源需要位于webapp文件夹下。我面临着这个整合问题。

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

web.xml

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

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

更新:

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

最佳答案

如果您的项目配置正确,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,请在数据库配置 xml 中尝试此配置:

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

关于java - 配置数据源以将 Spring Security 集成到现有 Spring 项目中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41357235/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com