Java Spring安全:创建用户

Java Spring安全:创建用户,java,spring,spring-mvc,spring-security,Java,Spring,Spring Mvc,Spring Security,我正在应用程序中使用SpringWeb(4.0.3.RELEASE)和SpringSecurityWeb(3.2.3.RELEASE)。我的目标是在应用程序启动时自动创建一些用户。但是,当我使用“security:user…”标记添加用户时,它要么不创建用户,要么抱怨 Configuration problem: authentication-provider element cannot have child elements when used with 'ref' attribute

我正在应用程序中使用SpringWeb(4.0.3.RELEASE)和SpringSecurityWeb(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>
您可以列出任意数量的脚本文件

如果要添加对加密密码的支持,请使用BCrypt密码编码器,如下所示:

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


您可以将此bean自动连接到您的
daoAuthenticationProvider
,并使用它检查密码输入是否与数据库中存储的密码匹配。如果愿意,也可以将脚本中创建的任何用户的密码硬编码为“asdf123”的哈希版本。最后由你决定。

我已经试过了,根据你的回答,我又试了一次。在此场景中,不创建任何用户。查看日志,我可以看到Hibernate创建表,但找不到“Insert blach..”的实例=(因为这两个用户只存储在内存中。如果你想将它们存储在数据库中,你需要在应用程序启动时使用SQL脚本插入数据。你可以使用这些用户的脚本登录到应用程序中吗?它工作了吗?如果没有,还有更多的事情要尝试。看起来我将不得不使用一个简单的脚本来完成我要做的事情。)ed to。我担心userPropertyToUse/salt/password组合之间会引入脆性,但我可能必须这样做才能使其工作。我明天会再试一次。我采用的解决方案是使用import sql语句。我不喜欢它,但它允许我继续我的玩具项目,这才是真正重要的。
<beans:bean id="passwordEncoder"
    class="org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder" />