我在应用程序中使用 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 Autowiring 到您的 daoAuthenticationProvider
中,并使用它来检查密码输入是否与数据库中存储的内容匹配。如果您愿意,您还可以将在脚本中创建的任何用户的密码硬编码为“asdf123”的哈希版本。最终取决于你。
关于java - Spring 安全: Create Users,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23485713/