使用Spring Security的Spring 3应该很简单,但我知道我做的事情不对。我正在制作一个示例Spring3 Web App,我使用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);
有人可以告诉我为什么它不起作用!我需要帮助
答案 0 :(得分:0)
您需要通过查询从数据库中获取用户凭据。