首页 > 解决方案 > 创建客户端 Spring Security OAuth2 应用程序

问题描述

我的项目中有以下架构:

 JOB_SCHEDULE ---> API A  ---> API B

我的 JOB_SCHEDULE 调用 API A 没有任何身份验证,这对我们来说没问题。但是 API A 需要调用 API B,而这最后有一个 client_credentials 认证。

我们有一个 IdentityServer 来生成这个令牌,所以在API A中我配置了以下内容:

security.oauth2.client.client-id=clientid
security.oauth2.client.client-secret=secret
security.oauth2.client.access-token-uri=http://localhost:8080/oauth/token
security.oauth2.client.user-authorization-uri=http://localhost:8080/oauth/authorize
security.oauth2.client.grant-type=client_credentials
security.oauth2.client.authentication-scheme=form

在我的 pom.xml 中:

<dependency>
            <groupId>org.springframework.security.oauth.boot</groupId>
            <artifactId>spring-security-oauth2-autoconfigure</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.springframework.security.oauth</groupId>
            <artifactId>spring-security-oauth2</artifactId>
            <version>2.3.5.RELEASE</version>
        </dependency>

我在API A中有一个调用 API B 的端点,请参阅:

private final OAuth2RestTemplate restTemplate;

    @GetMapping("/sayhello")
    public ResponseEntity<String> redirectToResourceServer() {
        String strReturn = restTemplate.getForObject("http://localhost:9090/noscope", String.class);
        return ResponseEntity.ok(strReturn);
    }

但是每次我打电话时http://localhost/api/v1/hello/sayhello,我都会被重定向到登录页面,但这是 client_credentials 我不应该被重定向。

标签: springspring-bootspring-security-oauth2

解决方案


推荐阅读