Java 配置用于在现有Spring项目中集成Spring安全性的数据源
我正在现有的SpringMVC项目中实现SpringSecurity。我使用xml来配置spring安全性。我使用本教程实现了spring安全性 在我的项目中,我有一个db源文件(Java 配置用于在现有Spring项目中集成Spring安全性的数据源,java,spring-mvc,spring-security,Java,Spring Mvc,Spring Security,我正在现有的SpringMVC项目中实现SpringSecurity。我使用xml来配置spring安全性。我使用本教程实现了spring安全性 在我的项目中,我有一个db源文件(MySQL\u Datasource.xml)在main下面的resources文件夹中(在webapp之外)。按照本教程中spring安全性的实现方式,数据源需要位于webapp文件夹下。我面临着这个融合的问题 下面是我的项目结构和右侧配置的快照。在web.xml的代码中,我已经在图像中的行上进行了注释,在该行中我
MySQL\u Datasource.xml
)在main下面的resources文件夹中(在webapp之外)。按照本教程中spring安全性的实现方式,数据源需要位于webapp文件夹下。我面临着这个融合的问题
下面是我的项目结构和右侧配置的快照。在web.xml
的代码中,我已经在图像中的行上进行了注释,在该行中我必须定义我的数据源位置
这是将使用数据源的spring安全代码
<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>
如果项目配置正确,
src/main/resources
文件夹将在项目构建期间打包在WEB-INF/classes
下
因此,如果project/properties中的maven configuration或deployment assembly部分没有问题,那么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>
属性加载问题:
要正确加载db.properties,请在db config xml中尝试此配置:
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:/db.properties</value>
</property>
</bean>
类路径:/db.properties
您还可以指定相对于当前类路径的上下文位置。确保资源文件夹在类路径上,如果在类路径上。然后您可以将配置文件加载到资源文件夹中,如下所示:
<context-param>
<param-value>classpath:MySQL_DataSource.xml</param-value>
</context-param>
类路径:MySQL_DataSource.xml
你犯了什么错误感谢你的时间。您可以在图中第23行的web.xml代码中看到。我必须在下面给出数据源的路径,以便数据源可以在我的spring-security.xml中使用。我面临着让路的问题。所以,要么我没有正确地给出路径,要么我已经走错了实现的方法,对吧,但是您在使用您的方法时遇到了什么问题?我的数据源找不到(因为我没有给出正确的路径)。当我在webapp下移动数据源时,Spring安全性可以工作,但我不能这样做,因为我的所有bean都在数据源中定义,我必须在应用程序上下文中使用它们。你能发布你的stacktraceThanks以获得答案吗。我已经更新了代码,现在我的dataSouce为db.properties文件提供了fileNotFoundException。当我硬编码db条目并从数据源中删除db.properties条目时,编译器并没有显示任何错误。但是我的ajax服务给出了405:post方法不受支持。我解决了这个post方法不受支持的问题。但是我确实需要帮助,为什么我的MySQL_DataSource.xml不访问db.properties你应该发布你的MySQL_DataSource.xml,至少看看发生了什么请参阅相关的更新部分。我已经添加了dataSource和db的相关代码。properties@RishiPandey,看看我在“属性加载问题”中的答案,这个不错的选项也可以,但问题与我在上面对jlumietu答案的评论相同
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>
/WEB-INF/spring-security.xml
/WEB-INF/classes/MySQL_DataSource.xml
</param-value>
</context-param>
<bean class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location">
<value>classpath:/db.properties</value>
</property>
</bean>
<context-param>
<param-value>classpath:MySQL_DataSource.xml</param-value>
</context-param>