如何将用户名/密码移出spring-security-context.xml?

时间:2012-06-17 22:03:43

标签: spring spring-mvc spring-security

我在我的一个项目中使用Spring Security。网络应用程序要求用户登录。因此,我在spring-security-context.xml文件中添加了一些用户名和密码,如下所示:

<authentication-manager>
    <authentication-provider>
        <user-service>
            <user name="user_1" password="password_1" authorities="ROLE_USER" />
            <user name="user_2" password="password_2" authorities="ROLE_USER" />
        </user-service>
    </authentication-provider>
</authentication-manager>

我的问题是,如何将这些用户名 - 密码对移动到不同的文件(如某些属性文件),而不是将它们保存在spring-security-context.xml中?以及如何读取该文件属性文件?

6 个答案:

答案 0 :(得分:15)

您可以将用户名和密码存储在单独的.properties文件中。

<user-service id="userDetailsService" properties="users.properties"/> 

users.properties应具有以下格式:

jimi=jimispassword,ROLE_USER,ROLE_ADMIN,enabled
bob=bobspassword,ROLE_USER,enabled

如果您想将其存储在数据库中,我建议您阅读这篇文章:http://www.mkyong.com/spring-security/spring-security-form-login-using-database/

参考:Spring Security In-Memory Authentication

答案 1 :(得分:2)

您可以使用PropertyPlaceholderConfigurer - 将它们放在属性文件中,然后使用EL:

引用它们

http://static.springsource.org/spring/docs/3.1.x/spring-framework-reference/html/beans.html#beans-factory-placeholderconfigurer

答案 2 :(得分:1)

您可以找到将它们移动到数据库或LDAP的方法。 Spring Security肯定支持两者。

答案 3 :(得分:1)

我已经尝试了最后建议的方法我做了以下似乎很好地工作

在您的网络xml中添加了这些更改

<filter-mapping>
<filter-name>springSecurityFilterChain</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping> 

<servlet-mapping>
<servlet-name>service</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

<filter>
<filter-name>springSecurityFilterChain</filter-name>
<filter-class>org.springframework.web.filter.DelegatingFilterProxy</filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping> 

在spring-security xml中添加这些更改

<security:authentication-manager alias="authenticationManager">
<security:authentication-provider>
<security:user-service>
<security:user name="${resource.service.authentication.name}"
authorities="${resource.service.authentication.authorities}"
password="${resource.service.authentication.password}"/>
</security:user-service>
</security:authentication-provider>
</security:authentication-manager>

将这些更改添加到应用程序上下文xml中,或者如果您具有property-loader xml甚至     更好

<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="placeholderPrefix" value="${" />
<property name="placeholderSuffix" value="}" />
<property name="locations">
<list>
<value>classpath:resourceservice.properties</value>
</list>
</property>
</bean>

然后在属性文件resourceservice.properties

中添加这些更改
memberservice.authentication.name=usename
memberservice.authentication.authorities=AUTHORISED
memberservice.authentication.password=password

在使用Jersey

的资源中添加这些更改
@PUT
@Path("{accountId}")
@Consumes("application/xml")
@PreAuthorize("hasRole('AUTHORISED')")
public Response methodName

答案 4 :(得分:0)

这适用于我使用属性文件进行Spring安全身份验证和授权:

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:security="http://www.springframework.org/schema/security"

    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-3.2.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
        http://www.springframework.org/schema/security
        http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <mvc:annotation-driven />

    <bean id="webPropertyConfigurer"
        class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="ignoreResourceNotFound" value="true" />
        <property name="ignoreUnresolvablePlaceholders" value="true" />
        <property name="locations">
            <list>
                <value>classpath:abc.properties</value>
            </list>
        </property>
    </bean>

    <bean
        class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor" />

    <security:http auto-config="true" use-expressions="true">
        <security:intercept-url pattern="/stat/login" access="permitAll"/>
        <security:intercept-url pattern="/stat/summary" access="hasRole('ROLE_ADMIN')" />

        <security:form-login login-page="/stat/login"
            default-target-url="/stat/summary" authentication-failure-url="/stat/loginError" /> 
    </security:http>
    <!-- Username and password used from xml -->
    <!-- <security:authentication-manager>
        <security:authentication-provider>
            <security:user-service>
                <security:user name="xyz" password="xyz" authorities="ROLE_ADMIN" />
            </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager> -->

    <security:authentication-manager>
        <security:authentication-provider>
             <security:user-service>
        <security:user name="${stat.user}" password="${stat.pwd}" authorities="ROLE_ADMIN" />
        </security:user-service>
        </security:authentication-provider>
    </security:authentication-manager> 
</beans>

abc.properties文件:

stat.user=xyz
stat.pwd=xyz

spring-security实现的web.xml条目:

<filter>
    <filter-name>springSecurityFilterChain</filter-name>
    <filter-class>org.springframework.web.filter.DelegatingFilterProxy
    </filter-class>
</filter>

<filter-mapping>
    <filter-name>springSecurityFilterChain</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

答案 5 :(得分:0)

您只需在 Spring Security 配置中添加Bean即可:

@Bean
public UserDetailsService userDetailsService() {
   Properties users = PropertiesLoaderUtils.loadAllProperties("users.properties");
   return new InMemoryUserDetailsManager(users);
}

users.properties 如下:

admin={noop}password,ROLE_USER,ROLE_ADMIN,enabled
bob={noop}password,ROLE_USER,enabled
123={noop}123,ROLE_USER,enabled