繁体   English   中英

从 Spring Security 中的文件读取用户名/密码

[英]Read username/password from a file in Spring Security

<beans:beans xmlns="http://www.springframework.org/schema/security"
    xmlns:beans="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/security
    http://www.springframework.org/schema/security/spring-security-3.2.xsd">

    <http use-expressions="true" auto-config="true">
        <intercept-url pattern="/login**" access="permitAll"/>
        <intercept-url pattern="/resources/**" access="permitAll"/>
        <intercept-url pattern="/favicon.ico" access="permitAll"/>
        <intercept-url pattern="/**" access="hasRole('ROLE_USER')" />

        <form-login 
            login-page="/login" 
            default-target-url="/" 
            always-use-default-target="true" 
            authentication-failure-url="/login?error=" 
            username-parameter="username"
            password-parameter="password" />
        <logout invalidate-session="true" logout-success-url="/login?logout="  />
        <!-- enable csrf protection -->
        <!-- <csrf/> -->
    </http>

    <authentication-manager>
        <authentication-provider>
            <user-service>
                <user name="soguha9@gmail.com" password= "soham123" authorities="ROLE_USER"/>
            </user-service>
        </authentication-provider>
    </authentication-manager>

</beans:beans>

我想在单独的文件中添加用户名和密码。

您可以使用:

 <context:property-placeholder location="classpath:myapp.properties" />

将属性文件添加到您的资源文件夹,确保它包含您的属性

myapplication.username=soguha9@gmail.com
myapplication.password=soham
myapplication.authorities=ROLE_USER

然后在您的用户服务中使用如下属性:

 <property name="username" value="${myapplication.username}" />
 <property name="password" value="${myapplication.password}" />
 <property name="authorities" value="${myapplication.authorities}" />
<authentication-manager>
    <authentication-provider user-service-ref="userServiceBean">        
    </authentication-provider>
</authentication-manager>

您可以拥有自定义用户服务,它可以从属性文件中读取用户详细信息。

Spring Security 参考指南中所述,您可以将user-service指向包含所需信息的属性文件。

src/main/resources创建一个属性文件users.properties

soguha9@gmail.com=soham123,ROLE_USER,enabled

然后将user-service指向这个文件

<user-service properties="classpath:users.properties" />

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM