如何配置Spring Security使用基本身份验证对数据库进行身份验证?

9

我已经学习Java编程13周并正在开发一个RESTful的Web服务。现在,我的后端处理完了,接下来我要开始创建用户界面。其中一个要求是要使用http基础认证进行用户登录。我已经将其配置完成,当用户导航到页面时,会弹出对话框,您可以输入一个我预先设置好的用户以进行登录。但是,我真正需要的是检查数据库中的用户信息是否匹配。我已经进行了广泛的搜索,试图找到一种配置方法来验证数据库中的用户,但无济于事。这是我的spring-security.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.1.xsd">

    <!-- <http auto-config="true"> <intercept-url pattern="/welcome*" access="ROLE_USER" 
        /> <form-login login-page="/login" default-target-url="/welcome" authentication-failure-url="/loginfailed" 
        /> <logout logout-success-url="/logout" /> </http> -->

    <http>
        <intercept-url pattern="/*" access="ROLE_USER" />
        <http-basic />
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="tmain" password="123456" authorities="ROLE_USER" />
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>

以下是我认为与我的web.xml文件设置相关的唯一相关信息。

<filter>
        <filter-name>springSecurityFilterChain</filter-name>
        <filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>springSecurityFilterChain</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

有人能指导我如何配置Spring Security来使用数据库进行身份验证,而不是使用我拥有的虚拟用户吗? 我在数据库中有一个名为User的实体,其中包含名字、姓氏、电子邮件、密码、活动状态和时区。 电子邮件是用户的用户名。 非常感谢任何帮助。

2个回答

3
您的身份验证提供程序应该像这样:

您的身份验证提供程序应该如下所示:

<authentication-manager>
    <authentication-provider>
        <jdbc-user-service data-source-ref="dataSource" />
    </authentication-provider>
</authentication-manager>

默认实现需要以下这些表:

create table users(
    username varchar_ignorecase(50) not null primary key,
    password varchar_ignorecase(50) not null,
    enabled boolean not null);

create table authorities (
    username varchar_ignorecase(50) not null,
    authority varchar_ignorecase(50) not null,
    constraint fk_authorities_users foreign key(username) references users(username));
    create unique index ix_auth_username on authorities (username,authority);

你可以拥有不同的数据库结构,并使用以下方式:

您可以拥有不同的数据库结构,并使用类似以下方式:

<jdbc-user-service data-source-ref="dataSource" authorities-by-username-query="select username,authority from users where username=?"/>

不要忘记创建dataSource bean...

而且.. 所有的这些都可以在文档中找到。


1
谢谢,我最终选择了这个方案,利用我已经准备好的不同数据库结构,并为角色创建了一个单独的表。现在它可以正常工作了。 - Tyler Main

1

您需要使用 jdbc-user-service 而不是 user-service。您可以先阅读 文档


谢谢Dani,我会朝那个方向寻找并从那里开始。我已经找到了一个网站,其中包含有关如何进行此操作的一些信息:http://www.theserverside.com/tip/-Spring-Security-Customizing-Your-User-and-Authorization-in - Tyler Main

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接