我想在 Spring Boot 2.4 中添加 Azure AD 作为 OAuth2 提供程序。我跟着 Spring Boot's OAuth2 docs 想出了以下配置:
spring.security.oauth2.client.provider.azuread.issuer-uri=https://login.microsoftonline.com/<tenant uuid>/v2.0
spring.security.oauth2.client.registration.azuread.authorization-grant-type=authorization_code
spring.security.oauth2.client.registration.azuread.client-id=<client uuid>
spring.security.oauth2.client.registration.azuread.client-name=Azure AD
spring.security.oauth2.client.registration.azuread.client-secret=<client secret>
spring.security.oauth2.client.registration.azuread.provider=azuread
spring.security.oauth2.client.registration.azuread.scope=openid
为了完整起见,这是我的网络安全配置:
@EnableWebSecurity
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
// @formatter:off
http
.authorizeRequests(a -> a
.antMatchers("/", "/login", "/error", "/webjars/**").permitAll()
.anyRequest().authenticated()
)
.oauth2Login();
// @formatter:on
}
}
在 https://login.microsoftonline.com 上输入凭据返回时,我收到以下错误:
[invalid_id_token] An error occurred while attempting to decode the Jwt: Signed JWT rejected: Another algorithm expected, or no matching key(s) found.
问题源自 Nimus-JOSE-JWT 的 DefaultJWTProcessor.java。
查看 Firefox 网络检查器中的请求,Spring Boot 从 Issuer URI 中选取正确的 URL。我不知道出了什么问题,并感谢您的指点。
答案 0 :(得分:0)
根据您的描述,您似乎想在 Spring Boot 中使用 azure ad 作为 OAuth2 提供程序,我发现doc 说'Spring Starter for Azure Active Directory (AD) 现在已与春季安全 5.0'。它还提供了 a sample springboot web 应用程序。我也试过了,确实有效。
我觉得需要注意的是,教程说azure ad中设置的redirect url是'http://localhost:8080/login/oauth2/code/',但是经过我的测试,结果是'http ://localhost:8080/login/oauth2/code/azure'。
根据您的错误,我不确定,但也许您可以将“范围”的配置更改为“openid,profile,offline_access”。
答案 1 :(得分:0)
Azure AD 有一些非常不直观(在我看来)的默认行为 - 我认为这就是您所遇到的:
你的问题原因(我认为)
你需要做什么
更多信息
答案 2 :(得分:0)
我遇到了类似的问题,如果我没记错的话,范围有问题。不知道这是否也是您的问题,但无论如何以下配置对我有用(注意我使用的是客户端凭据而不是身份验证代码):
spring:
security:
oauth2:
client:
provider:
azure:
token-uri: https://login.microsoftonline.com/${custom.azure.account.tenant-id}/oauth2/token
registration:
azure:
client-id: ${custom.azure.service-principal.client-id}
client-secret: ${custom.azure.service-principal.client-secret}
authorization-grant-type: client_credentials
scope:
- https://graph.microsoft.com/.default