【问题标题】:Spring Boot + Azure Active Directory Signed JWT rejected: Another algorithm expected, or no matching key(s) foundSpring Boot + Azure Active Directory 签名的 JWT 被拒绝:需要另一个算法,或者找不到匹配的密钥
【发布时间】:2022-01-15 02:09:29
【问题描述】:

我正在尝试使用 Web 服务为我的 Spring Boot 运行 azure Active Directory。问题是当我成功登录时,它会抛出一个错误:

我添加了以下属性(tetant-id、client-id、client-secret、user-group.allowed-group-names)

azure.activedirectory.redirect-uri-template=http://localhost:8080/login/oauth2/code/

我的配置是:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class AADSecurityConfiguration extends WebSecurityConfigurerAdapter {

  @Override
  public void configure(WebSecurity web) {
    web.ignoring().antMatchers("/health");
  }

  @Override
  protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
        .antMatchers("/oauth2/**", "/login/**")
        .permitAll()
        .anyRequest()
        .authenticated()
        .and()
        .oauth2Login();
  }
}

简单的控制器请求是:

 @GetMapping("/list")
  @PreAuthorize("hasRole('Admin') or hasRole('Users')")
  public String getListPage() {
    return "list";
  }

依赖的版本是:

<spring.security.version>5.6.0</spring.security.version>
<spring.boot.version>2.5.4</spring.boot.version>
<azure.version>3.10.0</azure.version>

  <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-oauth2-client</artifactId>
        <version>${spring.boot.version}</version>
    </dependency>

    <dependency>
        <groupId>com.azure.spring</groupId>
        <artifactId>azure-spring-boot-starter-active-directory</artifactId>
        <version>${azure.version}</version>
    </dependency>

    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-oauth2-jose</artifactId>
        <version>5.6.0</version>
    </dependency>

你能告诉我这个问题,以便我可以解决吗?

更新: 使用 msal4j 解决。

示例是:

https://github.com/Azure-Samples/ms-identity-java-webapp/tree/master/msal-java-webapp-sample

【问题讨论】:

  • 嗨@volkangurbuz,检查令牌使用jwt.io 验证声明:1)确保您选择了正确的签名算法(RS256)2)检查孩子 声明表示用于验证令牌的特定公钥。确保您正在检查用于签署令牌的密钥。3)验证 scp 声明以验证用户已授予调用应用程序调用您的 API 的权限。4)检查与 aud (audience) :标识令牌的预期接收者
  • 也可以参考这个帖子:stackoverflow.com/questions/56638408/…
  • 您的问题解决了吗?

标签: spring-boot spring-security azure-active-directory spring-security-oauth2


【解决方案1】:

检查令牌并使用https://jwt.io 验证声明:

1)确保您选择了正确的签名算法(RS256) JWT 的标头包含有关用于签署令牌的密钥和加密方法的信息:

例子:

{
  "typ": "JWT",
  "alg": "RS256",
  "x5t": "iBjL1Rcqzhiy4fpxIxdZqohM2Yk",
  "kid": "iBjL1Rcqzhiy4fpxIxdZqohM2Yk"
}

2) 检查 kid 声明表明用于验证令牌的特定公钥。确保您正在检查用于签署令牌的密钥。

3) 验证 scp 声明以验证用户已授予调用应用程序调用您的 API 的权限。

4)aud (audience) 核对:这可识别令牌的预期接收者 - 其受众。如果值不匹配,您的 API 必须验证此值并拒绝令牌。在 v2.0 令牌中,这始终是 API 的客户端 ID,而在 v1.0 令牌中,它可以是客户端 ID 或请求中使用的资源 URI,具体取决于客户端如何请求令牌。

5) roleswids 声称验证用户自己有权调用您的 API。例如,管理员可能有权写入您的 API,但不是普通用户。

有关更多详细信息,请参阅这些文档

  1. An error occurred while attempting to decode the Jwt: Signed JWT rejected: Another algorithm expected, or no matching key(s) found
  2. Microsoft identity platform access tokens - Microsoft identity platform | Microsoft Docs

【讨论】:

猜你喜欢
  • 2021-11-18
  • 2019-10-31
  • 2021-04-06
  • 1970-01-01
  • 2018-06-26
  • 2022-01-05
  • 2020-02-08
  • 1970-01-01
  • 2020-11-14
相关资源
最近更新 更多