Spring3 Security 使用 JDBC 并停止工作

发布于 2024-10-27 06:23:23 字数 2350 浏览 1 评论 0原文

使用 Spring Security 的 Spring 3 应该很容易,但我知道我做的事情不对。我正在制作一个示例 Spring3 Web 应用程序,并在我的 Web 应用程序中使用了 Spring Security。经过两周的调试但没有让它工作,我发现它是我的 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);

有人可以告诉我为什么它不起作用!我需要帮助

Spring 3 using Spring Security should be easy but I know I am doing something not right. I am making a sample Spring3 Web App and I used Spring Security for my web app. After two weeks of debug and not getting it working I found out it was something in my web.xml and now it works great. But I need to move the users from the XML file to a MySQL database. So I remove the users from the XML file and change my file to look like the following:

<?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>

I made my SQL database with the following:

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);

Can someone please tell me why it is not working! I need help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2024-11-03 06:23:23

您需要通过查询从数据库获取用户凭据。

you need to get the user credentials from the data base through a query.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文