2016-12-10 2 views
0

私は春のセキュリティでWebアプリケーションを作っています。彼はログインやパスワードをコードやXMLで保存して、1人のユーザーしかアクセスできないようにしたいと考えています。唯一のユーザーの春のセキュリティ

これは可能ですか?

答えて

2

InMemoryAuthenticationを使用できます。したがって、データベースは必要ありません。

@Configuration 
@ComponentScan 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
      auth.inMemoryAuthentication().withUser("admin").password("pw123").roles("ADMIN"); 
      auth.inMemoryAuthentication().withUser("user1").password("pw123").roles("USER"); 
      auth.inMemoryAuthentication().withUser("user2").password("pw123").roles("USER"); 
    } 
} 

コードでユーザーとパスワードを定義する代わりに、XMLファイルからユーザーとパスワードを読み取ることができます。

+0

また、これらの資格情報を外部プロパティファイルに保存する場合は、上記のコンフィグレーションクラスで 'org.springframework.annotation.PropertySource'アノテーションと' org.springframework.core.env.Environment'オブジェクトを使用できます。 –

関連する問題