繁体   English   中英

如何使用基本身份验证配置spring security以对数据库进行身份验证?

[英]How do you configure spring security to authenticate against a database using basic auth?

我用Java编写了13周的时间,正在开发一个RESTful Web服务。 后端已完成,现在我正在创建一个UI。 其中一个要求是用户使用http basic登录。 我已将此配置为当用户导航到页面时弹出对话框出现的位置,您可以输入我所拥有的一个硬编码用户并将其登录。但我真正需要它做的是验证用户是否在一个数据库。 我已经广泛搜索,试图找到一种方法来配置它来验证数据库,但无济于事。 这是我的假用户的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实体,其中包含firstname,lastname,email,password,activeStatus和timezone。 电子邮件是用户的用户名。 任何和所有的帮助将不胜感激。

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM