2017-07-15 12 views
-1

例外が発生した後にスーパークラスメソッドを呼び出すのはなぜですか?例外が発生した場合、スーパークラスメソッドを実行する代わりにコールスタックが呼び出し元に戻ります。SpringセキュリティUsernamePasswordAuthenticationTokenは、スーパークラスメソッドを呼び出す前に例外をスローします

public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException { 
     if (isAuthenticated) { 
      throw new IllegalArgumentException(
        "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead"); 
     } 

     super.setAuthenticated(false); 
    } 

https://github.com/spring-projects/spring-security/blob/master/core/src/main/java/org/springframework/security/authentication/UsernamePasswordAuthenticationToken.java

+1

なぜダウン投票が、このためにそこにある:スーパークラスメソッドの呼び出しについて

、この機能を使用するコンストラクタがありますか? – youcanlearnanything

答えて

1

UsernamePasswordAuthenticationTokenクラスのsetAuthenticated(にisAuthenticated boolean)メソッドはAbstractAuthenticationTokenクラスのオーバーライドされたメソッドです。

このクラスでプライベート認証されたプロパティを設定する唯一の方法は、super.setAuthenticated(ブール認証済み)メソッドです。

public UsernamePasswordAuthenticationToken(Object principal, Object credentials, 
      Collection<? extends GrantedAuthority> authorities) { 
     super(authorities); 
     this.principal = principal; 
     this.credentials = credentials; 
     super.setAuthenticated(true); // must use super, as we override 
} 

をそして、それは真として明示的に認証されたプロパティを設定することはできません:

setAuthenticated方法のこのオーバーライドされた行動は、それが唯一のコンストラクタの1つを介してtrueに設定することができることを保証します。

public UsernamePasswordAuthenticationToken(Object principal, Object credentials) { 
     super(null); 
     this.principal = principal; 
     this.credentials = credentials; 
     setAuthenticated(false); 
} 
+0

私はコードを理解していますが、スーパーメソッドの前に例外が処理された理由を調べたいと思います。 – youcanlearnanything

関連する問題