简体   繁体   English

如何从 Spring 检索 azure AD JWT 访问令牌?

[英]How can I retrieve the azure AD JWT access token from Spring?

I'm trying to retrieve the azure JWT access token from my Spring Boot application from another application by querying a /token endpoint, but the token I receive is seemingly incorrect.我正在尝试通过查询 /token 端点从另一个应用程序的 Spring Boot 应用程序中检索 azure JWT 访问令牌,但我收到的令牌似乎不正确。

The project has a Spring Boot backend and an Eclipse rcp frontend.该项目有一个 Spring Boot 后端和一个 Eclipse rcp 前端。 I'm attempting to retrieve the access token from the eclipse frontend.我正在尝试从 eclipse 前端检索访问令牌。 For this, I have the controller below:为此,我有以下控制器:

    @Autowired
    private OAuth2AuthorizedClientService authorizedClientService;

    @GetMapping("/token")
    public String user(OAuth2AuthenticationToken authentication) {

        OAuth2AuthorizedClient authorizedClient = this.authorizedClientService
                        .loadAuthorizedClient(authentication.getAuthorizedClientRegistrationId(), authentication.getName());
        return authorizedClient.getAccessToken().getTokenValue();
    }

Which returns a token with the following format:它返回一个具有以下格式的令牌:

PAQABAAAAAABeAFzDwllzTYGDLh_qYbH8hgtbYMB8x7YLamQyQPk_MEXyd9Ckc5epDFQMv3RxjmMie0JDr5uN82U4RFLgU3fnDBxGolo4XVwzLEsTZDmUK_r0YG6ZwLbbQI_ch_Xn8xCxhsFq-AoRbEESDqK3GmK4eXwCYoT0G8_XfZjHTvCNTOMqUb2Q-CD2EalIKf0zSZ5184qrvlXfdNeT_BJdH_tqaodn80Bp2UL2hdnOCDZuWRqKl_2fi4v-eOOKJCcjOqY6SreVEeoKkIvVdayGE8F6qCxFehmlA0sX9sVW34FIVYVo4lDRsTkm-WN2KJwxJmalNcxg0k2ObDnIeC1ulPPpiPq-O_LK9bVA4HEZ63cJi9ZwQHwLPUhOO6TquoCOroHSy5KPoFkX3N796hM1i0NpaaY4MeAx17CSYeZ9P06jvYD7UMTV3OwWt-OVrDm5z_AvbOvyHRf9wjh31H6oLoc-iu_NCspT6NzC2UZQSHBtKdydEcP6sNkRp073jrZEg8UtcVT6HzddIBk2P0tVeIiSyU3SfLETbzJE67xtJVip3ai9aLN28c0qt3rDBaVGDAXjXhqrh5D3NiXdQjS6YTAKy0bVmNk9Yr9o2CGBA2wFjE8OZ6_Hb3k8_13KMJHafx0gAA

Dependencies from pom.xml来自 pom.xml 的依赖

Built using spring boot with the following relevant dependencies:使用具有以下相关依赖项的 spring boot 构建:

  • spring-boot-starter-web v2.2.4 spring-boot-starter-web v2.2.4
  • azure-active-directory-spring-boot-starter v2.2.1 azure-active-directory-spring-boot-starter v2.2.1
  • spring-security-oauth2-client v5.2.1 spring-security-oauth2-client v5.2.1
  • spring-security-oauth2-jose v5.2.1 spring-security-oauth2-jose v5.2.1
  • spring-security-oauth2-resource-server v5.2.1 spring-security-oauth2-resource-server v5.2.1

Config from application.yml来自 application.yml 的配置

We support multiple authorization servers, here is the fully configured azure client:我们支持多个授权服务器,这里是完全配置好的 azure 客户端:

spring:
  security:
    oauth2:
      client:
        azure:
          client-id: XXX
          client-secret: XXX
          client-name: Microsoft
          scope: openid, https://graph.microsoft.com/user.read, profile
          authorization-grant-type: authorization_code
          redirect-uri: http://localhost:8080/login/oauth2/code/azure
          client-authentication-method: basic
          authentication-method: post
      provider:
        authorization-uri: https://login.microsoftonline.com/XXX/oauth2/authorize
        token-uri: https://login.microsoftonline.com/XXX/oauth2/token
        user-info-uri: https://login.microsoftonline.com/XXX/openid/userinfo
        jwt-set-uri: https://login.microsoftonline.com/dXXX/discovery/keys

azure:
   activedirectory:
      tenant-id: XXX
      active-directory-groups: XXX
      allow-telemetry: false

websecurityconfig.java网络安全配置文件

@Configuration
@EnableConfigurationProperties
@EnableWebSecurity
@Order(1)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    private OAuth2UserService<OidcUserRequest, OidcUser> oidcUserService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http.csrf().disable()
                .authorizeRequests()
                    [...]
                    .anyRequest().authenticated();

        http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt)

        http.oauth2Login()
                .userInfoEndpoint()
                .oidcUserService(oidcUserService)
                .and()
                .authorizationEndpoint();
    }

    [...]
}

This is how I ended up obtaining the open id token from Azure这就是我最终从 Azure 获取开放 ID 令牌的方式

@GetMapping("/token")
public String user(OAuth2AuthenticationToken authentication) {
    DefaultOidcUser user = (DefaultOidcUser) authentication.getPrincipal();
    return user.getIdToken().getTokenValue();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Spring Keycloak - 如何从 JWT 访问令牌设置主体 - Spring Keycloak - How to set principal from JWT access token 如何验证从 Java 中的 Azure AD B2C 生成的 JWT 令牌? - How to validate JWT token generated from Azure AD B2C in Java? Spring OAuth / JWT从访问令牌获取额外信息 - Spring OAuth/JWT get extra information from access token JWT 令牌的签名无效 - Azure + Spring 引导 - Invalid signature of JWT token - Azure + Spring Boot 如何在其他 java 微服务(spring)中重用令牌(jwt)? - How can I re-use token (jwt) in other java microservices (spring)? 如何将一些附加字段添加到此 Spring 引导服务生成的 JWT 令牌中? - How can I add some additional fields into a JWT token generated by this Spring Boot service? 任何人都可以建议如何验证(来自可靠来源,未过期)JWT 令牌来自 header 在 spring webflux - Can anyone suggest how to validate (coming from realiable source,not expired) JWT token coming in header in spring webflux 使用 Stomp 访问 Spring 中的 JWT 令牌 - Access JWT token in Spring Boot with Stomp jwt 访问令牌存储在 spring 引导中的位置? - Where jwt access token is stored in spring boot? 如何使用Webflux访问Spring API处理程序方法中的JWT声明? - How can I access JWT claims in Spring API handler methods using Webflux?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM