2017-08-25 13 views
0

私は春のセキュリティに対して非常に新しいです。私はJWTフィルターを私のWebアプリケーションに実装してステートレスにしようとしています。私は、コードの一部、ユーザーが/loginパスに当たったとき、制御がメソッドになり、SpringブートWebアプリケーションでのJWTフィルタとスプリングのセキュリティ制御フロー

public LoginResponse login( 
AuthenticationRequest authenticationRequest, Device device) 
     throws WebappException 
{ 
    // Perform the security 
    final Authentication authentication = authenticationManager 
      .authenticate(new 
UsernamePasswordAuthenticationToken(authenticationRequest.getUsername(), 
        authenticationRequest.getPassword())); 

SecurityContextHolder.getContext().setAuthentication(authentication); 
/** some more logic**/ 

ここで私は私を導いてください final Authentication authentication = authenticationManager .authenticate(new UsernamePasswordAuthenticationToken(authenticationRequest.getUsername(), authenticationRequest.getPassword()));

の目的を理解していないですがあります! AuthenticationRequestには、2つのフィールドuserNamepasswordがあります。

答えて

0

メソッドはUsernamePasswordAuthenticationTokenAuthenticationProviderに渡し、提供されたユーザー名とパスワードでユーザーを認証しようとします。

  • 成功した場合は、認証が失敗した場合に例外をスローする
  • 、付与された当局とAuthenticationオブジェクトを返します。

authentication.isAuthenticated()を呼び出して、トークンが認証されているかどうかを知ることができます。

データベースにアクセスして認証を確認する場合は、DaoAuthenticationProviderの実装(またはAbstractUserDetailsAuthenticationProvider)を使用する必要があります。 インターフェイスUserDetailsServiceからユーザの詳細を取得します。そのため、というメソッドをオーバーライドしてを実装し、UserDetailsを返すクラスMyUserDetailsServiceを作成する必要があります。 UserDetailsには、ユーザー名、パスワード、権限が含まれています。独自のMyUserdetailsクラスを作成し、UserDetailsインターフェイスを実装します。その後 、あなたのcutomクラスを参照するように春を設定:http://www.baeldung.com/spring-security-authentication-with-a-database

DaoAuthenticationProvider authProvider = new DaoAuthenticationProvider(); 
authProvider.setUserDetailsService(myUserDetailsService); 

詳細およびあなたが直接あなたのデータソースとSQLクエリを指定するJdbcUserDetailsManagerConfigurerを使用することができます。

について
@Autowired 
public void configAuthentication(AuthenticationManagerBuilder auth) throws Exception { 
    auth.jdbcAuthentication().dataSource(dataSource) 
     .usersByUsernameQuery("select username, password, enabled from users where username=?") 
     .authoritiesByUsernameQuery("select username, role from user_roles where username=?"); 
} 

JWT、私はあなたのログイン方法が最初にユーザーの認証をチェックして、ユーザーの詳細でJWTを構築してブラウザに返すべきだと思います。 その後、クライアントはこのトークンをサーバーに再送信することができ、このJWTは復号化され、別の方法で検証されます。

+1

https://www.toptal.com/java/rest-security-with-jwt-spring-security-and-javaの 詳細についてはどのように '最終認証認証= authenticationManager .authenticate(新しい UsernamePasswordAuthenticationToken(authenticationRequest.getUsername()、 authenticationRequest.getPassword()));'ユーザ名とパスワードのチェックを行い、?私は単に、ユーザーがリクエストに投稿した内容を渡しています。データベース内のデータに対してどのように検証されますか? –

関連する問題