2014-05-06 82 views
0

我在應用程序中使用spring-web(4.0.3.RELEASE)以及spring-security-web(3.2.3.RELEASE)。我的目標是在我的應用程序啓動時自動創建一些用戶。然而,當我添加使用用戶的「安全性:用戶...」的標籤,它要麼不創建用戶,或者抱怨Spring Security:創建用戶

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

截至目前,我的安全-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> 

我想知道什麼是創建用戶並填充我的分貝接受的方式。

回答

2

改成這樣:

<!--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」。它最終取決於你。

+0

我已經嘗試過,並且根據您的回覆再次執行了此操作。在這種情況下,沒有用戶被創建。看看日誌,我可以看到Hibernate創建表,但沒有找到「Insert blach ..」的實例。 =( –

+2

)因爲這兩個用戶只存儲在內存中,如果你想將它們存儲在數據庫中,你需要在應用程序啓動時使用SQL腳本來插入數據。這些用戶的? – JamesENL

+0

它是否工作?如果它沒有更多的東西可以嘗試 – JamesENL

相關問題