【问题标题】:How do you configure spring security to authenticate against a database using basic auth?您如何配置 Spring Security 以使用基本身份验证对数据库进行身份验证?
【发布时间】:2012-06-05 21:38:07
【问题描述】:

我已经 13 周开始使用 Java 进行编码,并且正在开发一个 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 以针对数据库而不是我拥有的虚拟用户进行身份验证的指导?我在数据库中有一个用户实体,其中包含名字、姓氏、电子邮件、密码、activeStatus 和时区。电子邮件是用户的用户名。任何和所有的帮助将不胜感激。

【问题讨论】:

    标签: java spring-security basic-authentication


    【解决方案1】:

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

    【讨论】:

    • 谢谢 Dani,我会朝那个方向看,然后从那里开始。我在一个网站上找到了一些关于这样做的信息:theserverside.com/tip/…
    【解决方案2】:

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

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

    是的。所有这些都可以在documentation 中找到。

    【讨论】:

    • 谢谢,我最终决定这样做,使用我现有的不同数据库结构并为角色创建一个单独的表。我现在可以工作了。
    猜你喜欢
    • 2016-01-11
    • 2019-02-12
    • 2011-10-17
    • 2015-02-06
    • 1970-01-01
    • 2011-02-11
    • 2016-03-20
    • 1970-01-01
    • 2012-11-27
    相关资源
    最近更新 更多