2016-08-27 17 views
11

私はFirebaseで行われた認証に依存するSpring Boot RESTアプリケーションを持っています。クライアント側でFirebaseはトークンを生成し、それによってSpringブートで、私はUIDを確認する必要があります。しかし、コードがコールバックモードになっていることに気づいたので、どのようにしてSpringのブート機能を実装してタスクを終了できるのでしょうか?ここでSpringブートRESTアプリケーションでFirebaseを使用するには?

@RequestMapping(value = "/api/restCall", method = RequestMethod.POST, consumes = "application/json", produces = "application/json") 
    public Object restCall(@RequestBody Parameters requestBody) throws Exception { 

    // idToken comes from the client app (shown above) 
     String idToken = requestBody.getToken(); 

     Task<FirebaseToken> task = FirebaseAuth.getInstance().verifyIdToken(idToken) 
      .addOnSuccessListener(new OnSuccessListener<FirebaseToken>() { 
       @Override 
       public void onSuccess(FirebaseToken decodedToken) { 
        String uid = decodedToken.getUid(); 
        // process the code here 
       } 
     }); 
     // how do I return here, since the code is in the onSuccess? 
     // do I return as a DeferredResult? 

    } 

答えて

4

は、自分の質問に答えるのはまだテストされていませんが、多分誰かがこのスレッドを参照して、それを検証することができますするために私自身の試みです: Java Equivalent of C# async/await?

@RequestMapping(value = "/api/restCall", method = RequestMethod.POST, consumes = "application/json", produces = "application/json") 
public DeferredResult<Object> restCall(@RequestBody Parameters requestBody) throws Exception { 

// idToken comes from the client app (shown above) 
    String idToken = requestBody.getToken(); 
    final DeferredResult<Object> promise = new DeferredResult<Object>(); 

    Task<FirebaseToken> task = FirebaseAuth.getInstance().verifyIdToken(idToken) 
     .addOnSuccessListener(new OnSuccessListener<FirebaseToken>() { 
      @Override 
      public void onSuccess(FirebaseToken decodedToken) { 
       String uid = decodedToken.getUid(); 
       // process the code here 
       // once it is done 
       promise.setResult(object); 

      } 
    }); 
    return promise; 

} 
+0

を経て下さい。 –

3

Firebaseに春を統合するには以下のサンプルコード

まず追加Firbase依存性がある

<dependency> 
    <groupId>com.google.firebase</groupId> 
    <artifactId>firebase-server-sdk</artifactId> 
    <version>3.0.1</version> 
</dependency> 
完全な例については

サンプルコード

@Component 
public class FirebaseAuthenticationProvider implements AuthenticationProvider { 

    @Autowired 
    @Qualifier(value = UserServiceImpl.NAME) 
    private UserDetailsService userService; 

    public boolean supports(Class<?> authentication) { 
     return (FirebaseAuthenticationToken.class.isAssignableFrom(authentication)); 
    } 

    public Authentication authenticate(Authentication authentication) throws AuthenticationException { 
     if (!supports(authentication.getClass())) { 
      return null; 
     } 

     FirebaseAuthenticationToken authenticationToken = (FirebaseAuthenticationToken) authentication; 
     UserDetails details = userService.loadUserByUsername(authenticationToken.getName()); 
     if (details == null) { 
      throw new FirebaseUserNotExistsException(); 
     } 

     authenticationToken = new FirebaseAuthenticationToken(details, authentication.getCredentials(), 
       details.getAuthorities()); 

     return authenticationToken; 
    } 

} 

これはしかし春の方法ではありませんリンクの下のgithubの https://github.com/savicprvoslav/Spring-Boot-starter

関連する問題