Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use Firebase with Spring boot REST Application?

I have a Spring Boot REST application that depends on the authentication done in Firebase.

On the client side Firebase generates a token whereby in the Spring Boot, I need to verify the UID.

But the code is in a callback mode, so how do I implement the function so that it can finish the task?

@RequestMapping(value = "/api/restCall", method = RequestMethod.POST,               consumes = "application/json", produces = "application/json") public Object restCall(@RequestBody Parameters requestBody) throws Exception {     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();                 }             });     return "???"; // what return here? } 

How do I return after onSuccess? DeferredResult?

like image 377
dickyj Avatar asked Aug 27 '16 16:08

dickyj


People also ask

Can we use Firebase with spring boot?

Now that we have the private key, we can start integrating Firebase with our Spring Boot app. To start, place the JSON file in the resources folder under a new folder called firebase-config . Next, we need to add the Firebase admin SDK to our project using Maven.

How do I use Firebase authentication in spring boot?

Open the Page Enter Your email and password which you have created the Firebase Authentication Dashboard and Click login. This idToken is your Bearer token you can modify it in the SpringBoot project according to your use-case. When you hit this private API you will get a user response with user details.

How do I use Firebase REST API?

You can use any Firebase Realtime Database URL as a REST endpoint. All you need to do is append . json to the end of the URL and send a request from your favorite HTTPS client. HTTPS is required.

Can we deploy Java application on Firebase?

Then, using Firebase Hosting, you can direct HTTPS requests to trigger your containerized app. Cloud Run supports several languages (including Go, Node. js, Python, and Java), giving you the flexibility to use the programming language and framework of your choice.


2 Answers

To integrate Firebase with Spring, below is the sample code

In new Admin SDK the process is simple just use below code snippet.

FirebaseAuth.getInstance().deleteUser(uid); System.out.println("Successfully deleted user."); 

For more detail visit this URL https://firebase.google.com/docs/auth/admin/manage-users

This is for a legacy code. First add Firbase dependency

<dependency>     <groupId>com.google.firebase</groupId>     <artifactId>firebase-server-sdk</artifactId>     <version>3.0.1</version> </dependency> 

Sample Code

@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;     }  } 

For Complete example please gone through github below link https://github.com/savicprvoslav/Spring-Boot-starter Complete BlogPost with CRUD operation: https://medium.com/techwasti/spring-boot-firebase-crud-b0afab27b26e

like image 183
Maheshwar Ligade Avatar answered Sep 22 '22 19:09

Maheshwar Ligade


Here is my own attempt to answer my own question

@RequestMapping(value = "/api/restCall", method = RequestMethod.POST, consumes = "application/json", produces = "application/json") public Object restCall(@RequestBody Parameters requestBody,@RequestHeader(value = FIREBASETOKEN, required = true) String idToken) throws Exception {      // idToken comes from the HTTP Header     FirebaseToken decodedToken = FirebaseAuth.getInstance().verifyIdTokenAsync(idToken).get();     final String uid = decodedToken.getUid();      // process the code here     // once it is done     return object;  } 

You can try below code as well

FirebaseAuth.getInstance().deleteUser(uid); System.out.println("Successfully deleted user."); 

for More deetails URL https://firebase.google.com/docs/auth/admin/manage-users

like image 20
dickyj Avatar answered Sep 22 '22 19:09

dickyj