2016-08-27 55 views
11

我有一個Spring Boot REST應用程序,它取決於在Firebase中完成的身份驗證。在客戶端,Firebase生成一個令牌,在Spring Boot中,我需要驗證uid。但是我注意到代碼處於回調模式,那麼如何實現Spring Boot函數以完成任務?如何使用Firebase與Spring引導REST應用程序?

@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

爲了整合春季火力地堡下面是示例代碼

首先添加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

相關問題