访问Vaadin 23&#x2B中的Microsoft Graph API弹簧安全+ Azure AD

发布于 2025-01-25 05:29:16 字数 2919 浏览 4 评论 0原文

我正在开发一个企业Vaadin应用程序,我想知道是否有人弄清楚了如何获得JWT令牌,以(从后端)向Graplapi提出请求以获取其他用户详细信息。

我的安全配置看起来像这样。我通过这种配置实现的是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);
    }
}

在应用程序

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/

<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>
...

。 Spring-Boot-starter-for-As-Active-Dilectory-developer-guide#access-Resource-Servers-from-a-a-web-application“ rel =“ nofollow noreferrer”>有记录良好我应该如何能够能够取回令牌并打电话。

@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角色user.readcalendars.readwrite允许访问Graph API。

问题:

  • 我缺少应用程序上的内容。
  • 我不知道我需要@Autowire从vaadin范围中的用户特定上下文访问令牌。

备注:

  • 我正在使用“正常”身份验证,并且该应用程序适用于拥有O365帐户的同一公司。它不是多租户或B2C。

I'm developing an enterprise Vaadin application and I'd like to know if anyone has figured out how I can obtain the JWT token to make (from backend) a request to GraphAPI to fetch additional user details.

My Security Configuration looks like this. What I achieve with this configuration is a SSO experience. Just entering the site, the user is redirected to MS Authentication portal and redirected back when authenticated.

@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);
    }
}

On application.properties I have the following settings:

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/

and on 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>
...

From a Rest Endpoint, it's well documented how I should be able to retrieve the token and make a call.

@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);
}

Also, on AAD for this application I've setup the API roles User.Read and Calendars.ReadWrite that allows accessing graph API.

Questions:

  • I'm missing something on the application.properties to configure the permissions.
  • I have no idea of what bean I need to @Autowire to access the token from the user-specific context in Vaadin scope.

Remarks:

  • I'm using 'normal' authentication and the application is for the same company that has the O365 account. It's not multi-tenant or B2C.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

笑梦风尘 2025-02-01 05:29:16

如果登录名完全由 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()

...

Need some more clarification about the login, if the login is completely handled by the MS Authentication portal ?.

I think you can add some more filtration on you WebSecurityConfiguration class to permit login with other 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()

...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文