簡體   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