我在现有spring mvc项目中实现spring security。我曾使用XML来配置弹簧安全。我已经使用这个教程实施春季安全 http://www.mkyong.com/spring-security/spring-security-form-login-using-database/在现有Spring项目中配置用于集成Spring Security的数据源
在我的项目我有资源文件夹不到主要为DB-源文件(MySQL_Datasource.xml
)(Web应用程序之外)。在教程中实现spring安全的方式,数据源需要位于webapp文件夹下。我面临着这个整合问题。
下面是我的项目结构和右侧配置的捕捉。代码web.xml
,我已经评论了图像中我必须定义数据源位置的行。
这是春天的安全的代码,其中数据源将用于
<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>
我这样做的第一次。我需要帮助,以便我能完成这件事。
UPDATE:
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
你的错误是什么 – bmarkham
感谢您的时间。您可以在图像中的第23行的web.xml代码中看到。我必须在下给出dataSource的路径,以便dataSource可以在我的spring-security.xml中使用。我面临着给路径的问题。所以要么我没有正确地给路径,要么我错误地执行了 –
RishiPandey
对,但是你遇到了什么问题使用你的方法 – bmarkham