2

I am developing Spring Boot & Spring Security OAuth2 client example trying to do SSO with facebook getting below error

[invalid_token_response] An error occurred while attempting to retrieve the OAuth 2.0 Access Token Response: I/O error on POST request for "https://graph.facebook.com/v2.8/oauth/access_token": PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.4.5</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.example</groupId>
    <artifactId>spring-security-oauth-ex1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>spring-security-oauth-ex1</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

application.yml

server:
  port: 8080
  
logging:
  level:
    root: INFO
    com.memorynotfound: INFO
    org.springframework.web: INFO
    org.springframework.security: DEBUG
    
    
spring:
  security:
    oauth2:
      client:
        registration:
          facebook:
            client-id: 156900623068997
            client-secret: 

portfolio.html

<!DOCTYPE html>
<html>
<head>
    <meta charset="ISO-8859-1">
    <title>Insert title here</title>
</head>

<body>
    Welcome to your portfolio
</body>
</html>

PortfolioController.java

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class PortfolioController {

    @GetMapping("/portfolio")
    public String portfolio() {
        return "portfolio";
    }
}

TokenController.java

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.core.oidc.user.DefaultOidcUser;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class TokenController {

    @GetMapping("/token")
    public String getAccessToken() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        DefaultOidcUser principal = (DefaultOidcUser) auth.getPrincipal();
        return principal.getIdToken().getTokenValue();
    }
}
PAA
  • 1
  • 46
  • 174
  • 282
  • Generally `SunCertPathBuilderException: unable to find valid certification path to requested target` tells that you haven't provide your certififcate to RestTemplate (or whatever REST client you use to perform remote calls) – Nikolai Shevchenko May 25 '21 at 07:24
  • Concur with above. This is a pretty mundane error which translates to "You asked me to open a connection to someone whose certificates I don't trust." Which normally either means the server who you are connecting to's certificate is bogus or more likely you just haven't configured your https/sal client connection provider with a proper set of certificate authorities to trust. – Atmas May 27 '21 at 02:26
  • It looks like you need to import facebook ssl certificate to your java application. – ILya Cyclone May 29 '21 at 12:01
  • See #1 in here: https://stackoverflow.com/a/42878358/4178245 – kolejnik May 31 '21 at 15:00

1 Answers1

0

Maybe try use @AuthenticationPrincipal someway like:

@RestController
public class TokenController {

    @GetMapping("/token")
    public String getAccessToken(@AuthenticationPrincipal OidcUser principal) {
        OidcIdToken idToken = principal.getIdToken();
        String idTokenValue = idToken.getTokenValue();

        return idTokenValue ;

    }
}
PAA
  • 1
  • 46
  • 174
  • 282