[英]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.