Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从Spring Security中的文件读取用户名/密码_Java_Spring_Spring Security - Fatal编程技术网

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

Java 从Spring Security中的文件读取用户名/密码,java,spring,spring-security,Java,Spring,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.s

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

您可以使用:

<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>
然后在用户服务中使用以下属性:

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


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

如中所述,您可以将
用户服务
指向包含所需信息的属性文件

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

<authentication-manager>
    <authentication-provider user-service-ref="userServiceBean">        
    </authentication-provider>
</authentication-manager>
然后将
用户服务
指向此文件

soguha9@gmail.com=soham123,ROLE_USER,enabled


您是否花时间阅读了如果我想从txt或csv文件中读取用户名和密码而不是属性文件,该怎么办?您必须自己实现这一点,Spring Security支持上述格式以及开箱即用的参考指南中所述的格式。您需要自己实现的任何其他内容。我可以使用文件的相对路径而不是类路径吗?
<authentication-manager>
    <authentication-provider user-service-ref="userServiceBean">        
    </authentication-provider>
</authentication-manager>
soguha9@gmail.com=soham123,ROLE_USER,enabled
<user-service properties="classpath:users.properties" />