Reputation: 2951
I have a Java Rest API running with Jersey on a Glassfish server, and I use Firebase Auth to authenticate my users.
So I use the Firebase Admin SDK to verify the token FirebaseAuth.getInstance().verifyIdToken(idToken)
But it throws the following error:
sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
I initialize my app correctly, by calling:
FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.fromStream(serviceAccountStream)).build();
With serviceAccountStream an InputStream to my service account JSON file.
Edit: I got the same problem with Firebase's auth emulator and other Firebase services such as Firestore, that's weird
Upvotes: 3
Views: 1736
Reputation: 1
Here I listed five pointer, may be helpful for someone.
For example,
FirebaseOptions options = new FirebaseOptions.Builder().setCredentials(GoogleCredentials.fromStream(serviceAccountStream)).setProjectId("my-project-id").build();
The Firebase Admin SDK attempts to obtain a project ID via one of the following methods:
https://github.com/googleapis/google-api-java-client/issues/1114
https://github.com/googleapis/google-auth-library-java#configuring-a-proxy
Could you please check whether your machine is behind corporate proxy.
keytool -import -alias mycertificate -keystore "/Library/Java/JavaVirtualMachines/jdk-15.0.1.jdk/Contents/Home/lib/security/cacerts " -file yourcertificate.cer
password: changeit
Upvotes: 0
Reputation: 912
The problem is coming from the old libraries you use. The following code works fine on SpringBoot 2.5 set up:
try {
InputStream serviceAccount credStream = getClass().getResourceAsStream(credsPath);
FirebaseOptions options = new FirebaseOptions.Builder()
.setCredentials(GoogleCredentials.fromStream(credStream))
.setDatabaseUrl(databaseURL)
.build();
FirebaseApp.initializeApp(options);
} catch (Exception e) {
logger.error(e.getMessage());
} finally {
...
}
If you cannot upgrade the libraries, you need to add your server certificate into the trustStore. Please, look here for the steps: Java: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Upvotes: 1