2016-07-12 18 views
0

私のアプリは、春の雲oauth2を使用しています。oauth2でログイン失敗/成功を聴く方法grant_type =パスワード

私の目標は、ログイン失敗の最大数を制限するために、春のサーバーを使用することです

angular2のログインコード:

const body = "username=" + encodeURI(username) + "&password=" + encodeURI(password) + 
     "&grant_type=password&client_id=" + encodeURI(this.clientId); 

this.http.post("/oauth/token",body,{headers:authHeaders}).map{ 
... 
} 

春認証サーバーのWebセキュリティコード:

@Override 
     protected void configure(HttpSecurity http) throws Exception { 

     http.httpBasic().and().sessionManagement() 
       .sessionCreationPolicy(SessionCreationPolicy.STATELESS) 
       .and().authorizeRequests() 
      .anyRequest().authenticated(); 
     } 

私はこれらの2つのイベントを試します:

public class AuthenticationFailureListener 
    implements ApplicationListener<AuthenticationFailureBadCredentialsEvent>{ 
@Override 
    public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent e) { 
    //... 
} 
} 

と:

public class AuthenticationSuccessListener 
    implements ApplicationListener<AuthenticationSuccessEvent> { 
    @Override 
    public void onApplicationEvent(AuthenticationSuccessEvent e) { 
//... 
} 
} 

しかし、それは "ログインが失敗と成功" 聞く方法

に動作しますか?

答えて

0

春のセキュリティはデフォルトAuthenticationFailureBadCredentialsEvent(ログインに失敗した)イベントを公開しないます。

ApplicationEventPublisherを使用してDefaultAuthenticationEventPublisherをオーバーライドする必要があります。

これはあなたの認証設定クラスで以下のようにしなければなりません。

@Configuration 
protected static class MyAuthenticationConfiguration extends 
     GlobalAuthenticationConfigurerAdapter { 

    @Value("${ldap.url}") 
    String url; 

    @Value("${ldap.base}") 
    String base; 

    @Value("${ldap.managerDn}") 
    String managerDn; 

    @Value("${ldap.password}") 
    String password; 

    @Autowired 
    ApplicationEventPublisher applicationEventPublisher; 


    @Override 
    public void init(AuthenticationManagerBuilder auth) throws Exception { 
     auth.ldapAuthentication().userSearchFilter("sAMAccountName={0}") 
       .userSearchBase(base).contextSource().url(url) 
       .managerDn(managerDn).managerPassword(password); 
     //This publisher will trigger AuthenticationFailureBadCredentialsEvent (AbstractAuthenticationFailureEvent) 
     auth.authenticationEventPublisher(new DefaultAuthenticationEventPublisher(applicationEventPublisher)); 

    } 

、フォームベースの認証をサポートしてあなたのconfigure()メソッドに以下を追加します。

.and().formLogin(); 

全体の構成方法は、以下のようにする必要があります。

@Override 
protected void configure(HttpSecurity http) throws Exception { 

http.authorizeRequests().antMatchers("/css/**").permitAll() 
     .anyRequest().fullyAuthenticated().and().formLogin(); 
super.configure(http); 

} 
+0

私はLDAPに慣れていません... –

+0

LDAP認証は認証マネージャの一例です。メモリー内認証またはjdbc認証を使用できます。 –

関連する問題