尝试检索oauth 2.0访问令牌响应时出错:post请求的i/o错误

avkwfej4  于 2021-10-10  发布在  Java
关注(0)|答案(1)|浏览(581)

我正在开发SpringBoot和SpringSecurityOAuth2客户端示例,试图在facebook出现以下错误时执行sso
[invalid_token_response]尝试检索oauth 2.0访问令牌响应时出错:post请求时发生i/o错误“https://graph.facebook.com/v2.8/oauth/access_token“:pkix路径生成失败:sun.security.provider.certpath.suncertpathbuilderexception:找不到请求目标的有效证书路径;嵌套异常为javax.net.ssl.sslhandshakeexception:pkix路径生成失败:sun.security.provider.certpath.suncertpathbuilderexception:找不到请求目标的有效证书路径
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();
    }
}
7dl7o3gd

7dl7o3gd1#

也许可以尝试使用@authenticationprincipal,例如:

@RestController
public class TokenController {

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

        return idTokenValue ;

    }
}

相关问题