【发布时间】:2011-07-24 02:06:03
【问题描述】:
使用 Spring Security 的 Spring 3 应该很容易,但我知道我做的不对。我正在制作一个示例 Spring3 Web 应用程序,并将 Spring Security 用于我的 Web 应用程序。经过两周的调试并且没有让它工作,我发现它在我的 web.xml 中,现在它工作得很好。但我需要将用户从 XML 文件移动到 MySQL 数据库。所以我从 XML 文件中删除用户并将我的文件更改为如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<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"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/jdbc
http://www.springframework.org/schema/jdbc/spring-jdbc-3.0.xsd
http://www.springframework.org/schema/security
http://www.springframework.org/schema/security/spring-security-3.0.xsd">
<http auto-config="true">
<intercept-url pattern="/friends/**" access="ROLE_USER" />
<intercept-url pattern="/articles/**" access="ROLE_USER" />
<intercept-url pattern="/**" access="IS_AUTHENTICATED_ANONYMOUSLY" />
</http>
<authentication-manager alias="authenticationManager">
<authentication-provider>
<jdbc-user-service data-source-ref="dataSource"/>
</authentication-provider>
</authentication-manager>
</beans:beans>
我使用以下内容创建了我的 SQL 数据库:
CREATE TABLE IF NOT EXISTS `authorities` (
`username` varchar(50) NOT NULL,
`authority` varchar(50) NOT NULL,
UNIQUE KEY `authorities_idx_1` (`username`,`authority`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `authorities` (`username`, `authority`) VALUES
('admin', 'ROLE_ADMIN'),
('admin', 'ROLE_USER'),
('guest', 'ROLE_USER');
CREATE TABLE IF NOT EXISTS `users` (
`username` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`enabled` tinyint(1) NOT NULL,
PRIMARY KEY (`username`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
INSERT INTO `users` (`username`, `password`, `enabled`) VALUES
('admin', 'admin', 1),
('guest', 'guest', 1),
('john', 'sabrina', 1);
谁能告诉我为什么它不起作用!我需要帮助
【问题讨论】:
-
您需要接受之前问题的一些答案。
标签: java spring spring-mvc spring-security