【发布时间】: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