2013-02-18 25 views
6

私は春に認証サービスを作成しています。SpringSecurity UserDetailsS​​erviceパスワードを取得

私はUserDetailsS​​erviceを使用してフォーム変数を取得していますが、loadUserByUsernameには変数userNameが1つしかないことがわかりました。

パスワードの取得方法は?

public class userAuthentication implements UserDetailsService{ 

    private @Autowired 
    ASPWebServicesUtils aspWebServicesUtils; 

    @Override 
    public UserDetails loadUserByUsername(String name) throws UsernameNotFoundException { 

     //how to get password ? 

     User user = new User("test", "test", true, true, true, true, getAuthorities(true)); 

     return user; 

    } 

    private List<GrantedAuthority> getAuthorities(boolean isAdmin){ 

     List<GrantedAuthority> authorityList = new ArrayList<GrantedAuthority>(2); 
     authorityList.add(new SimpleGrantedAuthority("USER_ROLE")); 
     if(isAdmin){ 
      authorityList.add(new SimpleGrantedAuthority("ADMIN_ROLE")); 
     } 
     return authorityList; 

    } 
//... 
} 

おかげ

+0

私は情報を提供webserwiceを持って、ユーザーが存在しています。このwebserwiceにログインとパスワードを渡す必要があります。 – Ilkar

+0

[FAQ](http://static.springsource.org/spring-security/site/faq/faq.html#faq-what-is-userdetailservice)と[documentation](http:// 'UserDetailsS​​ervice'に何があるのか​​を確認してください。フレームワークにデータをロードするためだけです。 –

答えて

0

loadUserByUsername(String name)は、あなたのサービスが実装するインタフェース(userServicedetailsが、私は思う)、上で定義された方法です。実装を記述する必要があります。

getPassword()などの実装を記述する必要があるように、springはそれを提供しません。私はあなたのユーザオブジェクトにパスワードが格納されていると思いますが、あなたはそれを書きました... getPassword()メソッドを作成しましたか?

3

私はUserDetailsServiceはあなたがUserDetails、春のセキュリティ(またはあなたが)ユーザ名にそれを比較する必要があることをしたら、いくつかのバックエンド・ストレージ、データベース、フラット・ファイルなどからUserDetailsオブジェクトを取得するために使用されることを想定していると考えています(または他のプリンシパル)とパスワード(資格情報)を使用して、そのユーザーを認証します。

私はあなたが意図した通りに使用しているとは思わない。

+0

こんにちは、Spring Securityのデータベースに対してこのパスワードチェックを実装するにはどうすればよいですか?任意のメソッドをオーバーライドする必要がありますか、またはインターフェイスを実装する必要がありますか? –

12

Userオブジェクトを見ると、コンストラクタの2番目のパラメータはパスワードです。

UserDetailsServiceは、データベースなどのバックエンド構造からユーザーを読み込むために使用されます。 loadUserByUsernameメソッドは、ユーザーがユーザー名とパスワードでログインしようとしたときに呼び出されます。ユーザー定義を読み込んでセキュリティフレームワークに返すのはサービスの責任です。必要な詳細は、username,password,accountNonExpired,credentialsNonExpired,accountNonLockedおよびauthoritiesのようなデータを含む。

春のセキュリティは、ユーザーオブジェクトを受け取ると、それはユーザとユーザアカウントのステータス(accountNonExpired、credentialsNonExpiredなど)

+0

それは私が知っている - 私はそれを作成しています。私はフォームに渡されたデータを受け取りたい。 – Ilkar

+3

パスワードを処理するのはスプリングフレームワークの責任であり、セキュリティフレームワークはユーザーが入力したパスワードとサービスから送信されたパスワードを比較します –

1

のような他のデータが入力したパスワードに対してユーザーを検証しますアウトの標準(の一部-theボックス)ユーザ情報を取得し、認証情報を提供するメカニズムは、次のとおり

  • inMemoryAuthentication
  • jdbcAuthentication
  • ldapAuthentication
  • userDetailsS​​ervice

上記のあなたの目的に合わないと、あなたが作成し、そのように新しい認証プロバイダーを構成することができ、カスタムソリューションを持っている必要がある場合:

セキュリティ設定:

@Configuration 
@EnableWebMvcSecurity 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 

    @Override 
    @Autowired 
    public void configure(AuthenticationManagerBuilder auth) throws Exception { 
     auth.authenticationProvider(new CustomAuthenticationProvider()); 
    } 

    .... 
} 

認証プロバイダ:

public class CustomAuthenticationProvider implements AuthenticationProvider { 

    @Override 
    public Authentication authenticate(Authentication authentication) 
      throws AuthenticationException { 
     String name = authentication.getName(); 
     // You can get the password here 
     String password = authentication.getCredentials().toString(); 

     // Your custom authentication logic here 
     if (name.equals("admin") && password.equals("pwd")) { 
      Authentication auth = new UsernamePasswordAuthenticationToken(name, 
        password); 

      return auth; 
     } 

     return null; 
    } 

    @Override 
    public boolean supports(Class<?> authentication) { 
     return authentication.equals(UsernamePasswordAuthenticationToken.class); 
    } 

} 
0

XMLの実装:

<authentication-manager alias="loginAuthenticationManager"> 
    <authentication-provider ref="loginAuthenticationProvider" /> 
</authentication-manager> 

<!-- Bean implementing AuthenticationProvider of Spring Security --> 
<beans:bean id="loginAuthenticationProvider" class="com.config.LoginAuthenticationProvider"> 
</beans:bean> 

がAuthenticationProvider:

public class LoginAuthenticationProvider implements AuthenticationProvider { 
    @Override 
    public Authentication authenticate(Authentication authentication) 
      throws AuthenticationException { 
     String name = authentication.getName(); 
     // You can get the password here 
     String password = authentication.getCredentials().toString(); 

     // Your custom authentication logic here 
     if (name.equals("admin") && password.equals("pwd")) { 
      List<GrantedAuthority> grantedAuths = new ArrayList<>(); 
      grantedAuths.add(new SimpleGrantedAuthority("ROLE_USER")); 
      return new UsernamePasswordAuthenticationToken(name, password, grantedAuths); 
     } 
     return null; 
    } 
    @Override 
    public boolean supports(Class<?> authentication) { 
     return authentication.equals(UsernamePasswordAuthenticationToken.class); 
    } 
} 
関連する問題