簡體   English   中英

在 Vaadin 23 + Spring Security + Azure AD 中訪問 Microsoft Graph API

[英]Accessing Microsoft Graph API in Vaadin 23 + Spring Security + Azure AD

我正在開發一個企業 Vaadin 應用程序,我想知道是否有人知道如何獲取 JWT 令牌以(從后端)向 GraphAPI 發出請求以獲取其他用戶詳細信息。

我的安全配置看起來像這樣。 我通過這種配置實現的是 SSO 體驗。 只需進入該站點,用戶就會被重定向到 MS 身份驗證門戶,並在通過身份驗證時被重定向回來。

@Configuration
public class SecurityConfiguration extends VaadinWebSecurityConfigurerAdapter {

    private final OAuth2UserService<OidcUserRequest, OidcUser> oidcUserService;

    public SecurityConfiguration(OAuth2UserService<OidcUserRequest, OidcUser> oidcUserService) {
        this.oidcUserService = oidcUserService;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        super.configure(http);
        http.oauth2Login().userInfoEndpoint().oidcUserService(oidcUserService);
    }
}

在 application.properties 我有以下設置:

azure.activedirectory.tenant-id=my_tenant_id
azure.activedirectory.client-id=my_client_id
azure.activedirectory.client-secret=my_secret_key
azure.activedirectory.redirect-uri-template=http://localhost:8080/login/oauth2/code/

在 pom.xml 上

<dependencyManagement>
...
    <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>azure-spring-boot-bom</artifactId>
        <version>${azure.version}</version>
        <type>pom</type>
        <scope>import</scope>
    </dependency>
...
</dependencyManagement>
    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-oauth2-client</artifactId>
    </dependency>
    <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>azure-spring-boot-starter-active-directory</artifactId>
    </dependency>
    <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>azure-spring-boot-starter-active-directory</artifactId>
    </dependency>
...

從 Rest Endpoint 開始,有據可查,我應該如何檢索令牌並撥打電話。

@GetMapping("/graph")
@ResponseBody
public String graph(
    @RegisteredOAuth2AuthorizedClient("graph") OAuth2AuthorizedClient graphClient
) {
    // toJsonString() is just a demo.
    // oAuth2AuthorizedClient contains access_token. We can use this access_token to access the resource server.
    return toJsonString(graphClient);
}

此外,在此應用程序的 AAD 上,我設置了允許訪問圖形 API 的 API 角色User.ReadCalendars.ReadWrite

問題:

  • 我在 application.properties 上遺漏了一些東西來配置權限。
  • 我不知道@Autowire需要什么 bean 才能從 Vaadin 范圍內的用戶特定上下文訪問令牌。

評論:

  • 我正在使用“正常”身份驗證,並且該應用程序適用於擁有 O365 帳戶的同一家公司。 它不是多租戶或 B2C。

如果登錄完全由MS 身份驗證門戶處理,需要更多關於登錄的說明嗎?

我認為您可以在WebSecurityConfiguration類上添加更多過濾以允許使用其他 IP 登錄

    @Override
protected void configure(HttpSecurity http) throws Exception {
    http
            .addFilter(accessTokenProcessingFilter())
            .authenticationProvider(preAuthenticatedAuthenticationProvider())
            .exceptionHandling().and()
            .headers().and()
            .sessionManagement().sessionCreationPolicy(STATELESS).and()
            .securityContext().and()
            .anonymous().and()
            .authorizeRequests().antMatchers(HttpMethod.OPTIONS, "/**").permitAll()

...

暫無
暫無

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

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