2017-03-09 9 views
2

Springセキュリティ用のファイルにユーザー資格情報を格納および変更することは可能ですか?今のSpringセキュリティのファイルからユーザー資格情報の読み取り/追加

私が持っているのsecurity.xml:

<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.xsd"> 
<http> 
    <intercept-url pattern="/**" access="ROLE_USER" /> 
    <form-login /> 
    <logout /> 
</http> 

<authentication-manager> 
    <authentication-provider> 
     <user-service> 
      <user name="admin" password="admin" authorities="ROLE_USER, ROLE_ADMIN" /> 
      <user name="user" password="user" authorities="ROLE_USER" /> 
     </user-service> 
    </authentication-provider> 
</authentication-manager> 

をそして私が望むすべては/ユーザーを追加または変更する可能性USERS.TXTまたはusers.xmlのようにファイルの情報を格納することですパスワードはオンザフライで表示されます。

答えて

2

あなたは、その

UserDao.java

import org.springframework.security.core.userdetails.User; 
import org.springframework.security.core.userdetails.UserDetails; 
import org.springframework.security.core.userdetails.UserDetailsService; 
import org.springframework.security.core.userdetails.UsernameNotFoundException; 

/** 
* @author asif.hossain 
* @since 3/9/17. 
*/ 
public class UserDao implements UserDetailsService { 
    @Override 
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException { 

     String password = readPasswordFromFileOrDatabase(username); 

     if (password == null) throw new UsernameNotFoundException(""); 

     return User 
       .withUsername(username) 
       .password(password) 
       .authorities("ROLE_USER") 
       .build(); 
    } 

    private String readPasswordFromFileOrDatabase(String username) { 
     // Edit this code and read password and roles from data base or files 
     if (username.equals("user")) return "password"; 
     return null; 
    } 
} 

そしてのsecurity.xmlに

<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.xsd"> 
<http> 
    <intercept-url pattern="/**" access="ROLE_USER" /> 
    <form-login /> 
    <logout /> 
</http> 

<bean id="userDao" class="UserDao"></bean> 

<authentication-manager alias="authenticationManager"> 
    <authentication-provider user-service-ref="userDao"></authentication-provider> 
</authentication-manager> 
+0

おかげで多くのことを、このクラスのBeanを追加するためのUserDetailsServiceをオーバーライドする必要があります私のweb.xmlやapplicationContext.xmlに何かを追加すれば、ログインフォームが表示されません。 (今私はapplicationContextにsecurity.xmlをインポートします) –

+1

@ zelenov alekseyはapplicationContext.xml内のBeanを指摘してくれました。 –

関連する問題