簡體   English   中英

Spring服務自動調用Oauth2保護服務

[英]Spring service automatic calling Oauth2 protected services

我有一個服務 A,它調用服務 B 來獲取一些資源。 當客戶端使用訪問令牌調用 A 並且 A 將令牌中繼到對 B 的請求時,它工作正常。

現在,我正在實施 redis 來緩存數據。 當我收到來自 B 的消息更新某些內容時,我需要調用 B 來檢索那些更新的資源。 但是,A 現在沒有訪問令牌,我的請求也沒有被授權。

有沒有辦法自動生成訪問令牌?

謝謝你的幫助。

得到了一些工作,但不確定這是否是好的做法。

我為此創建了一個單獨的 RestTemplate。

依賴:

        <dependency>
            <groupId>org.springframework.security.oauth.boot</groupId>
            <artifactId>spring-security-oauth2-autoconfigure</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-jwt</artifactId>
            <version>1.1.1.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
        </dependency>

使能夠:

@EnableOAuth2Client

應用程序.yml:

security:
  oauth2:
    auto:
      clientId: <client id>
      clientSecret: <client key>
      grantType: password
      username: <username>
      password: <password>
      accessTokenUri: <URI>
      userAuthorizationUri: <URI>
      scope: openid profile email

配置:

    @Bean
    @ConfigurationProperties("security.oauth2.auto")
    protected ResourceOwnerPasswordResourceDetails autoOAuth2Details() {
        return new ResourceOwnerPasswordResourceDetails();
    }

    @LoadBalanced
    @Bean
    protected OAuth2RestTemplate autoRestTemplate(RestTemplateCustomizer customizer) {
        OAuth2RestTemplate restTemplate = new OAuth2RestTemplate(autoOAuth2Details);
        customizer.customize(restTemplate);
        return restTemplate;
    }

使用此模板,我的服務能夠自行調用其他服務。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM