簡體   English   中英

Spring Security:創建用戶

[英]Spring Security: Create Users

我在應用程序中使用spring-web(4.0.3.RELEASE)和spring-security-web(3.2.3.RELEASE)。 我的目標是在應用程序啟動時自動創建一些用戶。 但是,當我使用“security:user ...”標簽添加用戶時,它既不會創建用戶,也不會抱怨

Configuration problem: authentication-provider element cannot have 
child elements when used with 'ref' attribute

截至目前,我的security-config.xml文件看起來像這樣。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
   xmlns:context="http://www.springframework.org/schema/context"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns:security="http://www.springframework.org/schema/security"
   xmlns:jpa="http://www.springframework.org/schema/data/jpa"
   xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
    http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security-3.2.xsd">
<security:http auto-config='true'>
    <security:intercept-url pattern="/messagebroker/amf" access="ROLE_USER" />
    <security:intercept-url pattern="/login.json" access="ROLE_ANONYMOUS" />
</security:http>
<jpa:repositories base-package="com.thing.orlando.repositories" />

 <!--authentication manager and password hashing-->
<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider ref="daoAuthenticationProvider">
        <security:user-service>
            <security:user name="admin" password="password" authorities="ROLE_USER, ROLE_ADMIN" />
            <security:user name="user" password="password" authorities="ROLE_USER" />
        </security:user-service>
    </security:authentication-provider>
</security:authentication-manager>

<bean id="daoAuthenticationProvider" class="org.springframework.security.authentication.dao.DaoAuthenticationProvider">
    <property name="userDetailsService" ref="userDetailsService"/>
    <property name="saltSource">
        <bean class="org.springframework.security.authentication.dao.ReflectionSaltSource">
            <property name="userPropertyToUse" value="email"/>
        </bean>
    </property>
    <property name="passwordEncoder" ref="passwordEncoder"/>
</bean>

<bean id="userDetailsService"  name="userAuthenticationProvider"
      class="com.dallas.orlando.services.CustomUserDetailsService"/>
<bean id="passwordEncoder" class="org.springframework.security.authentication.encoding.ShaPasswordEncoder">
    <constructor-arg index="0" value="256"/>
</bean>

我想知道創建用戶和填充我的數據庫的可接受方式是什么。

改為:

<!--authentication manager and password hashing-->
<security:authentication-manager alias="authenticationManager">
    <security:authentication-provider ref="daoAuthenticationProvider"/>
    <security:authentication-provider    
        <security:user-service>
            <security:user name="admin" password="password" authorities="ROLE_USER, ROLE_ADMIN" />
            <security:user name="user" password="password" authorities="ROLE_USER" />
        </security:user-service>
</security:authentication-manager>

您需要將daoAuthenticationProvider指定為user-service身份驗證提供程序的單獨身份驗證提供程序,因為它們應提供兩種不同的方法來處理身份驗證嘗試。

您的daoAuthenticationProvider將執行您自己的自定義操作以確定是否驗證登錄嘗試,並且user-service將成功驗證您為其提供的兩個用戶。

回答您的問題:在應用程序啟動時使用SQL腳本創建用戶。 您可以使用這樣的SQL腳本:

<jdbc:initialize-database>
    <jdbc:script location="script.location.sql"/>
</jdbc:initialize-database>

您可以根據需要列出任意數量的腳本文件。

如果要添加對加密密碼的支持,請使用BCrypt密碼編碼器,如下所示:

<beans:bean id="passwordEncoder"
    class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />

您可以將此bean自動裝配到daoAuthenticationProvider並使用它來檢查密碼輸入是否與數據庫中存儲的內容相匹配。 如果您願意,您也可以將您在腳本中創建的任何用戶的密碼硬編碼為“asdf123”的哈希版本。 它最終取決於你。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM