2017-06-21 6 views
0

春、カスタム認証プロバイダで作成されたオブジェクトをコントローラに渡したいと思います。どうやってやるの?私のコントローラで カスタム認証プロバイダからカスタムコントローラへのカスタムオブジェクトの受け渡し方法

@Component 
public class CustomAuthProvider implements AuthenticationProvider { 


@Override 
public Authentication authenticate(Authentication authentication) throws AuthenticationException { 


    String email = authentication.getName(); 
    String password = authentication.getCredentials().toString(); 

    // check authentication here.... 

    // create custom object 

Object customObject = ... 

return new UsernamePasswordAuthenticationToken(email,password, customObject); 

} 

は、私はこのカスタムオブジェクトを使用したい:

@RequestMapping(value = "/user", method = RequestMethod.GET) 
     public String test(Object customObject) { 
    //use customObject here 
} 

I疲れカスタムトークンオブジェクトを作成するために、UsernamePasswordAuthenticationTokenをこのように拡張する:

public class CustomAuthToken extends 

UsernamePasswordAuthenticationToken { 

    //object of any class 
    private Object customObject; 

public CustomAuthToken(Object principal, Object credentials) { 
    super(principal, credentials); 
    this.customObject = null; 

} 

public CustomAuthToken(Object principal, Object credentials, Object customObject) { 
     super(principal, credentials); 
     this.customObject = customObject; 
     } 

カスタム認証プロバイダでこのトークンを返すと、次のエラーが表示されます。

No AuthenticationProvider found for com.example.demo.security.CustomAuthToken

これは私の望みを達成するための正しいアプローチですか?このエラーを修正するにはどうすればよいですか?

答えて

0

まあ、私の問題の解決策を見つけました。ここで私は固定しています:

では、拡張クラスのための編集コンストラクタです。正しいパラメーターを指定してcustomAuthTokenオブジェクトを返し、CustomAuthProviderクラスで

public class CustomAuthToken extends UsernamePasswordAuthenticationToken { 

    private Object object; 

    public CustomAuthToken(String principal, String credentials, Collection<? extends GrantedAuthority> authorities, Object object) { 
      super(principal, credentials, authorities); 

      this.object = object; 
    } 

} 

::

... return new CustomAuthToken(email,password, new ArrayList<>(), object); ...

コントローラでは、私は3パラメータ、校長、資格情報と当局とUsernamePasswordAuthenticationTokenを作成する必要があります認証タイプの正しいパラメータを設定します。

@RequestMapping(value = "/user", method = RequestMethod.GET) 
    public String test(CustomAuthToken auth) { 
System.out.println(auth.object.ToString()); 
} 
関連する問題