2016-08-27 56 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

相关问题