尝试验证azure令牌时出现401错误



嗨,我正在使用Which azure spring boot示例active directory示例来验证来自Vue.js应用程序的spring boot应用程序中的访问令牌?03用于验证令牌的资源服务器代码。但我在使用Postman时一直收到401的回复,而没有身体回复。可能是什么问题?我在过去的几天里一直被困在这个问题上请帮助

配置:

@EnableWebSecurity
@Order(SecurityProperties.BASIC_AUTH_ORDER)
public class WebSecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http.authorizeRequests()
.anyRequest().authenticated()
.and()
.oauth2ResourceServer()
.jwt()
.and();
}
}

控制器:

@RestController
@RequestMapping("/api")
public class HomeController {
@GetMapping("/asd")
@ResponseBody
public String home() {
return "Hello, this is resource server 1.";
}
}

application.yml

spring:
security:
oauth2:
resourceserver:
jwt:
jwk-set-uri: https://login.microsoftonline.com/{tenant-id}/discovery/keys
issuer-uri: https://login.microsoftonline.com/{tenant-id}/v2.0

pom.xml

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-oauth2-resource-server</artifactId>
</dependency>

您的Java代码看起来非常正确。我会从添加额外的日志到你的应用程序属性文件开始,看看这是否告诉你什么,例如:

logging:
level:
org:
springframework:
security: DEBUG

接下来,我将使用JWT查看器来查看访问令牌的JWT头中是否有nonce字段。如果是这样,那么它将无法通过验证-请参阅我最近的回答,了解Azure AD中exposing an API scope的更多信息。

最后,你可以暂时尝试另一个库,它可能会更好地解释原因。有关基于库的验证的示例,请参阅此jose4j代码。你可以将其粘贴到一个小型Java控制台应用程序中,并使用Azure访问令牌运行它。

最新更新