簡體   English   中英

使用Azure Active Directory作為Spring-boot REST服務的OAUTH2身份驗證服務

[英]Using Azure Active Directory as an OAUTH2 Authentication service for a Spring-boot REST service

我正在嘗試實現基於Spring-boot的REST服務,該服務應該使用Azure AD作為OAuth2服務器進行客戶端身份驗證。

我注冊了兩個應用程序:

  1. 用作我的服務客戶端的移動本機應用程序
  2. 休息服務作為后端。

應通過Azure AD使用OAuth2流對所有對后端應用程序的請求進行身份驗證。

作為移動應用程序的一個實現我使用curl:

要獲得Bearer令牌,請使用https://login.microsoftonline.com/TENANT_ID/oauth2/token

curl -s -X POST https://login.microsoftonline.com/<TENANT_ID>/oauth2/token -d grant_type=password -d username=$USER_NAME -d password=$PASSWORD -d resource=$RESOURCE_ID -d client_id=$CLIENT_ID

其中$ USER_NAME和$ PASSWORD是Azure AD用戶的憑據,$ RESOURCE_ID是我的REST服務的SID,$ CLIENT_ID是我的移動客戶端的SID,用於REST服務。

Azure使用令牌數據成功返回JSON。

我的Oauth2配置后端應用程序:

@Configuration
@EnableResourceServer
public class OAuth2Config extends ResourceServerConfigurerAdapter {
 @Bean
    ResourceServerTokenServices resourceTokenServices() {
        RemoteTokenServices tokenServices = new RemoteTokenServices();
        tokenServices.setClientId(resourceId);
        tokenServices.setClientSecret(/*I do not have it*/resourcePassword);
        tokenServices.setCheckTokenEndpointUrl(/*I do not have it*/checkToken);
        return tokenServices;
    }

@Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.tokenServices(resourceTokenServices());
        resources.resourceId("rest_api");
    }

@Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/**").authenticated();
    }
}

我的REST控制器:

@RestController
@RequestMapping("/data")
public class CustomerRestController {

    @RequestMapping(method = RequestMethod.GET)
    public SomeData getMyData(Principal principal){
        System.out.println("RESOURCE WAS REQUESTED BY " + principal.getName());
        return new SomeData(principal.getName());
    }
}

但是我沒有在端點列表中找到我的REST服務可用於檢查持票人令牌和從Azure AD獲取用戶數據的任何URL。 此外,據我所知,它應該為我的REST服務提供某種憑據以使用Azure AD

我怎樣才能找到所需的值或者我走錯了路?

最后我得到了答案。

Azure AD使用JWT令牌進行授權,因此我必須使用此類令牌實現工作,而不是檢查服務器上的令牌。

暫無
暫無

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

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