0

私のアプリは、Spring Cloud oauth2 rest と angular を使用しています。

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

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) {
//...
}
}

しかし、それは機能しません

「ログイン失敗と成功」を聞く方法は?

4

1 に答える 1

1

Spring Security は、デフォルトではAuthenticationFailureBadCredentialsEvent (ログイン失敗) イベントを発行しません

DefaultAuthenticationEventPublisher を ApplicationEventPublisher でオーバーライドする必要があります。

これは、以下のように認証構成クラスで行う必要があります。

@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);

}
于 2016-07-12T16:07:37.437 に答える